sessiond: notification: use lttng_payload for communications
[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 <stddef.h>
13 #include <stdint.h>
14 #include <limits.h>
15 #include <errno.h>
16 #include <pthread.h>
17 #include <locale.h>
18 #include <string.h>
19
20 static bool utf8_output_supported;
21
22 LTTNG_HIDDEN
23 bool locale_supports_utf8(void)
24 {
25 return utf8_output_supported;
26 }
27
28 LTTNG_HIDDEN
29 int timespec_to_ms(struct timespec ts, unsigned long *ms)
30 {
31 unsigned long res, remain_ms;
32
33 if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) {
34 errno = EOVERFLOW;
35 return -1; /* multiplication overflow */
36 }
37 res = ts.tv_sec * MSEC_PER_SEC;
38 remain_ms = ULONG_MAX - res;
39 if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) {
40 errno = EOVERFLOW;
41 return -1; /* addition overflow */
42 }
43 res += ts.tv_nsec / NSEC_PER_MSEC;
44 *ms = res;
45 return 0;
46 }
47
48 LTTNG_HIDDEN
49 struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2)
50 {
51 uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC +
52 (uint64_t) t1.tv_nsec;
53 uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC +
54 (uint64_t) t2.tv_nsec;
55 uint64_t diff = max(ts1, ts2) - min(ts1, ts2);
56 struct timespec res;
57
58 res.tv_sec = diff / (uint64_t) NSEC_PER_SEC;
59 res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC;
60 return res;
61 }
62
63 static
64 void __attribute__((constructor)) init_locale_utf8_support(void)
65 {
66 const char *program_locale = setlocale(LC_ALL, NULL);
67 const char *lang = getenv("LANG");
68
69 if (program_locale && strstr(program_locale, "utf8")) {
70 utf8_output_supported = true;
71 } else if (lang && strstr(lang, "utf8")) {
72 utf8_output_supported = true;
73 }
74 }
75
76 LTTNG_HIDDEN
77 int time_to_iso8601_str(time_t time, char *str, size_t len)
78 {
79 int ret = 0;
80 struct tm *tm_result;
81 struct tm tm_storage;
82 size_t strf_ret;
83
84 if (len < ISO8601_STR_LEN) {
85 ERR("Buffer too short to format ISO 8601 timestamp: %zu bytes provided when at least %zu are needed",
86 len, ISO8601_STR_LEN);
87 ret = -1;
88 goto end;
89 }
90
91 tm_result = localtime_r(&time, &tm_storage);
92 if (!tm_result) {
93 ret = -1;
94 PERROR("Failed to break down timestamp to tm structure");
95 goto end;
96 }
97
98 strf_ret = strftime(str, len, "%Y%m%dT%H%M%S%z", tm_result);
99 if (strf_ret == 0) {
100 ret = -1;
101 ERR("Failed to format timestamp as local time");
102 goto end;
103 }
104 end:
105 return ret;
106 }
107
108 LTTNG_HIDDEN
109 int time_to_datetime_str(time_t time, char *str, size_t len)
110 {
111 int ret = 0;
112 struct tm *tm_result;
113 struct tm tm_storage;
114 size_t strf_ret;
115
116 if (len < DATETIME_STR_LEN) {
117 ERR("Buffer too short to format to datetime: %zu bytes provided when at least %zu are needed",
118 len, DATETIME_STR_LEN);
119 ret = -1;
120 goto end;
121 }
122
123 tm_result = localtime_r(&time, &tm_storage);
124 if (!tm_result) {
125 ret = -1;
126 PERROR("Failed to break down timestamp to tm structure");
127 goto end;
128 }
129
130 strf_ret = strftime(str, len, "%Y%m%d-%H%M%S", tm_result);
131 if (strf_ret == 0) {
132 ret = -1;
133 ERR("Failed to format timestamp as local time");
134 goto end;
135 }
136 end:
137 return ret;
138 }
This page took 0.033242 seconds and 4 git commands to generate.