From 332a2147fece4548eac38a0929b208230a8da31f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 5 May 2022 15:15:44 -0400 Subject: [PATCH] Add lttng::utils::time_to_iso8601_str MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/common/time.cpp | 30 +++++++++++++++++++++++------- src/common/time.hpp | 10 ++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) 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); -- 2.34.1