Parse the full ISO 8601 grammar in `fromdateiso8601` (#3617)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
`fromdateiso8601` was `strptime("%Y-%m-%dT%H:%M:%SZ") | mktime`, which accepts only one of the many representations ISO 8601 permits. strptime cannot be extended to cover the rest portably, since it has no specifier for fractional seconds and glibc discards `%G` and `%V`. Replace it with a parser in src/iso8601.c that handles basic and extended representations, ordinal and week dates, fractional seconds, offsets and date-only input. It uses integer arithmetic instead of `time_t`, so the results no longer depend on the platform. This is stricter than before. Impossible dates like `2015-02-30`, which `mktime` used to normalize into March, and unpadded components like `2015-3-5T6:00:00Z` are now errors. So is any trailing input, including whitespace. Leading whitespace is rejected too; before, glibc skipped it and macOS did not. A leap second now maps to the second before it rather than the next minute. The error message has changed, and the result can now be non-integer.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+560 added−20 removed
- tests:+113 added−5 removed
- other:+447 added−15 removed
Provenance
- Commit
fd25c3e- Tree
78f527b- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
a19efdd
10 files changed+560 added−20 removed
modified:docs/content/manual/dev/manual.yml+51 added-8 removed
2248 unmodified lines2249 builtins deal exclusively with time in UTC.2250 2251 The `fromdateiso8601` builtin parses datetimes in the ISO 86012252 format to a number of seconds since the Unix epoch2253- (1970-01-01T00:00:00Z). The `todateiso8601` builtin does the2254- inverse.2253+ (1970-01-01T00:00:00Z). The `todateiso8601` builtin formats a2254+ number of seconds as `"%Y-%m-%dT%H:%M:%SZ"`.2255 2256- The `fromdate` builtin parses datetime strings. Currently2257- `fromdate` only supports ISO 8601 datetime strings, but in the2258- future it will attempt to parse datetime strings in more2259- formats.2256+ `fromdateiso8601` accepts a complete date, optionally followed by2257+ a time of day, written in either the extended representation, which2258+ separates the components, or the basic representation, which does2259+ not. The two cannot be mixed within one datetime. The date may be2260+ a calendar date (`2015-03-05`, `20150305`), an ordinal date2261+ (`2015-064`, `2015064`), or a week date (`2015-W10-4`, `2015W104`);2262+ all three denote the same day. Years outside the range 0000 to 99992263+ are written with a sign and six digits, as in `+012015-03-05`.2264 2265+ The time of day may omit its seconds, or its minutes and seconds,2266+ as in `2015-03-05T23:51Z` and `2015-03-05T23Z`, or be left out2267+ altogether, in which case the value is midnight UTC. It may carry a2268+ fractional part after a `.` or a `,`, which belongs to the lowest2269+ component present, so `23,5` is half past 23. It may be followed2270+ by an offset from UTC written as `Z`, `+hh`, `+hh:mm` or `+hhmm`;2271+ a datetime with no offset is read as UTC. A time of `24:00:00`2272+ denotes midnight of the following day, and a leap second is read as2273+ the second before it, since jq counts every day as 86400 seconds.2274+2275+ The `W`, `T` and `Z` designators may also be written in lower2276+ case, which ISO 8601 permits where upper case is not available.2277+ RFC 3339 additionally allows the date and the time to be separated2278+ by a space instead of a `T` "for the sake of readability", which jq2279+ accepts too. The offset may use the other representation's2280+ separator style, as in `2015-03-05T23:51:47+0900`, although2281+ ISO 8601 does not allow the basic and extended representations2282+ to be mixed.2283+2284+ A complete date is required, so `2015` and `2015-03` are errors2285+ even though ISO 8601 defines them as dates, and `T23:51:47` is an2286+ error because a time of day on its own is not a point in time.2287+ Dates that do not exist, such as `2015-02-30` or `2016-W53-1`, are2288+ errors too, as is `2015-03-05Z`, since a zone designator belongs to2289+ the time of day.2290+2291+ The `fromdate` builtin is an alias for `fromdateiso8601`.2292+2293 The `todate` builtin is an alias for `todateiso8601`.2294 2295 The `now` builtin outputs the current time, in seconds since2296 the Unix epoch.30 unmodified lines2327 given format. The `strflocaltime` does the same, but using2328 the local timezone setting.2329 2330 The format strings for `strptime` and `strftime` are described2299- in typical C library documentation. The format string for ISO2300- 8601 datetime is `"%Y-%m-%dT%H:%M:%SZ"`.2331+ in typical C library documentation. `"%Y-%m-%dT%H:%M:%SZ"` is2332+ the format that `todateiso8601` produces; it is only one of the2333+ ISO 8601 representations that `fromdateiso8601` accepts, and2334+ `strptime` cannot parse the others -- there is no portable format2335+ specifier for week dates or for fractional seconds.2336 2337 jq may not support some or all of this date functionality on2338 some systems. In particular, the `%u` and `%j` specifiers for2339 `strptime(fmt)` are not supported on macOS.2 unmodified lines2342 - program: 'fromdate'2343 input: '"2015-03-05T23:51:47Z"'2344 output: ['1425599507']2345 2346+ - program: 'fromdateiso8601'2347+ input: '"2015-03-06T08:51:47.25+09:00"'2348+ output: ['1425599507.25']2349+2350+ - program: 'map(fromdateiso8601)'2351+ input: '["20150305T235147Z","2015-064T23:51:47Z","2015-W10-4T23:51:47Z"]'2352+ output: ['[1425599507,1425599507,1425599507]']2353+2354 - program: 'strptime("%Y-%m-%dT%H:%M:%SZ")'2355 input: '"2015-03-05T23:51:47Z"'2356 output: ['[2015,2,5,23,51,47,4,63]']2357 1549 unmodified linesmodified:src/builtin.c+13 added
30 unmodified lines31 #include "jq_parser.h"32 #include "bytecode.h"33 #include "linker.h"34 #include "locfile.h"35+#include "iso8601.h"36 #include "jv_unicode.h"37 #include "jv_alloc.h"38 #include "jv_dtoa.h"39 #include "jv_dtoa_tsd.h"1640 unmodified lines1680 jv_free(a); // must come after `*end` because `end` is a pointer into `a`'s string1681 return r;1682 }1683 1684+static jv f_fromdateiso8601(jq_state *jq, jv a) {1685+ if (jv_get_kind(a) != JV_KIND_STRING)1686+ return ret_error(a, jv_string("fromdateiso8601 requires string inputs"));1687+ double t;1688+ if (!iso8601_parse(jv_string_value(a), jv_string_length_bytes(jv_copy(a)), &t))1689+ return ret_error(a, jv_string_fmt("date \"%s\" is not a valid ISO 8601 datetime",1690+ jv_string_value(a)));1691+ jv_free(a);1692+ return jv_number(t);1693+}1694+1695 static int jv2tm(jv a, struct tm *tm, int localtime) {1696 memset(tm, 0, sizeof(*tm));1697 static const size_t offsets[] = {1698 offsetof(struct tm, tm_year),353 unmodified lines2052 CFUNC(f_modulemeta, "modulemeta", 1),2053 CFUNC(f_input, "input", 1),2054 CFUNC(f_debug, "debug", 1),2055 CFUNC(f_stderr, "stderr", 1),2056+ CFUNC(f_fromdateiso8601, "fromdateiso8601", 1),2057 CFUNC(f_strptime, "strptime", 2),2058 CFUNC(f_strftime, "strftime", 2),2059 CFUNC(f_strflocaltime, "strflocaltime", 2),2060 CFUNC(f_mktime, "mktime", 1),114 unmodified linesmodified:src/builtin.jq-1 removed
72 unmodified lines73 def flatten($x): if $x < 0 then error("flatten depth must not be negative") else _flatten($x) end;74 def flatten: _flatten(-1);75 def range($x;$y): range($x;$y;1);76 def range($x): range(0;$x);77-def fromdateiso8601: strptime("%Y-%m-%dT%H:%M:%SZ")|mktime;77 def todateiso8601: strftime("%Y-%m-%dT%H:%M:%SZ");78 def fromdate: fromdateiso8601;79 def todate: todateiso8601;80 def ltrimstr($left): if startswith($left) then .[$left | length:] end;161 unmodified linesadded:src/iso8601.c+342 added
1+/*2+ * A self-contained ISO 8601 date and time parser.3+ *4+ * The C library is of no help here: strptime() has no format specifier for5+ * fractional seconds, and no portable way to parse week dates (glibc6+ * consumes %G and %V but then discards them). The results would also7+ * differ between the host strptime() and the copy bundled in util.c.8+ * Everything below is plain integer arithmetic instead, which also keeps9+ * the computation clear of the range of a 32-bit time_t.10+ *11+ * The accepted grammar is12+ *13+ * datetime := date [ ("T"|"t"|" ") time ]14+ * date := year "-" MM "-" DD | year MMDD15+ * | year "-" DDD | year DDD16+ * | year "-" ("W"|"w") ww "-" d | year ("W"|"w") ww d17+ * year := YYYY | ("+"|"-") YYYYYY18+ * time := hms [ ("."|",") digits ] [ offset ]19+ * hms := hh [ ":" mm [ ":" ss ] ] | hh [ mm [ ss ] ]20+ * offset := ("Z"|"z") | ("+"|"-") hh [ [":"] mm ]21+ *22+ * As in the standard's own notation a run of letters stands for that many23+ * decimal digits -- DDD is the three-digit day of the year, d the one-digit24+ * day of the week -- while "digits" is one or more of them.25+ *26+ * The decimal fraction belongs to the lowest component of the hms that is27+ * present, so "23,5" is half past 23 and "2320,8" is 23:20:48.28+ *29+ * The date must always be complete, so "2015-03" is an error even though it30+ * is a valid ISO 8601 date. The time may drop its seconds, or its minutes31+ * and seconds, or be omitted altogether, in which case the value is32+ * midnight UTC. The date and the time must agree on whether separators are33+ * used, as ISO 8601 requires -- "2026-08-28T093015Z" is not a valid34+ * representation. The offset is exempt from that rule, since "+0900" after35+ * an extended time is common in the wild.36+ */37+38+#include <stdint.h>39+40+#include "iso8601.h"41+42+/*43+ * Days since 1970-01-01 of the civil date y-m-d in the proleptic Gregorian44+ * calendar, negative before the epoch, after Howard Hinnant's45+ * days_from_civil(). Requires 1 <= m <= 12 and 1 <= d <= days_in_month(y, m);46+ * the callers check both.47+ */48+static int64_t days_from_civil(int64_t y, int m, int d) {49+ y -= m <= 2;50+ int64_t era = (y >= 0 ? y : y - 399) / 400;51+ int64_t yoe = y - era * 400; /* [0, 399] */52+ int64_t doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; /* [0, 365] */53+ int64_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; /* [0, 146096] */54+ return era * 146097 + doe - 719468;55+}56+57+static int is_leap(int64_t y) {58+ return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;59+}60+61+static int days_in_month(int64_t y, int m) {62+ static const int d[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};63+ return m == 2 && is_leap(y) ? 29 : d[m - 1];64+}65+66+/* ISO weekday (1 = Monday .. 7 = Sunday) of a day count since the epoch. */67+static int iso_wday(int64_t days) {68+ int64_t w = (days + 3) % 7;69+ if (w < 0)70+ w += 7;71+ return (int)w + 1;72+}73+74+/*75+ * An ISO year has 53 weeks if it starts on a Thursday, or if it is a leap76+ * year starting on a Wednesday; otherwise it has 52.77+ */78+static int iso_weeks_in_year(int64_t y) {79+ int jan1 = iso_wday(days_from_civil(y, 1, 1));80+ return jan1 == 4 || (jan1 == 3 && is_leap(y)) ? 53 : 52;81+}82+83+static int civil_to_days(int64_t y, int mon, int mday, int64_t *days) {84+ if (mon < 1 || mon > 12 || mday < 1 || mday > days_in_month(y, mon))85+ return 0;86+ *days = days_from_civil(y, mon, mday);87+ return 1;88+}89+90+static int ordinal_to_days(int64_t y, int yday, int64_t *days) {91+ if (yday < 1 || yday > (is_leap(y) ? 366 : 365))92+ return 0;93+ *days = days_from_civil(y, 1, 1) + yday - 1;94+ return 1;95+}96+97+static int week_to_days(int64_t y, int week, int wday, int64_t *days) {98+ if (week < 1 || week > iso_weeks_in_year(y) || wday < 1 || wday > 7)99+ return 0;100+ int64_t jan4 = days_from_civil(y, 1, 4);101+ *days = jan4 - (iso_wday(jan4) - 1) + (int64_t)(week - 1) * 7 + (wday - 1);102+ return 1;103+}104+105+/*106+ * The scanner works over an explicit [p, end) range rather than a C string, so107+ * that a NUL byte embedded in the input is a parse error instead of silently108+ * truncating the value.109+ */110+typedef struct {111+ const char *p;112+ const char *end;113+} iso8601_scanner;114+115+static int scan_at_end(iso8601_scanner *s) {116+ return s->p == s->end;117+}118+119+static int scan_peek(iso8601_scanner *s) {120+ return scan_at_end(s) ? -1 : (unsigned char)*s->p;121+}122+123+static int scan_eat(iso8601_scanner *s, char c) {124+ if (scan_at_end(s) || *s->p != c)125+ return 0;126+ s->p++;127+ return 1;128+}129+130+/*131+ * W, T and Z are what ISO 8601 calls designators. They are written in upper132+ * case, but the standard notes that "lower case characters may be used when133+ * upper case characters are not available", so match either.134+ */135+static int scan_designator(iso8601_scanner *s, char c) {136+ if (scan_at_end(s) || (*s->p != c && *s->p != c + ('a' - 'A')))137+ return 0;138+ s->p++;139+ return 1;140+}141+142+static size_t scan_count_digits(iso8601_scanner *s) {143+ size_t n = 0;144+ while (s->p + n < s->end && s->p[n] >= '0' && s->p[n] <= '9')145+ n++;146+ return n;147+}148+149+static int scan_digits(iso8601_scanner *s, int n, int *out) {150+ if (s->end - s->p < n)151+ return 0;152+ int v = 0;153+ for (int i = 0; i < n; i++) {154+ unsigned char c = (unsigned char)s->p[i];155+ if (c < '0' || c > '9')156+ return 0;157+ v = v * 10 + (c - '0');158+ }159+ s->p += n;160+ *out = v;161+ return 1;162+}163+164+/*165+ * A plain year is exactly four digits. An expanded year carries a mandatory166+ * sign and exactly six digits, which is what makes it unambiguous in front of167+ * a date in the basic form.168+ */169+static int parse_year(iso8601_scanner *s, int64_t *year) {170+ int64_t sign = 1;171+ int digits = 4;172+ if (scan_peek(s) == '+' || scan_peek(s) == '-') {173+ sign = *s->p == '-' ? -1 : 1;174+ s->p++;175+ digits = 6;176+ }177+ if (s->end - s->p < digits)178+ return 0;179+ int64_t v = 0;180+ for (int i = 0; i < digits; i++) {181+ unsigned char c = (unsigned char)s->p[i];182+ if (c < '0' || c > '9')183+ return 0;184+ v = v * 10 + (c - '0');185+ }186+ s->p += digits;187+ *year = sign * v;188+ return 1;189+}190+191+/* Sets *extended to whether the date was written with "-" separators. */192+static int parse_date(iso8601_scanner *s, int64_t *days, int *extended) {193+ int64_t year;194+ int week, wday, mon, mday, yday;195+196+ if (!parse_year(s, &year))197+ return 0;198+199+ if (scan_eat(s, '-')) {200+ *extended = 1;201+ if (scan_designator(s, 'W')) {202+ return scan_digits(s, 2, &week) && scan_eat(s, '-') &&203+ scan_digits(s, 1, &wday) && week_to_days(year, week, wday, days);204+ }205+ switch (scan_count_digits(s)) {206+ case 3:207+ return scan_digits(s, 3, &yday) && ordinal_to_days(year, yday, days);208+ case 2:209+ return scan_digits(s, 2, &mon) && scan_eat(s, '-') &&210+ scan_digits(s, 2, &mday) && civil_to_days(year, mon, mday, days);211+ default:212+ return 0;213+ }214+ }215+216+ *extended = 0;217+ if (scan_designator(s, 'W')) {218+ return scan_digits(s, 2, &week) && scan_digits(s, 1, &wday) &&219+ week_to_days(year, week, wday, days);220+ }221+ switch (scan_count_digits(s)) {222+ case 4:223+ return scan_digits(s, 2, &mon) && scan_digits(s, 2, &mday) &&224+ civil_to_days(year, mon, mday, days);225+ case 3:226+ return scan_digits(s, 3, &yday) && ordinal_to_days(year, yday, days);227+ default:228+ /* A bare "YYYYMM" is ambiguous with "YYMMDD"; the standard forbids it. */229+ return 0;230+ }231+}232+233+/*234+ * Parses the time of day, storing the seconds since midnight in *secs. A235+ * time of "24:00:00" is midnight of the following day, which *carry reports.236+ *237+ * The minutes and seconds may be omitted, in which case the decimal fraction238+ * belongs to the lowest component that is present -- "23,5" is half past 23239+ * -- so *unit* records how many seconds that component is worth.240+ */241+static int parse_time(iso8601_scanner *s, int extended, double *secs,242+ int64_t *carry) {243+ int hh, mm = 0, ss = 0, unit = 3600;244+245+ if (!scan_digits(s, 2, &hh))246+ return 0;247+ if (extended ? scan_eat(s, ':') : scan_count_digits(s) >= 2) {248+ if (!scan_digits(s, 2, &mm))249+ return 0;250+ unit = 60;251+ if (extended ? scan_eat(s, ':') : scan_count_digits(s) >= 2) {252+ if (!scan_digits(s, 2, &ss))253+ return 0;254+ unit = 1;255+ }256+ }257+258+ double frac = 0;259+ if (scan_peek(s) == '.' || scan_peek(s) == ',') {260+ static const double pow10[] = {261+ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,262+ 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15263+ };264+ s->p++;265+ if (scan_count_digits(s) == 0)266+ return 0;267+ uint64_t num = 0;268+ int digits = 0;269+ while (scan_peek(s) >= '0' && scan_peek(s) <= '9') {270+ if (digits < 15) {271+ num = num * 10 + (uint64_t)(*s->p - '0');272+ digits++;273+ }274+ s->p++;275+ }276+ frac = (double)num / pow10[digits];277+ }278+279+ *carry = 0;280+ if (hh == 24) {281+ if (mm != 0 || ss != 0 || frac != 0)282+ return 0;283+ hh = 0;284+ *carry = 1;285+ } else if (hh > 24 || mm > 59 || ss > 60) {286+ return 0;287+ } else if (ss == 60) {288+ ss = 59;289+ }290+291+ *secs = hh * 3600 + mm * 60 + ss + frac * unit;292+ return 1;293+}294+295+/* An absent offset means UTC, which is what jq has always assumed. */296+static int parse_offset(iso8601_scanner *s, int *offset) {297+ if (scan_designator(s, 'Z')) {298+ *offset = 0;299+ return 1;300+ }301+302+ int sign = scan_peek(s);303+ if (sign != '+' && sign != '-') {304+ *offset = 0;305+ return 1;306+ }307+308+ s->p++;309+ int oh, om = 0;310+ if (!scan_digits(s, 2, &oh))311+ return 0;312+ int had_colon = scan_eat(s, ':');313+ if (!scan_digits(s, 2, &om) && had_colon)314+ return 0;315+ if (oh > 23 || om > 59)316+ return 0;317+318+ *offset = (sign == '+' ? 1 : -1) * (oh * 3600 + om * 60);319+ return 1;320+}321+322+int iso8601_parse(const char *buf, size_t len, double *out) {323+ iso8601_scanner s = {buf, buf + len};324+ int64_t days, carry = 0;325+ int extended, offset = 0, sep;326+ double secs = 0;327+328+ if (!parse_date(&s, &days, &extended))329+ return 0;330+331+ sep = scan_peek(&s);332+ if (sep == 'T' || sep == 't' || sep == ' ') {333+ s.p++;334+ if (!parse_time(&s, extended, &secs, &carry) || !parse_offset(&s, &offset))335+ return 0;336+ }337+ if (!scan_at_end(&s))338+ return 0;339+340+ *out = (double)(days + carry) * 86400.0 + secs - offset;341+ return 1;342+}added:src/iso8601.h+14 added
1+#ifndef JQ_ISO8601_H2+#define JQ_ISO8601_H3+4+#include <stddef.h>5+6+/*7+ * Parses [buf, buf + len) as an ISO 8601 date and time of day.8+ *9+ * On success stores the number of seconds since the Unix epoch in *out and10+ * returns 1. On failure returns 0 and leaves *out untouched.11+ */12+int iso8601_parse(const char *buf, size_t len, double *out);13+14+#endif /* JQ_ISO8601_H */modified:tests/jq.test+105 added
1919 unmodified lines1920 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"))1921 null1922 [2037,1,11,1,2,3,3,41]1923 1924+# fromdateiso8601 accepts the calendar, ordinal and week date forms,1925+# in both the extended and the basic representation1926+[("2015-03-05T23:51:47Z", "20150305T235147Z", "2015-064T23:51:47Z", "2015064T235147Z", "2015-W10-4T23:51:47Z", "2015W104T235147Z") | fromdateiso8601]1927+null1928+[1425599507,1425599507,1425599507,1425599507,1425599507,1425599507]1929+1930+# fractional seconds, with either decimal sign1931+[("2015-03-05T23:51:47.25Z", "2015-03-05T23:51:47,25Z", "20150305T235147.25Z") | fromdateiso8601]1932+null1933+[1425599507.25,1425599507.25,1425599507.25]1934+1935+# UTC offsets; an absent offset means UTC1936+[("2015-03-06T08:51:47+09:00", "2015-03-06T08:51:47+0900", "2015-03-06T08:51:47+09", "2015-03-05T18:21:47-05:30", "2015-03-05T23:51:47z", "2015-03-05T23:51:47") | fromdateiso8601]1937+null1938+[1425599507,1425599507,1425599507,1425599507,1425599507,1425599507]1939+1940+# the date and the time may be separated by "T", "t" or a space1941+[("2015-03-05t23:51:47Z", "2015-03-05 23:51:47Z") | fromdateiso8601]1942+null1943+[1425599507,1425599507]1944+1945+# the W, T and Z designators may be written in lower case1946+[("2015-w10-4t23:51:47z", "2015w104t235147z", "2015-03-05t23:51:47z") | fromdateiso8601]1947+null1948+[1425599507,1425599507,1425599507]1949+1950+# expanded years carry a sign and exactly six digits1951+[("+012015-03-05T23:51:47Z", "-000001-01-01T00:00:00Z", "+0020150305T235147Z", "+2025-03-05", "+02025-03-05", "+0002025-03-05") | try fromdateiso8601 catch "ERR"]1952+null1953+[316995119507,-62198755200,1425599507,"ERR","ERR","ERR"]1954+1955+# "24:00:00" is midnight of the following day, at any precision1956+[("2015-03-05T24:00:00Z", "2015-03-05T24:00Z", "2015-03-05T24Z") | fromdateiso8601 | todate]1957+null1958+["2015-03-06T00:00:00Z","2015-03-06T00:00:00Z","2015-03-06T00:00:00Z"]1959+1960+# leap seconds are dropped rather than rejected1961+"2015-06-30T23:59:60Z" | fromdateiso8601 | todate1962+null1963+"2015-06-30T23:59:59Z"1964+1965+# 2015 starts on a Thursday, so it has 53 ISO weeks1966+"2015-W53-1T00:00:00Z" | fromdateiso8601 | todate1967+null1968+"2015-12-28T00:00:00Z"1969+1970+# 2016 is a leap year starting on a Friday: 366 days, but only 52 ISO weeks1971+[("2016-366T00:00:00Z", "2016-W52-7T00:00:00Z") | fromdateiso8601 | todate]1972+null1973+["2016-12-31T00:00:00Z","2017-01-01T00:00:00Z"]1974+1975+# fromdateiso8601 is not limited by the range of a 32-bit time_t (#3276)1976+fromdate1977+"2038-01-19T03:14:08Z"1978+21474836481979+1980+# the time may drop its seconds, or its minutes and seconds1981+[("2015-03-05T23:51Z", "2015-03-05T23Z", "20150305T2351Z", "20150305T23Z") | fromdateiso8601]1982+null1983+[1425599460,1425596400,1425599460,1425596400]1984+1985+# a decimal fraction belongs to the lowest component present1986+[("2015-03-05T23:51:47,5Z", "2015-03-05T23:51,5Z", "2015-03-05T23,5Z") | fromdateiso8601]1987+null1988+[1425599507.5,1425599490,1425598200]1989+1990+# a date on its own is midnight UTC1991+[("2015-03-05", "20150305", "2015-064", "2015064", "2015-W10-4", "2015W104") | fromdateiso8601]1992+null1993+[1425513600,1425513600,1425513600,1425513600,1425513600,1425513600]1994+1995+# the zone designator belongs to the time, so a bare date cannot carry one1996+[("2015-03-05Z", "2015-03-05+09:00", "2015-03-05T") | try fromdateiso8601 catch "ERR"]1997+null1998+["ERR","ERR","ERR"]1999+2000+# the date must be complete, and a time must not be empty2001+[("2015", "2015-03", "2015-W10", "2015-03T00:00:00Z", "T23:51:47Z", "T23:51:47", "23:51:47", "") | try fromdateiso8601 catch "ERR"]2002+null2003+["ERR","ERR","ERR","ERR","ERR","ERR","ERR","ERR"]2004+2005+# the date and the time must agree on whether separators are used,2006+# and "YYYYMM" is ambiguous with "YYMMDD"2007+[("2015-03-05T235147Z", "20150305T23:51:47Z", "201503T00:00:00Z", "2015-03-05T2351Z", "20150305T23:51Z", "20150305T23205Z") | try fromdateiso8601 catch "ERR"]2008+null2009+["ERR","ERR","ERR","ERR","ERR","ERR"]2010+2011+# out-of-range components are rejected2012+[("2015-02-30T00:00:00Z", "2015-13-01T00:00:00Z", "2015-00-01T00:00:00Z", "2015-366T00:00:00Z", "2016-W53-1T00:00:00Z", "2015-W10-8T00:00:00Z", "2015-W00-1T00:00:00Z", "2015-03-05T25:00:00Z", "2015-03-05T24:00:01Z", "2015-03-05T24:01Z", "2015-03-05T23:60:00Z", "2015-03-05T23:51:47+24:00", "2015-03-05T23:51:47+09:") | try fromdateiso8601 catch "ERR"]2013+null2014+["ERR","ERR","ERR","ERR","ERR","ERR","ERR","ERR","ERR","ERR","ERR","ERR","ERR"]2015+2016+# the input must be exactly a datetime: no surrounding whitespace2017+[(" 2015-03-05T23:51:47Z", "2015-03-05T23:51:47Z ") | try fromdateiso8601 catch "ERR"]2018+null2019+["ERR","ERR"]2020+2021+try fromdateiso8601 catch .2022+1232023+"fromdateiso8601 requires string inputs"2024+2025+try fromdateiso8601 catch .2026+"not a date"2027+"date \"not a date\" is not a valid ISO 8601 datetime"2028+2029 # module system2030 import "a" as foo; import "b" as bar; def fooa: foo::a; [fooa, bar::a, bar::b, foo::a]2031 null2032 ["a","b","c","a"]764 unmodified linesmodified:tests/man.test+8 added
745 unmodified lines746 fromdate747 "2015-03-05T23:51:47Z"748 1425599507749 750+fromdateiso8601751+"2015-03-06T08:51:47.25+09:00"752+1425599507.25753+754+map(fromdateiso8601)755+["20150305T235147Z","2015-064T23:51:47Z","2015-W10-4T23:51:47Z"]756+[1425599507,1425599507,1425599507]757+758 strptime("%Y-%m-%dT%H:%M:%SZ")759 "2015-03-05T23:51:47Z"760 [2015,2,5,23,51,47,4,63]761 245 unmodified linesmodified:tests/optional.test-5 removed
1 # See tests/jq.test and the jq manual for more information.2 3-# Regression test for #3276 (fails on mingw/WIN32)4-fromdate5-"2038-01-19T03:14:08Z"6-21474836487-3 # %e is not available on mingw/WIN324 strftime("%A, %B %e, %Y")5 1435677542.8223516 "Tuesday, June 30, 2015"9 unmodified linesmodified:jq.1.prebuilt+24 added-4 removed
1 .2-.TH "JQ" "1" "May 2025" "" ""2+.TH "JQ" "1" "August 2026" "" ""3 .4 .SH "NAME"5 \fBjq\fR \- Command\-line JSON processor6 .2449 unmodified lines2456 .SS "Dates"2457 jq provides some basic date handling functionality, with some high\-level and low\-level builtins\. In all cases these builtins deal exclusively with time in UTC\.2458 .2459 .P2460-The \fBfromdateiso8601\fR builtin parses datetimes in the ISO 8601 format to a number of seconds since the Unix epoch (1970\-01\-01T00:00:00Z)\. The \fBtodateiso8601\fR builtin does the inverse\.2460+The \fBfromdateiso8601\fR builtin parses datetimes in the ISO 8601 format to a number of seconds since the Unix epoch (1970\-01\-01T00:00:00Z)\. The \fBtodateiso8601\fR builtin formats a number of seconds as \fB"%Y\-%m\-%dT%H:%M:%SZ"\fR\.2461 .2462 .P2463-The \fBfromdate\fR builtin parses datetime strings\. Currently \fBfromdate\fR only supports ISO 8601 datetime strings, but in the future it will attempt to parse datetime strings in more formats\.2463+\fBfromdateiso8601\fR accepts a complete date, optionally followed by a time of day, written in either the extended representation, which separates the components, or the basic representation, which does not\. The two cannot be mixed within one datetime\. The date may be a calendar date (\fB2015\-03\-05\fR, \fB20150305\fR), an ordinal date (\fB2015\-064\fR, \fB2015064\fR), or a week date (\fB2015\-W10\-4\fR, \fB2015W104\fR); all three denote the same day\. Years outside the range 0000 to 9999 are written with a sign and six digits, as in \fB+012015\-03\-05\fR\.2464 .2465 .P2466+The time of day may omit its seconds, or its minutes and seconds, as in \fB2015\-03\-05T23:51Z\fR and \fB2015\-03\-05T23Z\fR, or be left out altogether, in which case the value is midnight UTC\. It may carry a fractional part after a \fB\.\fR or a \fB,\fR, which belongs to the lowest component present, so \fB23,5\fR is half past 23\. It may be followed by an offset from UTC written as \fBZ\fR, \fB+hh\fR, \fB+hh:mm\fR or \fB+hhmm\fR; a datetime with no offset is read as UTC\. A time of \fB24:00:00\fR denotes midnight of the following day, and a leap second is read as the second before it, since jq counts every day as 86400 seconds\.2467+.2468+.P2469+The \fBW\fR, \fBT\fR and \fBZ\fR designators may also be written in lower case, which ISO 8601 permits where upper case is not available\. RFC 3339 additionally allows the date and the time to be separated by a space instead of a \fBT\fR "for the sake of readability", which jq accepts too\. The offset may use the other representation\'s separator style, as in \fB2015\-03\-05T23:51:47+0900\fR, although ISO 8601 does not allow the basic and extended representations to be mixed\.2470+.2471+.P2472+A complete date is required, so \fB2015\fR and \fB2015\-03\fR are errors even though ISO 8601 defines them as dates, and \fBT23:51:47\fR is an error because a time of day on its own is not a point in time\. Dates that do not exist, such as \fB2015\-02\-30\fR or \fB2016\-W53\-1\fR, are errors too, as is \fB2015\-03\-05Z\fR, since a zone designator belongs to the time of day\.2473+.2474+.P2475+The \fBfromdate\fR builtin is an alias for \fBfromdateiso8601\fR\.2476+.2477+.P2478 The \fBtodate\fR builtin is an alias for \fBtodateiso8601\fR\.2479 .2480 .P2481 The \fBnow\fR builtin outputs the current time, in seconds since the Unix epoch\.16 unmodified lines2498 .P2499 The \fBstrftime(fmt)\fR builtin formats a time (GMT) with the given format\. The \fBstrflocaltime\fR does the same, but using the local timezone setting\.2500 .2501 .P2490-The format strings for \fBstrptime\fR and \fBstrftime\fR are described in typical C library documentation\. The format string for ISO 8601 datetime is \fB"%Y\-%m\-%dT%H:%M:%SZ"\fR\.2502+The format strings for \fBstrptime\fR and \fBstrftime\fR are described in typical C library documentation\. \fB"%Y\-%m\-%dT%H:%M:%SZ"\fR is the format that \fBtodateiso8601\fR produces; it is only one of the ISO 8601 representations that \fBfromdateiso8601\fR accepts, and \fBstrptime\fR cannot parse the others \-\- there is no portable format specifier for week dates or for fractional seconds\.2503 .2504 .P2505 jq may not support some or all of this date functionality on some systems\. In particular, the \fB%u\fR and \fB%j\fR specifiers for \fBstrptime(fmt)\fR are not supported on macOS\.2506 .4 unmodified lines2511 jq \'fromdate\'2512 "2015\-03\-05T23:51:47Z"2513 => 14255995072514 2515+jq \'fromdateiso8601\'2516+ "2015\-03\-06T08:51:47\.25+09:00"2517+=> 1425599507\.252518+2519+jq \'map(fromdateiso8601)\'2520+ ["20150305T235147Z","2015\-064T23:51:47Z","2015\-W10\-4T23:51:47Z"]2521+=> [1425599507,1425599507,1425599507]2522+2523 jq \'strptime("%Y\-%m\-%dT%H:%M:%SZ")\'2524 "2015\-03\-05T23:51:47Z"2525 => [2015,2,5,23,51,47,4,63]2526 1804 unmodified linesmodified:Makefile.am+3 added-2 removed
3 unmodified lines4 LIBJQ_INCS = src/builtin.h src/bytecode.h src/compile.h \5 src/exec_stack.h src/jq_parser.h src/jv_alloc.h src/jv_dtoa.h \6 src/jv_unicode.h src/jv_utf8_tables.h src/lexer.l src/libm.h \7 src/linker.h src/locfile.h src/opcode_list.h src/parser.y \8+ src/util.h src/iso8601.h \8- src/util.h src/jv_dtoa_tsd.h src/jv_thread.h src/jv_private.h \9+ src/jv_dtoa_tsd.h src/jv_thread.h src/jv_private.h \10 vendor/decNumber/decContext.h vendor/decNumber/decNumber.h \11 vendor/decNumber/decNumberLocal.h12 13 LIBJQ_SRC = src/builtin.c src/bytecode.c src/compile.c src/execute.c \14 src/jq_test.c src/jv.c src/jv_alloc.c src/jv_aux.c \15 src/jv_dtoa.c src/jv_file.c src/jv_parse.c src/jv_print.c \16 src/jv_unicode.c src/linker.c src/locfile.c src/util.c \16- src/jv_dtoa_tsd.c \17+ src/iso8601.c src/jv_dtoa_tsd.c \18 vendor/decNumber/decContext.c vendor/decNumber/decNumber.c \19 ${LIBJQ_INCS}20 21 ### C build options242 unmodified lines