From: Jérémie Galarneau Date: Thu, 5 May 2022 19:15:44 +0000 (-0400) Subject: Add lttng::utils::time_to_iso8601_str X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=332a2147fece4548eac38a0929b208230a8da31f;p=deliverable%2Flttng-tools.git Add lttng::utils::time_to_iso8601_str 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 Change-Id: I0bd7dbbdc2c3bae6fdef7917936450953af72175 --- diff --git a/src/common/time.cpp b/src/common/time.cpp index 714991246..a60fcf23c 100644 --- a/src/common/time.cpp +++ b/src/common/time.cpp @@ -5,18 +5,19 @@ * */ -#include +#include #include +#include #include -#include -#include -#include -#include +#include + +#include #include -#include #include +#include +#include +#include #include -#include 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; +} diff --git a/src/common/time.hpp b/src/common/time.hpp index 724b202b1..34c41a08a 100644 --- a/src/common/time.hpp +++ b/src/common/time.hpp @@ -9,7 +9,10 @@ #define LTTNG_TIME_H #include +#include #include +#include + #include #include @@ -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);