diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-03-30 17:27:52 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-03-30 17:27:52 -0400 |
| commit | 8cba6c3c24c2122c843aa0feaa26fd6e5c2412e2 (patch) | |
| tree | ae0583da790de2416f03a27e9f94953be7d4a803 /examples/time/time_defs.h | |
| parent | d853d7b8dc1586f6b2fad3bf05998bfbe935b8f3 (diff) | |
Deprecate built-in Moment datatype in favor of a `time` module
Diffstat (limited to 'examples/time/time_defs.h')
| -rw-r--r-- | examples/time/time_defs.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/examples/time/time_defs.h b/examples/time/time_defs.h new file mode 100644 index 00000000..fd8fd4f3 --- /dev/null +++ b/examples/time/time_defs.h @@ -0,0 +1,30 @@ +#pragma once +#include <time.h> +#include <sys/time.h> + +typedef struct timeval Time; + +static OptionalText_t _local_timezone = NONE_TEXT; + +static INLINE Text_t num_format(long n, const char *unit) +{ + if (n == 0) + return Text("now"); + return Text$format((n == 1 || n == -1) ? "%ld %s %s" : "%ld %ss %s", n < 0 ? -n : n, unit, n < 0 ? "ago" : "later"); +} + +static void set_local_timezone(Text_t tz) +{ + setenv("TZ", Text$as_c_string(tz), 1); + _local_timezone = tz; + tzset(); +} + +#define WITH_TIMEZONE(tz, body) ({ if (tz.length >= 0) { \ + OptionalText_t old_timezone = _local_timezone; \ + set_local_timezone(tz); \ + body; \ + set_local_timezone(old_timezone); \ + } else { \ + body; \ + }}) |
