Commit | Line | Data |
---|---|---|
507f5694 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
507f5694 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
507f5694 | 5 | * |
507f5694 JG |
6 | */ |
7 | ||
8 | #include <common/time.h> | |
8de88061 | 9 | #include <common/error.h> |
507f5694 | 10 | #include <common/macros.h> |
274ca939 | 11 | #include <common/error.h> |
edf4b93e | 12 | #include <common/compat/errno.h> |
507f5694 JG |
13 | #include <stddef.h> |
14 | #include <stdint.h> | |
15 | #include <limits.h> | |
507f5694 | 16 | #include <pthread.h> |
2a1135fa JG |
17 | #include <locale.h> |
18 | #include <string.h> | |
a6bc4ca9 | 19 | #include <algorithm> |
2a1135fa JG |
20 | |
21 | static bool utf8_output_supported; | |
22 | ||
2a1135fa JG |
23 | bool locale_supports_utf8(void) |
24 | { | |
25 | return utf8_output_supported; | |
26 | } | |
507f5694 | 27 | |
507f5694 JG |
28 | int timespec_to_ms(struct timespec ts, unsigned long *ms) |
29 | { | |
30 | unsigned long res, remain_ms; | |
31 | ||
32 | if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) { | |
33 | errno = EOVERFLOW; | |
34 | return -1; /* multiplication overflow */ | |
35 | } | |
36 | res = ts.tv_sec * MSEC_PER_SEC; | |
37 | remain_ms = ULONG_MAX - res; | |
38 | if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) { | |
39 | errno = EOVERFLOW; | |
40 | return -1; /* addition overflow */ | |
41 | } | |
42 | res += ts.tv_nsec / NSEC_PER_MSEC; | |
43 | *ms = res; | |
44 | return 0; | |
45 | } | |
46 | ||
507f5694 JG |
47 | struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) |
48 | { | |
49 | uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + | |
50 | (uint64_t) t1.tv_nsec; | |
51 | uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + | |
52 | (uint64_t) t2.tv_nsec; | |
a6bc4ca9 | 53 | uint64_t diff = std::max(ts1, ts2) - std::min(ts1, ts2); |
507f5694 JG |
54 | struct timespec res; |
55 | ||
56 | res.tv_sec = diff / (uint64_t) NSEC_PER_SEC; | |
57 | res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC; | |
58 | return res; | |
59 | } | |
2a1135fa JG |
60 | |
61 | static | |
62 | void __attribute__((constructor)) init_locale_utf8_support(void) | |
63 | { | |
64 | const char *program_locale = setlocale(LC_ALL, NULL); | |
65 | const char *lang = getenv("LANG"); | |
66 | ||
67 | if (program_locale && strstr(program_locale, "utf8")) { | |
68 | utf8_output_supported = true; | |
0731b11e | 69 | } else if (lang && strstr(lang, "utf8")) { |
2a1135fa JG |
70 | utf8_output_supported = true; |
71 | } | |
72 | } | |
274ca939 | 73 | |
274ca939 JG |
74 | int time_to_iso8601_str(time_t time, char *str, size_t len) |
75 | { | |
76 | int ret = 0; | |
77 | struct tm *tm_result; | |
78 | struct tm tm_storage; | |
79 | size_t strf_ret; | |
80 | ||
81 | if (len < ISO8601_STR_LEN) { | |
82 | ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed", | |
83 | len, ISO8601_STR_LEN); | |
84 | ret = -1; | |
85 | goto end; | |
86 | } | |
87 | ||
5e5af4be | 88 | tm_result = localtime_r(&time, &tm_storage); |
274ca939 JG |
89 | if (!tm_result) { |
90 | ret = -1; | |
91 | PERROR("Failed to break down timestamp to tm structure"); | |
92 | goto end; | |
93 | } | |
94 | ||
95 | strf_ret = strftime(str, len, "%Y%m%dT%H%M%S%z", tm_result); | |
96 | if (strf_ret == 0) { | |
97 | ret = -1; | |
98 | ERR("Failed to format timestamp as local time"); | |
99 | goto end; | |
100 | } | |
101 | end: | |
102 | return ret; | |
103 | } | |
a8b66566 | 104 | |
a8b66566 JR |
105 | int time_to_datetime_str(time_t time, char *str, size_t len) |
106 | { | |
107 | int ret = 0; | |
108 | struct tm *tm_result; | |
109 | struct tm tm_storage; | |
110 | size_t strf_ret; | |
111 | ||
112 | if (len < DATETIME_STR_LEN) { | |
113 | ERR("Buffer too short to format to datetime: %zu bytes provided when at least %zu are needed", | |
114 | len, 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 | } | |
132 | end: | |
133 | return ret; | |
134 | } |