Fix: lttng: poptGetArg doesn't provide string ownership
[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/compat/errno.hpp>
9 #include <common/error.hpp>
10 #include <common/exception.hpp>
11 #include <common/macros.hpp>
12 #include <common/time.hpp>
13
14 #include <algorithm>
15 #include <limits.h>
16 #include <locale.h>
17 #include <pthread.h>
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <string.h>
21
22 static bool utf8_output_supported;
23
24 bool locale_supports_utf8(void)
25 {
26 return utf8_output_supported;
27 }
28
29 int timespec_to_ms(struct timespec ts, unsigned long *ms)
30 {
31 unsigned long res, remain_ms;
32
33 if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) {
34 errno = EOVERFLOW;
35 return -1; /* multiplication overflow */
36 }
37 res = ts.tv_sec * MSEC_PER_SEC;
38 remain_ms = ULONG_MAX - res;
39 if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) {
40 errno = EOVERFLOW;
41 return -1; /* addition overflow */
42 }
43 res += ts.tv_nsec / NSEC_PER_MSEC;
44 *ms = res;
45 return 0;
46 }
47
48 struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2)
49 {
50 uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC +
51 (uint64_t) t1.tv_nsec;
52 uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC +
53 (uint64_t) t2.tv_nsec;
54 uint64_t diff = std::max(ts1, ts2) - std::min(ts1, ts2);
55 struct timespec res;
56
57 res.tv_sec = diff / (uint64_t) NSEC_PER_SEC;
58 res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC;
59 return res;
60 }
61
62 static
63 void __attribute__((constructor)) init_locale_utf8_support(void)
64 {
65 const char *program_locale = setlocale(LC_ALL, NULL);
66 const char *lang = getenv("LANG");
67
68 if (program_locale && strstr(program_locale, "utf8")) {
69 utf8_output_supported = true;
70 } else if (lang && strstr(lang, "utf8")) {
71 utf8_output_supported = true;
72 }
73 }
74
75 int time_to_iso8601_str(time_t time, char *str, size_t len)
76 {
77 int ret = 0;
78 struct tm *tm_result;
79 struct tm tm_storage;
80 size_t strf_ret;
81
82 if (len < ISO8601_STR_LEN) {
83 ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed",
84 len, ISO8601_STR_LEN);
85 ret = -1;
86 goto end;
87 }
88
89 tm_result = localtime_r(&time, &tm_storage);
90 if (!tm_result) {
91 ret = -1;
92 PERROR("Failed to break down timestamp to tm structure");
93 goto end;
94 }
95
96 strf_ret = strftime(str, len, "%Y%m%dT%H%M%S%z", tm_result);
97 if (strf_ret == 0) {
98 ret = -1;
99 ERR("Failed to format timestamp as local time");
100 goto end;
101 }
102 end:
103 return ret;
104 }
105
106 int time_to_datetime_str(time_t time, char *str, size_t len)
107 {
108 int ret = 0;
109 struct tm *tm_result;
110 struct tm tm_storage;
111 size_t strf_ret;
112
113 if (len < DATETIME_STR_LEN) {
114 ERR("Buffer too short to format to datetime: %zu bytes provided when at least %zu are needed",
115 len, DATETIME_STR_LEN);
116 ret = -1;
117 goto end;
118 }
119
120 tm_result = localtime_r(&time, &tm_storage);
121 if (!tm_result) {
122 ret = -1;
123 PERROR("Failed to break down timestamp to tm structure");
124 goto end;
125 }
126
127 strf_ret = strftime(str, len, "%Y%m%d-%H%M%S", tm_result);
128 if (strf_ret == 0) {
129 ret = -1;
130 ERR("Failed to format timestamp as local time");
131 goto end;
132 }
133 end:
134 return ret;
135 }
136
137 std::string lttng::utils::time_to_iso8601_str(std::time_t time)
138 {
139 std::string iso8601_str(ISO8601_STR_LEN, '\0');
140 const auto ret = ::time_to_iso8601_str(time, &iso8601_str[0], iso8601_str.capacity());
141
142 if (ret) {
143 LTTNG_THROW_ERROR("Failed to format time to iso8601 format");
144 }
145
146 /* Don't include '\0' in the C++ string. */
147 iso8601_str.resize(iso8601_str.size() - 1);
148
149 return iso8601_str;
150 }
This page took 0.034587 seconds and 4 git commands to generate.