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> | |
6e78b10e | 19 | #include <common/error.h> |
b74bc1d6 | 20 | #include <common/macros.h> |
b97f43a7 | 21 | #include <common/error.h> |
b74bc1d6 JG |
22 | #include <stddef.h> |
23 | #include <stdint.h> | |
24 | #include <limits.h> | |
25 | #include <errno.h> | |
26 | #include <pthread.h> | |
10945cfa JG |
27 | #include <locale.h> |
28 | #include <string.h> | |
29 | ||
30 | static bool utf8_output_supported; | |
31 | ||
32 | LTTNG_HIDDEN | |
33 | bool locale_supports_utf8(void) | |
34 | { | |
35 | return utf8_output_supported; | |
36 | } | |
b74bc1d6 JG |
37 | |
38 | LTTNG_HIDDEN | |
39 | int timespec_to_ms(struct timespec ts, unsigned long *ms) | |
40 | { | |
41 | unsigned long res, remain_ms; | |
42 | ||
43 | if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) { | |
44 | errno = EOVERFLOW; | |
45 | return -1; /* multiplication overflow */ | |
46 | } | |
47 | res = ts.tv_sec * MSEC_PER_SEC; | |
48 | remain_ms = ULONG_MAX - res; | |
49 | if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) { | |
50 | errno = EOVERFLOW; | |
51 | return -1; /* addition overflow */ | |
52 | } | |
53 | res += ts.tv_nsec / NSEC_PER_MSEC; | |
54 | *ms = res; | |
55 | return 0; | |
56 | } | |
57 | ||
58 | LTTNG_HIDDEN | |
59 | struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) | |
60 | { | |
61 | uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + | |
62 | (uint64_t) t1.tv_nsec; | |
63 | uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + | |
64 | (uint64_t) t2.tv_nsec; | |
65 | uint64_t diff = max(ts1, ts2) - min(ts1, ts2); | |
66 | struct timespec res; | |
67 | ||
68 | res.tv_sec = diff / (uint64_t) NSEC_PER_SEC; | |
69 | res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC; | |
70 | return res; | |
71 | } | |
10945cfa JG |
72 | |
73 | static | |
74 | void __attribute__((constructor)) init_locale_utf8_support(void) | |
75 | { | |
76 | const char *program_locale = setlocale(LC_ALL, NULL); | |
77 | const char *lang = getenv("LANG"); | |
78 | ||
79 | if (program_locale && strstr(program_locale, "utf8")) { | |
80 | utf8_output_supported = true; | |
2b8ce248 | 81 | } else if (lang && strstr(lang, "utf8")) { |
10945cfa JG |
82 | utf8_output_supported = true; |
83 | } | |
84 | } | |
b97f43a7 JG |
85 | |
86 | LTTNG_HIDDEN | |
87 | int time_to_iso8601_str(time_t time, char *str, size_t len) | |
88 | { | |
89 | int ret = 0; | |
90 | struct tm *tm_result; | |
91 | struct tm tm_storage; | |
92 | size_t strf_ret; | |
93 | ||
94 | if (len < ISO8601_STR_LEN) { | |
95 | ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed", | |
96 | len, ISO8601_STR_LEN); | |
97 | ret = -1; | |
98 | goto end; | |
99 | } | |
100 | ||
101 | tm_result = localtime_r(&time, &tm_storage); | |
102 | if (!tm_result) { | |
103 | ret = -1; | |
104 | PERROR("Failed to break down timestamp to tm structure"); | |
105 | goto end; | |
106 | } | |
107 | ||
108 | strf_ret = strftime(str, len, "%Y%m%dT%H%M%S%z", tm_result); | |
109 | if (strf_ret == 0) { | |
110 | ret = -1; | |
111 | ERR("Failed to format timestamp as local time"); | |
112 | goto end; | |
113 | } | |
114 | end: | |
115 | return ret; | |
116 | } |