Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit d0d8c37028435e1cd72205d6ab2610ba90cff166 1 parent 2861589
@csnover authored
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
4 iso8601.js
@@ -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
2  iso8601.min.js
@@ -1 +1 @@
-/** https://github.com/csnover/js-iso8601 */(function(a,b){var c=a.parse,d=[1,4,5,6,7,10,11];a.parse=function(e){var f,g,h=0;if(g=/^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2})?)?)?(?:T(\d{2}):?(\d{2})(?::?(\d{2})(?:[,.](\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?)?)?$/.exec(e)){for(var i=0,j;j=d[i];++i)g[j]=+g[j]||0;g[2]=(+g[2]||1)-1,g[3]=+g[3]||1,g[8]!=="Z"&&g[9]!==b&&(h=g[10]*60+g[11],g[9]==="+"&&(h=0-h)),f=a.UTC(g[1],g[2],g[3],g[4],g[5]+h,g[6],g[7])}else f=c?c(e):NaN;return f}})(Date)
+/** https://github.com/csnover/js-iso8601 */(function(n,f){var u=n.parse,c=[1,4,5,6,7,10,11];n.parse=function(t){var i,o,a=0;if(o=/^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(t)){for(var v=0,r;r=c[v];++v)o[r]=+o[r]||0;o[2]=(+o[2]||1)-1,o[3]=+o[3]||1,o[8]!=="Z"&&o[9]!==f&&(a=o[10]*60+o[11],o[9]==="+"&&(a=0-a)),i=n.UTC(o[1],o[2],o[3],o[4],o[5]+a,o[6],o[7])}else i=u?u(t):NaN;return i}})(Date)
View
15 tests/test.js
@@ -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
});
Please sign in to comment.
Something went wrong with that request. Please try again.