From aa9204b86cc1054e297f823c9cd55e0b8d64e2eb Mon Sep 17 00:00:00 2001 From: itchyny Date: Sat, 7 Jun 2025 07:38:30 +0900 Subject: [PATCH] Fix portability of strptime especially for Windows --- src/builtin.c | 7 +------ tests/jq.test | 16 ++++++++++++++-- tests/optional.test | 15 +-------------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/builtin.c b/src/builtin.c index 4863614081..5787ca852a 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -1559,14 +1559,9 @@ static jv f_strptime(jq_state *jq, jv a, jv b) { memset(&tm, 0, sizeof(tm)); tm.tm_wday = 8; // sentinel tm.tm_yday = 367; // sentinel + const char *input = jv_string_value(a); const char *fmt = jv_string_value(b); - -#ifndef HAVE_STRPTIME - if (strcmp(fmt, "%Y-%m-%dT%H:%M:%SZ")) { - return ret_error2(a, b, jv_string("strptime/1 only supports ISO 8601 on this platform")); - } -#endif const char *end = strptime(input, fmt, &tm); if (end == NULL || (*end != '\0' && !isspace((unsigned char)*end))) { return ret_error2(a, b, jv_string_fmt("date \"%s\" does not match format \"%s\"", input, fmt)); diff --git a/tests/jq.test b/tests/jq.test index 7e04c166d5..bf2311e750 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1810,8 +1810,6 @@ try ["OK", bsearch(0)] catch ["KO",.] "aa" ["KO","string (\"aa\") cannot be searched from"] -# strptime tests are in optional.test - strftime("%Y-%m-%dT%H:%M:%SZ") [2015,2,5,23,51,47,4,63] "2015-03-05T23:51:47Z" @@ -1854,6 +1852,20 @@ try ["OK", strflocaltime({})] catch ["KO", .] 0 ["KO","strflocaltime/1 requires a string format"] +[strptime("%Y-%m-%dT%H:%M:%SZ")|(.,mktime)] +"2015-03-05T23:51:47Z" +[[2015,2,5,23,51,47,4,63],1425599507] + +[strptime("%FT%T")|(.,mktime)] +"2025-06-07T08:09:10" +[[2025,5,7,8,9,10,6,157],1749283750] + +# Check day-of-week and day of year computations +# (should trip an assert if this fails) +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] + # module system import "a" as foo; import "b" as bar; def fooa: foo::a; [fooa, bar::a, bar::b, foo::a] null diff --git a/tests/optional.test b/tests/optional.test index 2623731bf5..77354ccefe 100644 --- a/tests/optional.test +++ b/tests/optional.test @@ -1,18 +1,6 @@ # See tests/jq.test and the jq manual for more information. -# 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] - -# 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] - -# Regression test for #3276 +# Regression test for #3276 (fails on mingw/WIN32) fromdate "2038-01-19T03:14:08Z" 2147483648 @@ -22,4 +10,3 @@ strftime("%A, %B %e, %Y") 1435677542.822351 "Tuesday, June 30, 2015" -