Skip to content

Commit 54e9962

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 0f096c2 commit 54e9962

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
@@ -1758,7 +1758,21 @@ static jv f_strftime(jq_state *jq, jv a, jv b) {
17581758
const char *fmt = jv_string_value(b);
17591759
size_t alloced = strlen(fmt) + 100;
17601760
char *buf = alloca(alloced);
1761+
#ifdef __APPLE__
1762+
/* Apple Libc (as of version 1669.40.2) contains a bug which causes it to
1763+
* ignore the `tm.tm_gmtoff` in favor of the global timezone. To print the
1764+
* proper timezone offset we temporarily switch the TZ to UTC. */
1765+
char *tz = getenv("TZ");
1766+
setenv("TZ", "UTC", 1);
1767+
#endif
17611768
size_t n = strftime(buf, alloced, fmt, &tm);
1769+
#ifdef __APPLE__
1770+
if (tz) {
1771+
setenv("TZ", tz, 1);
1772+
} else {
1773+
unsetenv("TZ");
1774+
}
1775+
#endif
17621776
jv_free(b);
17631777
/* POSIX doesn't provide errno values for strftime() failures; weird */
17641778
if (n == 0 || n > alloced)

0 commit comments

Comments
 (0)