X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Ftime.c;h=d2d2503d9f42962e5a83371b22309ef19c13001a;hp=c69002f3456c33de8ea6ff33fef7610d5745d3b2;hb=a8b66566890a353cc3a89836281541195ad69d73;hpb=a70ac2f49aa47a30abc2b50ec57b582eab4b1028 diff --git a/src/common/time.c b/src/common/time.c index c69002f34..d2d2503d9 100644 --- a/src/common/time.c +++ b/src/common/time.c @@ -114,3 +114,35 @@ int time_to_iso8601_str(time_t time, char *str, size_t len) end: return ret; } + +LTTNG_HIDDEN +int time_to_datetime_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 < DATETIME_STR_LEN) { + ERR("Buffer too short to format to datetime: %zu bytes provided when at least %zu are needed", + len, DATETIME_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%d-%H%M%S", tm_result); + if (strf_ret == 0) { + ret = -1; + ERR("Failed to format timestamp as local time"); + goto end; + } +end: + return ret; +}