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> | |
19 | ||
20 | static bool utf8_output_supported; | |
21 | ||
2a1135fa JG |
22 | bool locale_supports_utf8(void) |
23 | { | |
24 | return utf8_output_supported; | |
25 | } | |
507f5694 | 26 | |
507f5694 JG |
27 | int timespec_to_ms(struct timespec ts, unsigned long *ms) |
28 | { | |
29 | unsigned long res, remain_ms; | |
30 | ||
31 | if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) { | |
32 | errno = EOVERFLOW; | |
33 | return -1; /* multiplication overflow */ | |
34 | } | |
35 | res = ts.tv_sec * MSEC_PER_SEC; | |
36 | remain_ms = ULONG_MAX - res; | |
37 | if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) { | |
38 | errno = EOVERFLOW; | |
39 | return -1; /* addition overflow */ | |
40 | } | |
41 | res += ts.tv_nsec / NSEC_PER_MSEC; | |
42 | *ms = res; | |
43 | return 0; | |
44 | } | |
45 | ||
507f5694 JG |
46 | struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) |
47 | { | |
48 | uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + | |
49 | (uint64_t) t1.tv_nsec; | |
50 | uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + | |
51 | (uint64_t) t2.tv_nsec; | |
52 | uint64_t diff = max(ts1, ts2) - 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 | } | |
2a1135fa JG |
59 | |
60 | static | |
61 | void __attribute__((constructor)) init_locale_utf8_support(void) | |
62 | { | |
63 | const char *program_locale = setlocale(LC_ALL, NULL); | |
64 | const char *lang = getenv("LANG"); | |
65 | ||
66 | if (program_locale && strstr(program_locale, "utf8")) { | |
67 | utf8_output_supported = true; | |
0731b11e | 68 | } else if (lang && strstr(lang, "utf8")) { |
2a1135fa JG |
69 | utf8_output_supported = true; |
70 | } | |
71 | } | |
274ca939 | 72 | |
274ca939 JG |
73 | int time_to_iso8601_str(time_t time, char *str, size_t len) |
74 | { | |
75 | int ret = 0; | |
76 | struct tm *tm_result; | |
77 | struct tm tm_storage; | |
78 | size_t strf_ret; | |
79 | ||
80 | if (len < ISO8601_STR_LEN) { | |
81 | ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed", | |
82 | len, ISO8601_STR_LEN); | |
83 | ret = -1; | |
84 | goto end; | |
85 | } | |
86 | ||
5e5af4be | 87 | tm_result = localtime_r(&time, &tm_storage); |
274ca939 JG |
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 | } | |
100 | end: | |
101 | return ret; | |
102 | } | |
a8b66566 | 103 | |
a8b66566 JR |
104 | int 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, DATETIME_STR_LEN); | |
114 | ret = -1; | |
115 | goto end; | |
116 | } | |
117 | ||
118 | tm_result = localtime_r(&time, &tm_storage); | |
119 | if (!tm_result) { | |
120 | ret = -1; | |
121 | PERROR("Failed to break down timestamp to tm structure"); | |
122 | goto end; | |
123 | } | |
124 | ||
125 | strf_ret = strftime(str, len, "%Y%m%d-%H%M%S", tm_result); | |
126 | if (strf_ret == 0) { | |
127 | ret = -1; | |
128 | ERR("Failed to format timestamp as local time"); | |
129 | goto end; | |
130 | } | |
131 | end: | |
132 | return ret; | |
133 | } |