Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1474,8 +1474,10 @@ static time_t my_mktime(struct tm *tm) {
if (tz != NULL)
setenv("TZ", "", 1);
time_t t = mktime(tm);
if (tz != NULL)
if (tz != NULL) {
setenv("TZ", tz, 1);
free(tz);
}
return t;
#endif
}
Expand Down Expand Up @@ -1762,13 +1764,14 @@ static jv f_strftime(jq_state *jq, jv a, jv b) {
/* Apple Libc (as of version 1669.40.2) contains a bug which causes it to
* ignore the `tm.tm_gmtoff` in favor of the global timezone. To print the
* proper timezone offset we temporarily switch the TZ to UTC. */
char *tz = getenv("TZ");
char *tz = (tz = getenv("TZ")) != NULL ? strdup(tz) : NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no free(tz) after setenv?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

setenv("TZ", "UTC", 1);
#endif
size_t n = strftime(buf, alloced, fmt, &tm);
#ifdef __APPLE__
if (tz) {
setenv("TZ", tz, 1);
free(tz);
} else {
unsetenv("TZ");
}
Expand Down
8 changes: 8 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,14 @@ if ! $msys && ! $mingw; then
echo "Incorrectly formatted universal time"
exit 1
fi

if ! r=$(TZ=Etc/GMT+7 $JQ -nc '1731627341 | .,. | [strftime("%FT%T"),strflocaltime("%FT%T%z")]'
) \
|| [ "$r" != '["2024-11-14T23:35:41","2024-11-14T16:35:41-0700"]
["2024-11-14T23:35:41","2024-11-14T16:35:41-0700"]' ]; then
echo "strftime or strftimelocal are not pure functions"
exit 1
fi
fi

exit 0
Loading