Skip to content

Commit 0a35b19

Browse files
committed
fix: add workaround for apple libc
Apple's Libc implementation contains a bug which causees it to ignore the offset data present in the tm struct in favor of the older heuristic needed by legacy standards. This workaround temporarily sets the global timezone so it gets picked up during formatting.
1 parent 1a12699 commit 0a35b19

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/builtin.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,21 @@ static jv f_strftime(jq_state *jq, jv a, jv b) {
17531753
const char *fmt = jv_string_value(b);
17541754
size_t alloced = strlen(fmt) + 100;
17551755
char *buf = alloca(alloced);
1756+
#ifdef __APPLE__
1757+
/* Apple Libc (as of version 1669.40.2) contains a bug which causes it to
1758+
* ignore the `tm.tm_gmtoff` in favor of the global timezone. To print the
1759+
* proper timezone offset we temporarily switch the TZ to UTC. */
1760+
char *tz = getenv("TZ");
1761+
setenv("TZ", "UTC", 1);
1762+
#endif
17561763
size_t n = strftime(buf, alloced, fmt, &tm);
1764+
#ifdef __APPLE__
1765+
if (tz) {
1766+
setenv("TZ", tz, 1);
1767+
} else {
1768+
unsetenv("TZ");
1769+
}
1770+
#endif
17571771
jv_free(b);
17581772
/* POSIX doesn't provide errno values for strftime() failures; weird */
17591773
if (n == 0 || n > alloced)

0 commit comments

Comments
 (0)