Rename lttng_event_rule_userspace_probe to lttng_event_rule_kernel_uprobe
[lttng-tools.git] / src / common / time.c
CommitLineData
507f5694 1/*
ab5be9fa 2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
507f5694 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
507f5694 5 *
507f5694
JG
6 */
7
8#include <common/time.h>
8de88061 9#include <common/error.h>
507f5694 10#include <common/macros.h>
274ca939 11#include <common/error.h>
edf4b93e 12#include <common/compat/errno.h>
507f5694
JG
13#include <stddef.h>
14#include <stdint.h>
15#include <limits.h>
507f5694 16#include <pthread.h>
2a1135fa
JG
17#include <locale.h>
18#include <string.h>
19
20static bool utf8_output_supported;
21
22LTTNG_HIDDEN
23bool locale_supports_utf8(void)
24{
25 return utf8_output_supported;
26}
507f5694
JG
27
28LTTNG_HIDDEN
29int 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
48LTTNG_HIDDEN
49struct 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}
2a1135fa
JG
62
63static
64void __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;
0731b11e 71 } else if (lang && strstr(lang, "utf8")) {
2a1135fa
JG
72 utf8_output_supported = true;
73 }
74}
274ca939
JG
75
76LTTNG_HIDDEN
77int 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
5e5af4be 91 tm_result = localtime_r(&time, &tm_storage);
274ca939
JG
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 }
104end:
105 return ret;
106}
a8b66566
JR
107
108LTTNG_HIDDEN
109int 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 }
136end:
137 return ret;
138}
This page took 0.039709 seconds and 4 git commands to generate.