X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Ftime.c;fp=src%2Fcommon%2Ftime.c;h=0000000000000000000000000000000000000000;hp=ccb5874746b713f3872bee059d6a1998af3495d7;hb=a6bc4ca9d659caf016ef932fcd944029737ac57c;hpb=97535efaa975ca52bf02c2d5e76351bfd2e3defa diff --git a/src/common/time.c b/src/common/time.c deleted file mode 100644 index ccb587474..000000000 --- a/src/common/time.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2013 Jérémie Galarneau - * - * SPDX-License-Identifier: GPL-2.0-only - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static bool utf8_output_supported; - -bool locale_supports_utf8(void) -{ - return utf8_output_supported; -} - -int timespec_to_ms(struct timespec ts, unsigned long *ms) -{ - unsigned long res, remain_ms; - - if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) { - errno = EOVERFLOW; - return -1; /* multiplication overflow */ - } - res = ts.tv_sec * MSEC_PER_SEC; - remain_ms = ULONG_MAX - res; - if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) { - errno = EOVERFLOW; - return -1; /* addition overflow */ - } - res += ts.tv_nsec / NSEC_PER_MSEC; - *ms = res; - return 0; -} - -struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) -{ - uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + - (uint64_t) t1.tv_nsec; - uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + - (uint64_t) t2.tv_nsec; - uint64_t diff = max(ts1, ts2) - min(ts1, ts2); - struct timespec res; - - res.tv_sec = diff / (uint64_t) NSEC_PER_SEC; - 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; - } -} - -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; -} - -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; -}