X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcommon%2Ftime.c;h=387db3e3273224ff7f0837f02725ce36bfc92976;hb=a113ee0dd7fdba285a379289d47351f6e39c51db;hp=5c5594584dc6ae37c59ab44625ca7ad9b8c9534d;hpb=507f56949c928c084e647fa88498b823aeab585d;p=lttng-tools.git diff --git a/src/common/time.c b/src/common/time.c index 5c5594584..387db3e32 100644 --- a/src/common/time.c +++ b/src/common/time.c @@ -16,12 +16,23 @@ */ #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 +68,47 @@ struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC; return res; } + +LTTNG_HIDDEN +int time_t_to_ISO8601(char *dest, size_t dest_size, time_t time) +{ + int ret; + struct tm tm, *timeinfo; + + if (dest_size < ISO8601_LEN) { + ERR("Failed to format time to ISO8601 destination too small"); + ret = -1; + goto end; + } + + timeinfo = localtime_r(&time, &tm); + if (!timeinfo) { + PERROR("localtime"); + ret = -1; + goto end; + } + + ret = strftime(dest, dest_size, ISO8601_FORMAT, timeinfo); + if (ret == 0) { + ERR("Failed to format time to ISO8601"); + ret = -1; + goto end; + } + + ret = 0; +end: + return ret; +} + +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; + } +}