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(void)
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
+
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
);
57 res
.tv_sec
= diff
/ (uint64_t) NSEC_PER_SEC
;
58 res
.tv_nsec
= diff
% (uint64_t) NSEC_PER_SEC
;
63 void __attribute__((constructor
)) init_locale_utf8_support(void)
65 const char *program_locale
= setlocale(LC_ALL
, NULL
);
66 const char *lang
= getenv("LANG");
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;
75 int time_to_iso8601_str(time_t time
, char *str
, size_t len
)
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
);
89 tm_result
= localtime_r(&time
, &tm_storage
);
92 PERROR("Failed to break down timestamp to tm structure");
96 strf_ret
= strftime(str
, len
, "%Y%m%dT%H%M%S%z", tm_result
);
99 ERR("Failed to format timestamp as local time");
106 int time_to_datetime_str(time_t time
, char *str
, size_t len
)
109 struct tm
*tm_result
;
110 struct tm tm_storage
;
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
);
120 tm_result
= localtime_r(&time
, &tm_storage
);
123 PERROR("Failed to break down timestamp to tm structure");
127 strf_ret
= strftime(str
, len
, "%Y%m%d-%H%M%S", tm_result
);
130 ERR("Failed to format timestamp as local time");
137 std::string
lttng::utils::time_to_iso8601_str(std::time_t time
)
139 std::string
iso8601_str(ISO8601_STR_LEN
, '\0');
140 const auto ret
= ::time_to_iso8601_str(time
, &iso8601_str
[0], iso8601_str
.capacity());
143 LTTNG_THROW_ERROR("Failed to format time to iso8601 format");
146 /* Don't include '\0' in the C++ string. */
147 iso8601_str
.resize(iso8601_str
.size() - 1);
This page took 0.033182 seconds and 4 git commands to generate.