Commit | Line | Data |
---|---|---|
b74bc1d6 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> | |
19 | #include <common/macros.h> | |
b97f43a7 | 20 | #include <common/error.h> |
b74bc1d6 JG |
21 | #include <stddef.h> |
22 | #include <stdint.h> | |
23 | #include <limits.h> | |
24 | #include <errno.h> | |
25 | #include <pthread.h> | |
10945cfa 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 | } | |
b74bc1d6 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 | } | |
10945cfa JG |
71 | |
72 | static | |
73 | void __attribute__((constructor)) init_locale_utf8_support(void) | |
74 | { | |
75 | const char *program_locale = setlocale(LC_ALL, NULL); | |
76 | const char *lang = getenv("LANG"); | |
77 | ||
78 | if (program_locale && strstr(program_locale, "utf8")) { | |
79 | utf8_output_supported = true; | |
2b8ce248 | 80 | } else if (lang && strstr(lang, "utf8")) { |
10945cfa JG |
81 | utf8_output_supported = true; |
82 | } | |
83 | } | |
b97f43a7 JG |
84 | |
85 | LTTNG_HIDDEN | |
86 | int time_to_iso8601_str(time_t time, char *str, size_t len) | |
87 | { | |
88 | int ret = 0; | |
89 | struct tm *tm_result; | |
90 | struct tm tm_storage; | |
91 | size_t strf_ret; | |
92 | ||
93 | if (len < ISO8601_STR_LEN) { | |
94 | ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed", | |
95 | len, ISO8601_STR_LEN); | |
96 | ret = -1; | |
97 | goto end; | |
98 | } | |
99 | ||
100 | tm_result = localtime_r(&time, &tm_storage); | |
101 | if (!tm_result) { | |
102 | ret = -1; | |
103 | PERROR("Failed to break down timestamp to tm structure"); | |
104 | goto end; | |
105 | } | |
106 | ||
107 | strf_ret = strftime(str, len, "%Y%m%dT%H%M%S%z", tm_result); | |
108 | if (strf_ret == 0) { | |
109 | ret = -1; | |
110 | ERR("Failed to format timestamp as local time"); | |
111 | goto end; | |
112 | } | |
113 | end: | |
114 | return ret; | |
115 | } |