X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Ftime.c;h=90ce4d4b67694bd046b3ddd589bf27e9644d1231;hp=5c5594584dc6ae37c59ab44625ca7ad9b8c9534d;hb=0db0f8e075e3790a2707736d2a4d086b123f3160;hpb=507f56949c928c084e647fa88498b823aeab585d diff --git a/src/common/time.c b/src/common/time.c index 5c5594584..90ce4d4b6 100644 --- a/src/common/time.c +++ b/src/common/time.c @@ -1,27 +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) @@ -57,3 +59,80 @@ struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC; return res; } + +static +void __attribute__((constructor)) init_locale_utf8_support(void) +{ + const char *program_locale = setlocale(LC_ALL, NULL); + const char *lang = getenv("LANG"); + + if (program_locale && strstr(program_locale, "utf8")) { + utf8_output_supported = true; + } else if (lang && strstr(lang, "utf8")) { + 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; +} + +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; +}