X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Ftime.c;h=ccb5874746b713f3872bee059d6a1998af3495d7;hb=48a4000561343808724f7cb5fa8c131877489ccd;hp=885f58cb175bf4a2336cf957047abcc339642c15;hpb=274ca939e0a5baa74d4ff61518ed0af7b0da5465;p=lttng-tools.git diff --git a/src/common/time.c b/src/common/time.c index 885f58cb1..ccb587474 100644 --- a/src/common/time.c +++ b/src/common/time.c @@ -1,40 +1,29 @@ /* - * Copyright (C) 2013 - Jérémie Galarneau + * Copyright (C) 2013 Jérémie Galarneau * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License, version 2 only, as - * published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 51 - * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include +#include #include #include +#include #include #include #include -#include #include #include #include static bool utf8_output_supported; -LTTNG_HIDDEN bool locale_supports_utf8(void) { return utf8_output_supported; } -LTTNG_HIDDEN int timespec_to_ms(struct timespec ts, unsigned long *ms) { unsigned long res, remain_ms; @@ -54,7 +43,6 @@ int timespec_to_ms(struct timespec ts, unsigned long *ms) return 0; } -LTTNG_HIDDEN struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) { uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + @@ -82,7 +70,6 @@ void __attribute__((constructor)) init_locale_utf8_support(void) } } -LTTNG_HIDDEN int time_to_iso8601_str(time_t time, char *str, size_t len) { int ret = 0; @@ -97,7 +84,7 @@ int time_to_iso8601_str(time_t time, char *str, size_t len) goto end; } - tm_result = localtime_r(&time, &tm_storage); + tm_result = localtime_r(&time, &tm_storage); if (!tm_result) { ret = -1; PERROR("Failed to break down timestamp to tm structure"); @@ -113,3 +100,34 @@ int time_to_iso8601_str(time_t time, char *str, size_t len) end: return ret; } + +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; +}