Commit | Line | Data |
---|---|---|
507f5694 JG |
1 | /* |
2 | * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #include <common/time.h> | |
d77fb8c7 | 19 | #include <common/error.h> |
507f5694 JG |
20 | #include <common/macros.h> |
21 | #include <stddef.h> | |
22 | #include <stdint.h> | |
23 | #include <limits.h> | |
24 | #include <errno.h> | |
25 | #include <pthread.h> | |
2a1135fa JG |
26 | #include <locale.h> |
27 | #include <string.h> | |
28 | ||
29 | static bool utf8_output_supported; | |
30 | ||
31 | LTTNG_HIDDEN | |
32 | bool locale_supports_utf8(void) | |
33 | { | |
34 | return utf8_output_supported; | |
35 | } | |
507f5694 JG |
36 | |
37 | LTTNG_HIDDEN | |
38 | int timespec_to_ms(struct timespec ts, unsigned long *ms) | |
39 | { | |
40 | unsigned long res, remain_ms; | |
41 | ||
42 | if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) { | |
43 | errno = EOVERFLOW; | |
44 | return -1; /* multiplication overflow */ | |
45 | } | |
46 | res = ts.tv_sec * MSEC_PER_SEC; | |
47 | remain_ms = ULONG_MAX - res; | |
48 | if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) { | |
49 | errno = EOVERFLOW; | |
50 | return -1; /* addition overflow */ | |
51 | } | |
52 | res += ts.tv_nsec / NSEC_PER_MSEC; | |
53 | *ms = res; | |
54 | return 0; | |
55 | } | |
56 | ||
57 | LTTNG_HIDDEN | |
58 | struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) | |
59 | { | |
60 | uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + | |
61 | (uint64_t) t1.tv_nsec; | |
62 | uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + | |
63 | (uint64_t) t2.tv_nsec; | |
64 | uint64_t diff = max(ts1, ts2) - min(ts1, ts2); | |
65 | struct timespec res; | |
66 | ||
67 | res.tv_sec = diff / (uint64_t) NSEC_PER_SEC; | |
68 | res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC; | |
69 | return res; | |
70 | } | |
2a1135fa | 71 | |
d77fb8c7 JR |
72 | LTTNG_HIDDEN |
73 | int time_t_to_ISO8601(char *dest, size_t dest_size, time_t time) | |
74 | { | |
75 | int ret; | |
76 | struct tm tm, *timeinfo; | |
77 | ||
78 | if (dest_size < ISO8601_LEN) { | |
79 | ERR("Failed to format time to ISO8601 destination too small"); | |
80 | ret = -1; | |
81 | goto end; | |
82 | } | |
83 | ||
84 | timeinfo = localtime_r(&time, &tm); | |
85 | if (!timeinfo) { | |
86 | PERROR("localtime"); | |
87 | ret = -1; | |
88 | goto end; | |
89 | } | |
90 | ||
91 | ret = strftime(dest, dest_size, ISO8601_FORMAT, timeinfo); | |
92 | if (ret == 0) { | |
93 | ERR("Failed to format time to ISO8601"); | |
94 | ret = -1; | |
95 | goto end; | |
96 | } | |
97 | ||
98 | ret = 0; | |
99 | end: | |
100 | return ret; | |
101 | } | |
102 | ||
2a1135fa JG |
103 | static |
104 | void __attribute__((constructor)) init_locale_utf8_support(void) | |
105 | { | |
106 | const char *program_locale = setlocale(LC_ALL, NULL); | |
107 | const char *lang = getenv("LANG"); | |
108 | ||
109 | if (program_locale && strstr(program_locale, "utf8")) { | |
110 | utf8_output_supported = true; | |
0731b11e | 111 | } else if (lang && strstr(lang, "utf8")) { |
2a1135fa JG |
112 | utf8_output_supported = true; |
113 | } | |
114 | } |