code / tomo-time

Lines215 Tomo183 C21 Markdown11
(26 lines)
1 // Some helper logic for working with times.
3 #pragma once
4 #include <stdlib.h>
5 #include <sys/time.h>
6 #include <time.h>
8 static OptionalText_t _local_timezone = NONE_TEXT;
10 static void set_local_timezone(Text_t tz) {
11 setenv("TZ", Text$as_c_string(tz), 1);
12 _local_timezone = tz;
13 tzset();
16 #define WITH_TIMEZONE(tz, body) \
17 ({ \
18 if (tz.length >= 0) { \
19 OptionalText_t old_timezone = _local_timezone; \
20 set_local_timezone(tz); \
21 body; \
22 set_local_timezone(old_timezone); \
23 } else { \
24 body; \
25 } \
26 })