vscode: Add configurations to run the executables under the debugger
[lttng-tools.git] / src / common / time.c
1 /*
2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <common/time.h>
9 #include <common/error.h>
10 #include <common/macros.h>
11 #include <common/error.h>
12 #include <common/compat/errno.h>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <limits.h>
16 #include <pthread.h>
17 #include <locale.h>
18 #include <string.h>
19
20 static bool utf8_output_supported;
21
22 bool locale_supports_utf8(void)
23 {
24 return utf8_output_supported;
25 }
26
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
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 }
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;
68 } else if (lang && strstr(lang, "utf8")) {
69 utf8_output_supported = true;
70 }
71 }
72
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
87 tm_result = localtime_r(&time, &tm_storage);
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 }
103
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 }
This page took 0.030785 seconds and 4 git commands to generate.