summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <thomas.voss@humanwave.nl> 2025-09-11 10:53:17 +0200
committerThomas Voss <thomas.voss@humanwave.nl> 2025-09-11 10:53:17 +0200
commit3449603d2ba363f1e86cd9c783dfb5a8aeac7a48 (patch)
treefcba677363b0a1661f6e00cbb47710fd997df926
parentfffb8f9dc2802401645a14823702abd6d9df679a (diff)
Support multiple timezones in one clock
-rw-r--r--main.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/main.c b/main.c
index 8c23394..b7c7b0d 100644
--- a/main.c
+++ b/main.c
@@ -89,18 +89,31 @@ main(int argc, char **argv)
: interval == 'h' ? synch
: /* default */ syncs;
+ const char *tzorig = getenv("TZ");
+
for (;;) {
struct timespec now, then;
if (clock_gettime(CLOCK_REALTIME, &now) == -1)
warn(_("failed to get the time"));
- struct tm *tm = localtime(&now.tv_sec);
+
+ if (tzorig != nullptr) {
+ if (setenv("TZ", tzorig, true) == -1)
+ warn(_("failed to set the timezone"));
+ } else if (unsetenv("TZ") == -1)
+ warn(_("failed to set the timezone"));
for (int i = 0; i < argc; i++) {
- char buf[256]; /* TODO: Make dynamic */
- size_t n = strftime(buf, sizeof(buf), argv[i], tm);
- if (n == 0)
- warnx(_("buffer too small"));
- fputs(buf, stdout);
+ if (strlen(argv[i]) >= 3 && memcmp("TZ=", argv[i], 3) == 0) {
+ if (putenv(argv[i]) == -1)
+ warn(_("failed to set the timezone"));
+ } else {
+ char buf[256]; /* TODO: Make dynamic */
+ struct tm *tm = localtime(&now.tv_sec);
+ size_t n = strftime(buf, sizeof(buf), argv[i], tm);
+ if (n == 0)
+ warnx(_("buffer too small"));
+ fputs(buf, stdout);
+ }
}
putchar('\n');