Build fix: missing initializer for member 'payload'
[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
2a1135fa
JG
24bool locale_supports_utf8(void)
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;
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
507f5694
JG
48struct 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;
a6bc4ca9 54 uint64_t diff = std::max(ts1, ts2) - std::min(ts1, ts2);
507f5694
JG
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}
2a1135fa
JG
61
62static
63void __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;
0731b11e 70 } else if (lang && strstr(lang, "utf8")) {
2a1135fa
JG
71 utf8_output_supported = true;
72 }
73}
274ca939 74
274ca939
JG
75int 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
5e5af4be 89 tm_result = localtime_r(&time, &tm_storage);
274ca939
JG
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 }
102end:
103 return ret;
104}
a8b66566 105
a8b66566
JR
106int 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 }
133end:
134 return ret;
135}
332a2147
JG
136
137std::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.051937 seconds and 4 git commands to generate.