2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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.
18 #include <common/time.h>
19 #include <common/error.h>
20 #include <common/macros.h>
29 static bool utf8_output_supported
;
32 bool locale_supports_utf8(void)
34 return utf8_output_supported
;
38 int timespec_to_ms(struct timespec ts
, unsigned long *ms
)
40 unsigned long res
, remain_ms
;
42 if (ts
.tv_sec
> ULONG_MAX
/ MSEC_PER_SEC
) {
44 return -1; /* multiplication overflow */
46 res
= ts
.tv_sec
* MSEC_PER_SEC
;
47 remain_ms
= ULONG_MAX
- res
;
48 if (ts
.tv_nsec
/ NSEC_PER_MSEC
> remain_ms
) {
50 return -1; /* addition overflow */
52 res
+= ts
.tv_nsec
/ NSEC_PER_MSEC
;
58 struct timespec
timespec_abs_diff(struct timespec t1
, struct timespec t2
)
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
);
67 res
.tv_sec
= diff
/ (uint64_t) NSEC_PER_SEC
;
68 res
.tv_nsec
= diff
% (uint64_t) NSEC_PER_SEC
;
73 int time_t_to_ISO8601(char *dest
, size_t dest_size
, time_t time
)
76 struct tm tm
, *timeinfo
;
78 if (dest_size
< ISO8601_LEN
) {
79 ERR("Failed to format time to ISO8601 destination too small");
84 timeinfo
= localtime_r(&time
, &tm
);
91 ret
= strftime(dest
, dest_size
, ISO8601_FORMAT
, timeinfo
);
93 ERR("Failed to format time to ISO8601");
104 void __attribute__((constructor
)) init_locale_utf8_support(void)
106 const char *program_locale
= setlocale(LC_ALL
, NULL
);
107 const char *lang
= getenv("LANG");
109 if (program_locale
&& strstr(program_locale
, "utf8")) {
110 utf8_output_supported
= true;
111 } else if (lang
&& strstr(lang
, "utf8")) {
112 utf8_output_supported
= true;
This page took 0.03208 seconds and 5 git commands to generate.