Skip to content

Commit 4736fd8

Browse files
committed
Add support for space or lowercase t for date time delimiter (RFC 9557 valid)
1 parent 2e2d0fc commit 4736fd8

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class IsoDateParts {
66
};
77

88
static FULL_DATE_REGEX = /^([+-]\d{6}|\d{4})-?([01]\d)-?([0-3]\d)$/;
9-
static DATETIME_REGEX = /^([+-]\d{6}|\d{4})-?([01]\d)-?([0-3]\d)T([0-2]\d(?:[\.\,]\d+)?)(?::?([0-5]\d(?:[\.\,]\d+)?)(?::?([0-5]\d))?(?:[\.\,](\d+))?)?(Z|[+-][0-2]\d(?::?[0-5]\d)?)?$/;
9+
static DATETIME_REGEX = /^([+-]\d{6}|\d{4})-?([01]\d)-?([0-3]\d)[Tt ]([0-2]\d(?:[\.\,]\d+)?)(?::?([0-5]\d(?:[\.\,]\d+)?)(?::?([0-5]\d))?(?:[\.\,](\d+))?)?(Z|[+-][0-2]\d(?::?[0-5]\d)?)?$/;
1010
static TIMEZONE_REGEX = /^([+-]\d{2})(?::?(\d{2}))?$/;
1111
static IS_FRACTIONAL_REGEX = /^\d+[\.\,]\d+$/;
1212

parse.test.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IsoDate, parse } from "./parse.js";
44
import { parse as temporalParse } from "./test/temporal.js";
55

66
// This test suite compares with Luxon output for maximum backwards compatibility
7-
import { shouldSkip, VALID_TEST_CASES, INVALID_TEST_CASES, SUPPLIED_TEST_CASES } from './test/utils.js';
7+
import { shouldSkip, VALID_TEST_CASES, VALID_BUT_INVALID_IN_LUXON_TEST_CASES, INVALID_TEST_CASES, SUPPLIED_TEST_CASES } from './test/utils.js';
88

99
// Some test cases from https://moment.github.io/luxon/#/parsing?id=ad-hoc-parsing
1010
// ISO8601 date parsing https://github.com/11ty/eleventy/issues/3587
@@ -17,10 +17,26 @@ for(let line of VALID_TEST_CASES.split("\n")) {
1717
// assert.equal(received, expected)
1818

1919
// Compare to luxon
20-
assert.equal(parse(line).toUTCString(), DateTime.fromISO(line, {zone: "utc"}).toJSDate().toUTCString());
20+
assert.equal(parse(line).toUTCString(), DateTime.fromISO(line, {zone: "utc"}).toJSDate().toUTCString(), `Invalid compared to luxon for '${line}'`);
2121

2222
// Compare to Temporal
23-
assert.equal(parse(line).toUTCString(), temporalParse(line).toString());
23+
assert.equal(parse(line).toUTCString(), temporalParse(line).toString(), `Invalid compared to Temporal for '${line}'`);
24+
});
25+
}
26+
27+
for(let line of VALID_BUT_INVALID_IN_LUXON_TEST_CASES.split("\n")) {
28+
if(shouldSkip(line)) {
29+
continue;
30+
}
31+
32+
test(`Parse ${line}`, () => {
33+
// assert.equal(received, expected)
34+
35+
// Should not equal luxon
36+
assert.notEqual(parse(line).toUTCString(), DateTime.fromISO(line, {zone: "utc"}).toJSDate().toUTCString(), `Should not match luxon for '${line}'`);
37+
38+
// Compare to Temporal
39+
assert.equal(parse(line).toUTCString(), temporalParse(line).toString(), `Invalid compared to Temporal for '${line}'`);
2440
});
2541
}
2642

test/utils.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ export const VALID_TEST_CASES = `
8181
-002000-01-01
8282
+0020000101
8383
-0020000101
84+
85+
// case insensitive delimiter (RFC 9557-valid)
86+
2016-05-25t12:00:00
87+
`;
88+
89+
export const VALID_BUT_INVALID_IN_LUXON_TEST_CASES = `
90+
// space delimiter (RFC 9557-valid)
91+
2016-05-25 12:00:00
8492
`
8593

8694
// Expected to fail
@@ -96,13 +104,9 @@ export const INVALID_TEST_CASES = `
96104
// Support removed (missing delimiters)
97105
201605
98106
202618
107+
2016-05-2501
99108
2016-05-25T01:
100109
101-
// Bad date/time delimiter
102-
// These are supported by RFC 9337
103-
2016-05-25 12:00:00
104-
2016-05-25t12:00:00
105-
106110
// Bad syntax, number of time digits
107111
2016-05-25T1:11
108112
2016-05-25T1:1:1

0 commit comments

Comments
 (0)