docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / common / time.cpp
1 /*
2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/time.hpp>
9 #include <common/error.hpp>
10 #include <common/macros.hpp>
11 #include <common/error.hpp>
12 #include <common/compat/errno.hpp>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <limits.h>
16 #include <pthread.h>
17 #include <locale.h>
18 #include <string.h>
19 #include <algorithm>
20
21 static bool utf8_output_supported;
22
23 bool locale_supports_utf8(void)
24 {
25 return utf8_output_supported;
26 }
27
28 int timespec_to_ms(struct timespec ts, unsigned long *ms)
29 {
30 unsigned long res, remain_ms;
31
32 if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) {
33 errno = EOVERFLOW;
34 return -1; /* multiplication overflow */
35 }
36 res = ts.tv_sec * MSEC_PER_SEC;
37 remain_ms = ULONG_MAX - res;
38 if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) {
39 errno = EOVERFLOW;
40 return -1; /* addition overflow */
41 }
42 res += ts.tv_nsec / NSEC_PER_MSEC;
43 *ms = res;
44 return 0;
45 }
46
47 struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2)
48 {
49 uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC +
50 (uint64_t) t1.tv_nsec;
51 uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC +
52 (uint64_t) t2.tv_nsec;
53 uint64_t diff = std::max(ts1, ts2) - std::min(ts1, ts2);
54 struct timespec res;
55
56 res.tv_sec = diff / (uint64_t) NSEC_PER_SEC;
57 res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC;
58 return res;
59 }
60
61 static
62 void __attribute__((constructor)) init_locale_utf8_support(void)
63 {
64 const char *program_locale = setlocale(LC_ALL, NULL);
65 const char *lang = getenv("LANG");
66
67 if (program_locale && strstr(program_locale, "utf8")) {
68 utf8_output_supported = true;
69 } else if (lang && strstr(lang, "utf8")) {
70 utf8_output_supported = true;
71 }
72 }
73
74 int time_to_iso8601_str(time_t time, char *str, size_t len)
75 {
76 int ret = 0;
77 struct tm *tm_result;
78 struct tm tm_storage;
79 size_t strf_ret;
80
81 if (len < ISO8601_STR_LEN) {
82 ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed",
83 len, ISO8601_STR_LEN);
84 ret = -1;
85 goto end;
86 }
87
88 tm_result = localtime_r(&time, &tm_storage);
89 if (!tm_result) {
90 ret = -1;
91 PERROR("Failed to break down timestamp to tm structure");
92 goto end;
93 }
94
95 strf_ret = strftime(str, len, "%Y%m%dT%H%M%S%z", tm_result);
96 if (strf_ret == 0) {
97 ret = -1;
98 ERR("Failed to format timestamp as local time");
99 goto end;
100 }
101 end:
102 return ret;
103 }
104
105 int time_to_datetime_str(time_t time, char *str, size_t len)
106 {
107 int ret = 0;
108 struct tm *tm_result;
109 struct tm tm_storage;
110 size_t strf_ret;
111
112 if (len < DATETIME_STR_LEN) {
113 ERR("Buffer too short to format to datetime: %zu bytes provided when at least %zu are needed",
114 len, DATETIME_STR_LEN);
115 ret = -1;
116 goto end;
117 }
118
119 tm_result = localtime_r(&time, &tm_storage);
120 if (!tm_result) {
121 ret = -1;
122 PERROR("Failed to break down timestamp to tm structure");
123 goto end;
124 }
125
126 strf_ret = strftime(str, len, "%Y%m%d-%H%M%S", tm_result);
127 if (strf_ret == 0) {
128 ret = -1;
129 ERR("Failed to format timestamp as local time");
130 goto end;
131 }
132 end:
133 return ret;
134 }
This page took 0.030295 seconds and 4 git commands to generate.