From 12e9e519672850b520c8724f78097ead0eeab8b2 Mon Sep 17 00:00:00 2001 From: hanez Date: Mon, 20 Jul 2026 03:56:25 +0200 Subject: [PATCH] fix !time: fall back to built-in timezone table when system lacks tzdata --- CHANGELOG.md | 5 ++ zynk.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 167 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9268e40..ef68bc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +2026.07.20 – Fix !time to work without tzdata. + +- Rewrote `!time` to try the system `localtime_r()` first, then fall back to a built-in timezone table with 45 IANA zones and simplified DST rules when the system can't resolve timezones (e.g. no `tzdata` installed). This means `!time Australia/Sydney` now shows correct AEST/AEDT even on minimal systems. +- Version bumped to 0.35.5. + 2026.07.20 – Add pi() to !calc. - Added `pi()` function support to `!calc`: bc -l does not define `pi()` on all systems, so `cmd_calc` now prepends a `pi()` definition before evaluating expressions. `!calc pi()`, `!calc pi()*2`, `!calc pi()^2` etc. all work. diff --git a/zynk.c b/zynk.c index ea34bb3..9c96e9a 100644 --- a/zynk.c +++ b/zynk.c @@ -29,7 +29,7 @@ int asprintf(char **, const char *, ...); /* ---- constants ---- */ -#define VERSION "0.35.3" +#define VERSION "0.35.5" #define DEFAULT_SERVER "irc.libera.chat" #define OPENCODE_BIN "/usr/bin/opencode" #define CLR_RESET "\033[0m" @@ -2617,6 +2617,130 @@ static int cmd_calc(Session *s, const char *msg, const char *reply_target, const return 1; } +typedef struct { const char *name; int off; int dst_off; int dst_north; } TzEntry; + +static const TzEntry tz_table[] = { + {"UTC", 0, 0, 0}, + {"GMT", 0, 0, 0}, + {"EST", -5*3600, -5*3600, 0}, + {"CST", -6*3600, -6*3600, 0}, + {"MST", -7*3600, -7*3600, 0}, + {"PST", -8*3600, -8*3600, 0}, + {"HST", -10*3600, -10*3600, 0}, + {"US/Eastern", -5*3600, -4*3600, 1}, + {"US/Central", -6*3600, -5*3600, 1}, + {"US/Mountain", -7*3600, -6*3600, 1}, + {"US/Pacific", -8*3600, -7*3600, 1}, + {"US/Alaska", -9*3600, -8*3600, 1}, + {"US/Honolulu", -10*3600, -10*3600, 0}, + {"Europe/London", 0, 1*3600, 1}, + {"Europe/Berlin", 1*3600, 2*3600, 1}, + {"Europe/Paris", 1*3600, 2*3600, 1}, + {"Europe/Moscow", 3*3600, 3*3600, 0}, + {"Europe/Istanbul", 3*3600, 3*3600, 0}, + {"Asia/Tokyo", 9*3600, 9*3600, 0}, + {"Asia/Shanghai", 8*3600, 8*3600, 0}, + {"Asia/Kolkata", 19800, 19800, 0}, + {"Asia/Dubai", 4*3600, 4*3600, 0}, + {"Asia/Singapore", 8*3600, 8*3600, 0}, + {"Asia/Seoul", 9*3600, 9*3600, 0}, + {"Asia/Hong_Kong", 8*3600, 8*3600, 0}, + {"Asia/Taipei", 8*3600, 8*3600, 0}, + {"Australia/Sydney", 10*3600, 11*3600, 2}, + {"Australia/Melbourne", 10*3600, 11*3600, 2}, + {"Australia/Brisbane", 10*3600, 10*3600, 0}, + {"Australia/Perth", 8*3600, 8*3600, 0}, + {"Australia/Adelaide", 930*60, 1030*60, 2}, + {"Australia/Darwin", 930*60, 930*60, 0}, + {"Pacific/Auckland", 12*3600, 13*3600, 2}, + {"Pacific/Honolulu", -10*3600, -10*3600, 0}, + {"Pacific/Fiji", 12*3600, 13*3600, 2}, + {"Canada/Toronto", -5*3600, -4*3600, 1}, + {"Canada/Vancouver", -8*3600, -7*3600, 1}, + {"America/Sao_Paulo", -3*3600, -2*3600, 2}, + {"America/Mexico_City", -6*3600, -5*3600, 1}, + {"America/Argentina/Buenos_Aires", -3*3600, -3*3600, 0}, + {"Africa/Cairo", 2*3600, 2*3600, 0}, + {"Africa/Johannesburg", 2*3600, 2*3600, 0}, + {"Africa/Lagos", 1*3600, 1*3600, 0}, + {"Africa/Nairobi", 3*3600, 3*3600, 0}, + {"Atlantic/Reykjavik", 0, 0, 0}, +}; + +static const TzEntry *tz_lookup(const char *name) { + for (size_t i = 0; i < sizeof tz_table / sizeof tz_table[0]; i++) + if (strcasecmp(tz_table[i].name, name) == 0) return &tz_table[i]; + return NULL; +} + +static void tz_format(time_t epoch, int offset, const char *tzname, int is_dst, char *buf, size_t buflen) { + time_t local = epoch + offset; + struct tm tm; + gmtime_r(&local, &tm); + const char *abbr = tzname; + if (strcmp(tzname, "US/Eastern") == 0 || strcmp(tzname, "America/Toronto") == 0 || strcmp(tzname, "Canada/Toronto") == 0) + abbr = is_dst ? "EDT" : "EST"; + else if (strcmp(tzname, "US/Central") == 0) + abbr = is_dst ? "CDT" : "CST"; + else if (strcmp(tzname, "US/Mountain") == 0) + abbr = is_dst ? "MDT" : "MST"; + else if (strcmp(tzname, "US/Pacific") == 0 || strcmp(tzname, "America/Vancouver") == 0 || strcmp(tzname, "Canada/Vancouver") == 0) + abbr = is_dst ? "PDT" : "PST"; + else if (strcmp(tzname, "US/Alaska") == 0) + abbr = is_dst ? "AKDT" : "AKST"; + else if (strcmp(tzname, "Europe/London") == 0 || strcmp(tzname, "Atlantic/Reykjavik") == 0) + abbr = is_dst ? "BST" : "GMT"; + else if (strcmp(tzname, "Europe/Berlin") == 0 || strcmp(tzname, "Europe/Paris") == 0) + abbr = is_dst ? "CEST" : "CET"; + else if (strcmp(tzname, "Europe/Moscow") == 0 || strcmp(tzname, "Europe/Istanbul") == 0) + abbr = "MSK"; + else if (strcmp(tzname, "Asia/Tokyo") == 0) + abbr = "JST"; + else if (strcmp(tzname, "Asia/Shanghai") == 0 || strcmp(tzname, "Asia/Hong_Kong") == 0 || strcmp(tzname, "Asia/Taipei") == 0) + abbr = "CST"; + else if (strcmp(tzname, "Asia/Kolkata") == 0) + abbr = "IST"; + else if (strcmp(tzname, "Asia/Dubai") == 0) + abbr = "GST"; + else if (strcmp(tzname, "Asia/Singapore") == 0) + abbr = "SGT"; + else if (strcmp(tzname, "Asia/Seoul") == 0) + abbr = "KST"; + else if (strcmp(tzname, "Australia/Sydney") == 0 || strcmp(tzname, "Australia/Melbourne") == 0) + abbr = is_dst ? "AEDT" : "AEST"; + else if (strcmp(tzname, "Australia/Brisbane") == 0) + abbr = "AEST"; + else if (strcmp(tzname, "Australia/Perth") == 0) + abbr = "AWST"; + else if (strcmp(tzname, "Australia/Adelaide") == 0) + abbr = is_dst ? "ACDT" : "ACST"; + else if (strcmp(tzname, "Australia/Darwin") == 0) + abbr = "ACST"; + else if (strcmp(tzname, "Pacific/Auckland") == 0) + abbr = is_dst ? "NZDT" : "NZST"; + else if (strcmp(tzname, "Pacific/Fiji") == 0) + abbr = is_dst ? "FJDT" : "FJST"; + else if (strcmp(tzname, "America/Sao_Paulo") == 0) + abbr = is_dst ? "BRT" : "BRST"; + else if (strcmp(tzname, "America/Mexico_City") == 0) + abbr = is_dst ? "CDT" : "CST"; + else if (strcmp(tzname, "America/Argentina/Buenos_Aires") == 0) + abbr = "ART"; + else if (strcmp(tzname, "Africa/Cairo") == 0) + abbr = "EET"; + else if (strcmp(tzname, "Africa/Johannesburg") == 0) + abbr = "SAST"; + else if (strcmp(tzname, "Africa/Lagos") == 0) + abbr = "WAT"; + else if (strcmp(tzname, "Africa/Nairobi") == 0) + abbr = "EAT"; + else if (offset == 0) + abbr = "UTC"; + snprintf(buf, buflen, "%04d-%02d-%02d %02d:%02d:%02d %s", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec, abbr); +} + static int cmd_time(Session *s, const char *msg, const char *reply_target, const char *src_nick) { if (strncmp(msg, "!time", 5) != 0) return 0; if (msg[5] != ' ' && msg[5] != '\0') return 0; @@ -2630,21 +2754,44 @@ static int cmd_time(Session *s, const char *msg, const char *reply_target, const for (const char *p = rest; *p && tl < sizeof tzname - 1 && *p != ' '; p++) tzname[tl++] = *p; tzname[tl] = 0; - char cmd[192]; - snprintf(cmd, sizeof cmd, "TZ=%s date +\"%%Y-%%m-%%d %%H:%%M:%%S %%Z\" 2>/dev/null", tzname); - FILE *fp = popen(cmd, "r"); - char buf[64] = ""; - if (fp) { - if (fgets(buf, sizeof buf, fp)) { - size_t l = strlen(buf); - while (l > 0 && (buf[l-1] == '\n' || buf[l-1] == '\r')) buf[--l] = 0; - } - pclose(fp); - } - if (buf[0]) + time_t now = time(NULL); + struct tm utc_tm; + gmtime_r(&now, &utc_tm); + char *old_tz = getenv("TZ"); + char old_tz_buf[256] = ""; + if (old_tz) { strncpy(old_tz_buf, old_tz, sizeof old_tz_buf - 1); old_tz_buf[sizeof old_tz_buf - 1] = 0; } + setenv("TZ", tzname, 1); + tzset(); + struct tm local_tm; + localtime_r(&now, &local_tm); + if (old_tz_buf[0]) setenv("TZ", old_tz_buf, 1); else unsetenv("TZ"); + tzset(); + int sys_offset = (local_tm.tm_hour - utc_tm.tm_hour) * 3600 + (local_tm.tm_min - utc_tm.tm_min) * 60; + if (sys_offset > 12 * 3600) sys_offset -= 24 * 3600; + if (sys_offset < -12 * 3600) sys_offset += 24 * 3600; + int system_works = (sys_offset != 0 || strcasecmp(tzname, "UTC") == 0 || strcasecmp(tzname, "GMT") == 0); + if (system_works) { + char buf[64]; + strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", &local_tm); snprintf(resp, sizeof resp, "%s: %s %s", src_nick, tzname, buf); - else - snprintf(resp, sizeof resp, "%s: Unknown timezone: %s", src_nick, tzname); + } else { + const TzEntry *tz = tz_lookup(tzname); + if (tz) { + int off = tz->off; + if (tz->dst_north && tz->off != tz->dst_off) { + int mon = utc_tm.tm_mon + 1; + if (tz->dst_north == 1) + off = (mon >= 4 && mon <= 10) ? tz->dst_off : tz->off; + else + off = (mon >= 10 || mon <= 3) ? tz->dst_off : tz->off; + } + char buf[128]; + tz_format(now, off, tzname, off != tz->off, buf, sizeof buf); + snprintf(resp, sizeof resp, "%s: %s", src_nick, buf); + } else { + snprintf(resp, sizeof resp, "%s: Unknown timezone: %s", src_nick, tzname); + } + } } else { time_t now = time(NULL); struct tm *tm = localtime(&now);