Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / common / time.cpp
... / ...
CommitLineData
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
22static bool utf8_output_supported;
23
24bool locale_supports_utf8()
25{
26 return utf8_output_supported;
27}
28
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
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 + (uint64_t) t1.tv_nsec;
51 uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + (uint64_t) t2.tv_nsec;
52 uint64_t diff = std::max(ts1, ts2) - std::min(ts1, ts2);
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}
59
60static void __attribute__((constructor)) init_locale_utf8_support()
61{
62 const char *program_locale = setlocale(LC_ALL, nullptr);
63 const char *lang = getenv("LANG");
64
65 if (program_locale && strstr(program_locale, "utf8")) {
66 utf8_output_supported = true;
67 } else if (lang && strstr(lang, "utf8")) {
68 utf8_output_supported = true;
69 }
70}
71
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",
81 len,
82 ISO8601_STR_LEN);
83 ret = -1;
84 goto end;
85 }
86
87 tm_result = localtime_r(&time, &tm_storage);
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}
103
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",
113 len,
114 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 }
132end:
133 return ret;
134}
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.023016 seconds and 4 git commands to generate.