tests: Use `--no-wait` when destroying sessions in relayd-grouping
[lttng-tools.git] / src / common / time.cpp
CommitLineData
507f5694 1/*
ab5be9fa 2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
507f5694 3 *
c922647d 4 * SPDX-License-Identifier: LGPL-2.1-only
507f5694 5 *
507f5694
JG
6 */
7
332a2147 8#include <common/compat/errno.hpp>
c9e313bc 9#include <common/error.hpp>
332a2147 10#include <common/exception.hpp>
c9e313bc 11#include <common/macros.hpp>
332a2147
JG
12#include <common/time.hpp>
13
14#include <algorithm>
507f5694 15#include <limits.h>
2a1135fa 16#include <locale.h>
332a2147
JG
17#include <pthread.h>
18#include <stddef.h>
19#include <stdint.h>
2a1135fa
JG
20#include <string.h>
21
22static bool utf8_output_supported;
23
cd9adb8b 24bool locale_supports_utf8()
2a1135fa
JG
25{
26 return utf8_output_supported;
27}
507f5694 28
507f5694
JG
29int 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;
28ab034a 35 return -1; /* multiplication overflow */
507f5694
JG
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;
28ab034a 41 return -1; /* addition overflow */
507f5694
JG
42 }
43 res += ts.tv_nsec / NSEC_PER_MSEC;
44 *ms = res;
45 return 0;
46}
47
507f5694
JG
48struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2)
49{
28ab034a
JG
50 uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + (uint64_t) t1.tv_nsec;
51 uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + (uint64_t) t2.tv_nsec;
a6bc4ca9 52 uint64_t diff = std::max(ts1, ts2) - std::min(ts1, ts2);
507f5694
JG
53 struct timespec res;
54
55 res.tv_sec = diff / (uint64_t) NSEC_PER_SEC;
56 res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC;
57 return res;
58}
2a1135fa 59
cd9adb8b 60static void __attribute__((constructor)) init_locale_utf8_support()
2a1135fa 61{
cd9adb8b 62 const char *program_locale = setlocale(LC_ALL, nullptr);
2a1135fa
JG
63 const char *lang = getenv("LANG");
64
65 if (program_locale && strstr(program_locale, "utf8")) {
66 utf8_output_supported = true;
0731b11e 67 } else if (lang && strstr(lang, "utf8")) {
2a1135fa
JG
68 utf8_output_supported = true;
69 }
70}
274ca939 71
274ca939
JG
72int time_to_iso8601_str(time_t time, char *str, size_t len)
73{
74 int ret = 0;
75 struct tm *tm_result;
76 struct tm tm_storage;
77 size_t strf_ret;
78
79 if (len < ISO8601_STR_LEN) {
80 ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed",
28ab034a
JG
81 len,
82 ISO8601_STR_LEN);
274ca939
JG
83 ret = -1;
84 goto end;
85 }
86
5e5af4be 87 tm_result = localtime_r(&time, &tm_storage);
274ca939
JG
88 if (!tm_result) {
89 ret = -1;
90 PERROR("Failed to break down timestamp to tm structure");
91 goto end;
92 }
93
94 strf_ret = strftime(str, len, "%Y%m%dT%H%M%S%z", tm_result);
95 if (strf_ret == 0) {
96 ret = -1;
97 ERR("Failed to format timestamp as local time");
98 goto end;
99 }
100end:
101 return ret;
102}
a8b66566 103
a8b66566
JR
104int time_to_datetime_str(time_t time, char *str, size_t len)
105{
106 int ret = 0;
107 struct tm *tm_result;
108 struct tm tm_storage;
109 size_t strf_ret;
110
111 if (len < DATETIME_STR_LEN) {
112 ERR("Buffer too short to format to datetime: %zu bytes provided when at least %zu are needed",
28ab034a
JG
113 len,
114 DATETIME_STR_LEN);
a8b66566
JR
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 }
132end:
133 return ret;
134}
332a2147
JG
135
136std::string lttng::utils::time_to_iso8601_str(std::time_t time)
137{
138 std::string iso8601_str(ISO8601_STR_LEN, '\0');
139 const auto ret = ::time_to_iso8601_str(time, &iso8601_str[0], iso8601_str.capacity());
140
141 if (ret) {
142 LTTNG_THROW_ERROR("Failed to format time to iso8601 format");
143 }
144
145 /* Don't include '\0' in the C++ string. */
146 iso8601_str.resize(iso8601_str.size() - 1);
147
148 return iso8601_str;
149}
This page took 0.062676 seconds and 4 git commands to generate.