X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Ftime.c;h=885f58cb175bf4a2336cf957047abcc339642c15;hp=a01c16df5c44f024c00469beeaa014542c658c9f;hb=274ca939e0a5baa74d4ff61518ed0af7b0da5465;hpb=fd774fc6fa1c368a400976ad362e4f60f46e9861 diff --git a/src/common/time.c b/src/common/time.c index a01c16df5..885f58cb1 100644 --- a/src/common/time.c +++ b/src/common/time.c @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -80,3 +81,35 @@ void __attribute__((constructor)) init_locale_utf8_support(void) utf8_output_supported = true; } } + +LTTNG_HIDDEN +int time_to_iso8601_str(time_t time, char *str, size_t len) +{ + int ret = 0; + struct tm *tm_result; + struct tm tm_storage; + size_t strf_ret; + + if (len < ISO8601_STR_LEN) { + ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed", + len, ISO8601_STR_LEN); + ret = -1; + goto end; + } + + tm_result = localtime_r(&time, &tm_storage); + if (!tm_result) { + ret = -1; + PERROR("Failed to break down timestamp to tm structure"); + goto end; + } + + strf_ret = strftime(str, len, "%Y%m%dT%H%M%S%z", tm_result); + if (strf_ret == 0) { + ret = -1; + ERR("Failed to format timestamp as local time"); + goto end; + } +end: + return ret; +}