Add lttng::utils::time_to_iso8601_str
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 5 May 2022 19:15:44 +0000 (15:15 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 13 Jun 2022 20:34:46 +0000 (16:34 -0400)
lttng::utils::time_to_iso8601_str implements the same formatting
as time_to_iso8601_str, but returns an std::string.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I0bd7dbbdc2c3bae6fdef7917936450953af72175

src/common/time.cpp
src/common/time.hpp

index 7149912468d2c9d7a6acd41d41e13fe9eed92e90..a60fcf23cfabf4c53edc3085084ca88a77996c76 100644 (file)
@@ -5,18 +5,19 @@
  *
  */
 
-#include <common/time.hpp>
+#include <common/compat/errno.hpp>
 #include <common/error.hpp>
+#include <common/exception.hpp>
 #include <common/macros.hpp>
-#include <common/error.hpp>
-#include <common/compat/errno.hpp>
-#include <stddef.h>
-#include <stdint.h>
+#include <common/time.hpp>
+
+#include <algorithm>
 #include <limits.h>
-#include <pthread.h>
 #include <locale.h>
+#include <pthread.h>
+#include <stddef.h>
+#include <stdint.h>
 #include <string.h>
-#include <algorithm>
 
 static bool utf8_output_supported;
 
@@ -132,3 +133,18 @@ int time_to_datetime_str(time_t time, char *str, size_t len)
 end:
        return ret;
 }
+
+std::string lttng::utils::time_to_iso8601_str(std::time_t time)
+{
+       std::string iso8601_str(ISO8601_STR_LEN, '\0');
+       const auto ret = ::time_to_iso8601_str(time, &iso8601_str[0], iso8601_str.capacity());
+
+       if (ret) {
+               LTTNG_THROW_ERROR("Failed to format time to iso8601 format");
+       }
+
+       /* Don't include '\0' in the C++ string. */
+       iso8601_str.resize(iso8601_str.size() - 1);
+
+       return iso8601_str;
+}
index 724b202b1025643ea503f5f7c6dd7a24b990c4e9..34c41a08adbd9837018489546c4dcc2a7894bd36 100644 (file)
@@ -9,7 +9,10 @@
 #define LTTNG_TIME_H
 
 #include <time.h>
+#include <ctime>
 #include <stdbool.h>
+#include <string>
+
 #include <common/macros.hpp>
 #include <common/compat/time.hpp>
 
@@ -59,6 +62,13 @@ struct timespec timespec_abs_diff(struct timespec ts_a, struct timespec ts_b);
  * Returns 0 on success, else -1 on error.
  */
 int time_to_iso8601_str(time_t time, char *str, size_t len);
+namespace lttng {
+namespace utils {
+
+std::string time_to_iso8601_str(time_t time);
+
+} /* namespace utils */
+} /* namespace lttng */
 
 int time_to_datetime_str(time_t time, char *str, size_t len);
 
This page took 0.026277 seconds and 4 git commands to generate.