Skip to content

Commit

Permalink
Fix strflocaltime on DST values
Browse files Browse the repository at this point in the history
The following behaviour demonstrates how the daylight savings value gets
lost when a time value gets turned into an internal array of numbers and
then back into a (struct tm):

        $ TZ=Europe/London ./jq -r -n '1504803635 | strflocaltime("%Y-%m-%d %H:%M:%S (%Z)")'
        2017-09-07 18:00:35 (GMT)

After this change however, this works as expected:

        $ TZ=Europe/London ./jq -r -n '1504803635 | strflocaltime("%Y-%m-%d %H:%M:%S (%Z)")'
        2017-09-07 18:00:35 (BST)
  • Loading branch information
wjlroe committed Mar 3, 2022
1 parent a9f97e9 commit 439cf72
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,8 @@ static jv tm2jv(struct tm *tm) {
jv_number(tm->tm_min),
jv_number(tm->tm_sec),
jv_number(tm->tm_wday),
jv_number(tm->tm_yday));
jv_number(tm->tm_yday),
jv_number(tm->tm_isdst));
}

#if defined(WIN32) && !defined(HAVE_SETENV)
Expand Down Expand Up @@ -1460,12 +1461,9 @@ static int jv2tm(jv a, struct tm *tm) {
TO_TM_FIELD(tm->tm_sec, a, 5);
TO_TM_FIELD(tm->tm_wday, a, 6);
TO_TM_FIELD(tm->tm_yday, a, 7);
TO_TM_FIELD(tm->tm_isdst, a, 8);
jv_free(a);

// We use UTC everywhere (gettimeofday, gmtime) and UTC does not do DST.
// Setting tm_isdst to 0 is done by the memset.
// tm->tm_isdst = 0;

// The standard permits the tm structure to contain additional members. We
// hope it is okay to initialize them to zero, because the standard does not
// provide an alternative.
Expand Down
4 changes: 2 additions & 2 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ bsearch(0,2,4)
# strptime tests are in optional.test

strftime("%Y-%m-%dT%H:%M:%SZ")
[2015,2,5,23,51,47,4,63]
[2015,2,5,23,51,47,4,63,0]
"2015-03-05T23:51:47Z"

strftime("%A, %B %d, %Y")
Expand All @@ -1449,7 +1449,7 @@ strftime("%A, %B %d, %Y")

gmtime
1425599507
[2015,2,5,23,51,47,4,63]
[2015,2,5,23,51,47,4,63,0]

# module system
import "a" as foo; import "b" as bar; def fooa: foo::a; [fooa, bar::a, bar::b, foo::a]
Expand Down
2 changes: 1 addition & 1 deletion tests/man.test
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ fromdate

strptime("%Y-%m-%dT%H:%M:%SZ")
"2015-03-05T23:51:47Z"
[2015,2,5,23,51,47,4,63]
[2015,2,5,23,51,47,4,63,0]

strptime("%Y-%m-%dT%H:%M:%SZ")|mktime
"2015-03-05T23:51:47Z"
Expand Down
4 changes: 2 additions & 2 deletions tests/optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
# strptime() is not available on mingw/WIN32
[strptime("%Y-%m-%dT%H:%M:%SZ")|(.,mktime)]
"2015-03-05T23:51:47Z"
[[2015,2,5,23,51,47,4,63],1425599507]
[[2015,2,5,23,51,47,4,63,0],1425599507]

# Check day-of-week and day of year computations
# (should trip an assert if this fails)
# This date range
last(range(365 * 67)|("1970-03-01T01:02:03Z"|strptime("%Y-%m-%dT%H:%M:%SZ")|mktime) + (86400 * .)|strftime("%Y-%m-%dT%H:%M:%SZ")|strptime("%Y-%m-%dT%H:%M:%SZ"))
null
[2037,1,11,1,2,3,3,41]
[2037,1,11,1,2,3,3,41,0]

# %e is not available on mingw/WIN32
strftime("%A, %B %e, %Y")
Expand Down

0 comments on commit 439cf72

Please sign in to comment.