2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
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>
22 static bool utf8_output_supported
;
24 bool locale_supports_utf8()
26 return utf8_output_supported
;
29 int timespec_to_ms(struct timespec ts
, unsigned long *ms
)
31 unsigned long res
, remain_ms
;
33 if (ts
.tv_sec
> ULONG_MAX
/ MSEC_PER_SEC
) {
35 return -1; /* multiplication overflow */
37 res
= ts
.tv_sec
* MSEC_PER_SEC
;
38 remain_ms
= ULONG_MAX
- res
;
39 if (ts
.tv_nsec
/ NSEC_PER_MSEC
> remain_ms
) {
41 return -1; /* addition overflow */
43 res
+= ts
.tv_nsec
/ NSEC_PER_MSEC
;
48 struct timespec
timespec_abs_diff(struct timespec t1
, struct timespec t2
)
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
);
55 res
.tv_sec
= diff
/ (uint64_t) NSEC_PER_SEC
;
56 res
.tv_nsec
= diff
% (uint64_t) NSEC_PER_SEC
;
60 static void __attribute__((constructor
)) init_locale_utf8_support()
62 const char *program_locale
= setlocale(LC_ALL
, nullptr);
63 const char *lang
= getenv("LANG");
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;
72 int time_to_iso8601_str(time_t time
, char *str
, size_t len
)
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",
87 tm_result
= localtime_r(&time
, &tm_storage
);
90 PERROR("Failed to break down timestamp to tm structure");
94 strf_ret
= strftime(str
, len
, "%Y%m%dT%H%M%S%z", tm_result
);
97 ERR("Failed to format timestamp as local time");
104 int time_to_datetime_str(time_t time
, char *str
, size_t len
)
107 struct tm
*tm_result
;
108 struct tm tm_storage
;
111 if (len
< DATETIME_STR_LEN
) {
112 ERR("Buffer too short to format to datetime: %zu bytes provided when at least %zu are needed",
119 tm_result
= localtime_r(&time
, &tm_storage
);
122 PERROR("Failed to break down timestamp to tm structure");
126 strf_ret
= strftime(str
, len
, "%Y%m%d-%H%M%S", tm_result
);
129 ERR("Failed to format timestamp as local time");
136 std::string
lttng::utils::time_to_iso8601_str(std::time_t time
)
138 std::string
iso8601_str(ISO8601_STR_LEN
, '\0');
139 const auto ret
= ::time_to_iso8601_str(time
, &iso8601_str
[0], iso8601_str
.capacity());
142 LTTNG_THROW_ERROR("Failed to format time to iso8601 format");
145 /* Don't include '\0' in the C++ string. */
146 iso8601_str
.resize(iso8601_str
.size() - 1);
This page took 0.031083 seconds and 4 git commands to generate.