This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Fix lax parsing of seconds and timezone offsets; fix bug that allowed…

… trailing hyphen in YYYY-MM style (thanks to reichstein)
  • Loading branch information...
csnover committed Jul 1, 2011
1 parent 2861589 commit d0d8c37028435e1cd72205d6ab2610ba90cff166
Showing with 14 additions and 7 deletions.
  1. +2 −2 iso8601.js
  2. +1 −1 iso8601.min.js
  3. +11 −4 tests/test.js
View
@@ -11,8 +11,8 @@
// ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
// before falling back to any implementation-specific date parsing, so that’s what we do, even if native
// implementations could be faster
- // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
- if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2})?)?)?(?:T(\d{2}):?(\d{2})(?::?(\d{2})(?:[,.](\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?)?)?$/.exec(date))) {
+ // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
+ if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
// avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
for (var i = 0, k; (k = numericKeys[i]); ++i) {
struct[k] = +struct[k] || 0;
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,7 +1,7 @@
var sixHours = 6 * 60 * 60 * 1000,
sixHoursThirty = sixHours + 30 * 60 * 1000;
-test('date-part', 13, function () {
+test('date-part', 16, function () {
strictEqual(Date.parse('1970-01-01'), Date.UTC(1970, 0, 1, 0, 0, 0, 0), 'Unix epoch');
strictEqual(Date.parse('2001'), Date.UTC(2001, 0, 1, 0, 0, 0, 0), '2001');
@@ -23,13 +23,16 @@ test('date-part', 13, function () {
strictEqual(Date.parse('0099-12-31T00:00Z'), Date.UTC(100, 0, 1, 0, 0, 0, 0) - 1000 * 60 * 60 * 24, '0099-12-31T00:00Z');
*/
- ok(isNaN(Date.parse('asdf')), 'invalid YYYY (asdf)');
- ok(isNaN(Date.parse('1970-as-df')), 'invalid YYYY-MM-DD (1970-as-df)');
+ ok(isNaN(Date.parse('asdf')), 'invalid YYYY (non-digits)');
+ ok(isNaN(Date.parse('1970-as-df')), 'invalid YYYY-MM-DD (non-digits)');
+ ok(isNaN(Date.parse('1970-01-')), 'invalid YYYY-MM- (extra hyphen)');
+ ok(isNaN(Date.parse('19700101')), 'invalid YYYY-MM-DD (missing hyphens)');
+ ok(isNaN(Date.parse('197001')), 'ambiguous YYYY-MM/YYYYYY (missing plus/minus or hyphen)');
// TODO: Test for invalid YYYYMM and invalid YYYYY?
});
-test('date-time', 27, function () {
+test('date-time', 31, function () {
strictEqual(Date.parse('2001-02-03T04:05'), Date.UTC(2001, 1, 3, 4, 5, 0, 0), '2001-02-03T04:05');
strictEqual(Date.parse('2001-02-03T04:05:06'), Date.UTC(2001, 1, 3, 4, 5, 6, 0), '2001-02-03T04:05:06');
strictEqual(Date.parse('2001-02-03T04:05:06.007'), Date.UTC(2001, 1, 3, 4, 5, 6, 7), '2001-02-03T04:05:06.007');
@@ -65,6 +68,10 @@ test('date-time', 27, function () {
ok(isNaN(Date.parse('1970-01-01 00:00:00')), 'invalid date-time (missing T)');
ok(isNaN(Date.parse('1970-01-01T00:00:00.000000')), 'invalid date-time (too many characters in millisecond part)');
+ ok(isNaN(Date.parse('1970-01-01T00:00:00,000')), 'invalid date-time (comma instead of dot)');
+ ok(isNaN(Date.parse('1970-01-01T00:00:00+0630')), 'invalid date-time (missing colon in timezone part)');
+ ok(isNaN(Date.parse('1970-01-01T0000')), 'invalid date-time (missing colon in time part)');
+ ok(isNaN(Date.parse('1970-01-01T00:00.000')), 'invalid date-time (msec with missing seconds)');
// TODO: DRY
});

0 comments on commit d0d8c37

Please sign in to comment.