Metadata: add env fields to ease lttng path hierarchy creation for viewer
[lttng-tools.git] / src / common / time.c
index 5519e3ab4715240cfd1b27ef7701d36c1b737039..c69002f3456c33de8ea6ff33fef7610d5745d3b2 100644 (file)
@@ -16,7 +16,9 @@
  */
 
 #include <common/time.h>
+#include <common/error.h>
 #include <common/macros.h>
+#include <common/error.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <limits.h>
@@ -76,7 +78,39 @@ void __attribute__((constructor)) init_locale_utf8_support(void)
 
        if (program_locale && strstr(program_locale, "utf8")) {
                utf8_output_supported = true;
-       } else if (strstr(lang, "utf8")) {
+       } 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;
+}
This page took 0.023951 seconds and 4 git commands to generate.