2
0
Fork 0
forked from zynk/zynk

fix !time timezone conversion: use TZ=date via popen instead of broken localtime approach

This commit is contained in:
Johannes Findeisen 2026-07-20 03:16:01 +02:00
commit f6431b02a5
2 changed files with 23 additions and 15 deletions

View file

@ -1,3 +1,8 @@
2026.07.20 – Fix !time timezone conversion bug.
- Fixed `!time` command: `setenv("TZ",...) + localtime()` was not reliably converting to the requested timezone. Replaced with `TZ=... date` via `popen()`, which handles all IANA timezone names correctly (e.g. `!time Australia/Sydney` now shows 11:15 AEST instead of UTC).
- Version bumped to 0.35.2.
2026.07.20 – Fix !calc not working (bc stdin not connected).
- Fixed `!calc` command: the expression was never piped to `bc`'s stdin, causing empty output and a "no result" error for every expression. Added a second pipe to feed the expression into `bc` before reading the result.

33
zynk.c
View file

@ -29,7 +29,7 @@ int asprintf(char **, const char *, ...);
/* ---- constants ---- */
#define VERSION "0.35.1"
#define VERSION "0.35.2"
#define DEFAULT_SERVER "irc.libera.chat"
#define OPENCODE_BIN "/usr/bin/opencode"
#define CLR_RESET "\033[0m"
@ -2619,8 +2619,6 @@ static int cmd_time(Session *s, const char *msg, const char *reply_target, const
if (strncmp(msg, "!time", 5) != 0) return 0;
if (msg[5] != ' ' && msg[5] != '\0') return 0;
if (rate_limit_check(src_nick) < 0) return 1;
time_t now = time(NULL);
struct tm *tm;
char resp[256];
const char *rest = msg[5] == ' ' ? msg + 6 : "";
while (*rest == ' ') rest++;
@ -2630,19 +2628,24 @@ 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 *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();
tm = localtime(&now);
char buf[64];
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", tm);
if (old_tz_buf[0]) setenv("TZ", old_tz_buf, 1); else unsetenv("TZ");
tzset();
snprintf(resp, sizeof resp, "%s: %s %s", src_nick, tzname, buf);
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])
snprintf(resp, sizeof resp, "%s: %s %s", src_nick, tzname, buf);
else
snprintf(resp, sizeof resp, "%s: Unknown timezone: %s", src_nick, tzname);
} else {
tm = localtime(&now);
time_t now = time(NULL);
struct tm *tm = localtime(&now);
char buf[64];
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", tm);
snprintf(resp, sizeof resp, "%s: %s", src_nick, buf);