]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Use different regexp for extended and basic iso
authorIskren Chernev <iskren.chernev@gmail.com>
Tue, 29 Sep 2015 08:11:40 +0000 (01:11 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 9 Nov 2015 02:27:40 +0000 (18:27 -0800)
src/lib/create/from-string.js
src/test/moment/create.js

index 38bdca17e106f071b006910036e2572533f697d3..6476457e8518fd9a9082b2734f6f1a8199403fda 100644 (file)
@@ -5,36 +5,37 @@ import getParsingFlags from './parsing-flags';
 
 // iso 8601 regex
 // 0000-00-00 0000-W00 or 0000-W00-0 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000 or +00)
-var isoRegex = /^\s*((?:[+-]\d{6}|\d{4})-?(?:\d\d-?\d\d|W\d\d-?\d|W\d\d|\d\d\d|\d\d))(?:(T| )(\d\d(?::?\d\d(?::?\d\d(?:[.,]\d+)?)?)?)([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?/;
+var extendedIsoRegex = /^\s*((?:[+-]\d{6}|\d{4})-(?:\d\d-\d\d|W\d\d-\d|W\d\d|\d\d\d|\d\d))(?:(T| )(\d\d(?::\d\d(?::\d\d(?:[.,]\d+)?)?)?)([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?/;
+var basicIsoRegex = /^\s*((?:[+-]\d{6}|\d{4})(?:\d\d\d\d|W\d\d\d|W\d\d|\d\d\d|\d\d))(?:(T| )(\d\d(?:\d\d(?:\d\d(?:[.,]\d+)?)?)?)([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?/;
 
 var tzRegex = /Z|[+-]\d\d(?::?\d\d)?/;
 
 var isoDates = [
-    ['YYYYYY-MM-DD', /[+-]\d{6}-\d\d-\d\d/, true],
-    ['YYYY-MM-DD', /\d{4}-\d\d-\d\d/, true],
-    ['GGGG-[W]WW-E', /\d{4}-W\d\d-\d/, true],
-    ['GGGG-[W]WW', /\d{4}-W\d\d/, true, false],
-    ['YYYY-DDD', /\d{4}-\d{3}/, true],
-    ['YYYY-MM', /\d{4}-\d\d/, true, false],
-    ['YYYYYYMMDD', /[+-]\d{10}/, false],
-    ['YYYYMMDD', /\d{8}/, false],
+    ['YYYYYY-MM-DD', /[+-]\d{6}-\d\d-\d\d/],
+    ['YYYY-MM-DD', /\d{4}-\d\d-\d\d/],
+    ['GGGG-[W]WW-E', /\d{4}-W\d\d-\d/],
+    ['GGGG-[W]WW', /\d{4}-W\d\d/, false],
+    ['YYYY-DDD', /\d{4}-\d{3}/],
+    ['YYYY-MM', /\d{4}-\d\d/, false],
+    ['YYYYYYMMDD', /[+-]\d{10}/],
+    ['YYYYMMDD', /\d{8}/],
     // YYYYMM is NOT allowed by the standard
-    ['GGGG[W]WWE', /\d{4}W\d{3}/, false],
-    ['GGGG[W]WW', /\d{4}W\d{2}/, false, false],
-    ['YYYYDDD', /\d{7}/, false]
+    ['GGGG[W]WWE', /\d{4}W\d{3}/],
+    ['GGGG[W]WW', /\d{4}W\d{2}/, false],
+    ['YYYYDDD', /\d{7}/]
 ];
 
 // iso time formats and regexes
 var isoTimes = [
-    ['HH:mm:ss.SSSS', /\d\d:\d\d:\d\d\.\d+/, true],
-    ['HH:mm:ss,SSSS', /\d\d:\d\d:\d\d,\d+/, true],
-    ['HH:mm:ss', /\d\d:\d\d:\d\d/, true],
-    ['HH:mm', /\d\d:\d\d/, true],
-    ['HHmmss.SSSS', /\d\d\d\d\d\d\.\d+/, false],
-    ['HHmmss,SSSS', /\d\d\d\d\d\d,\d+/, false],
-    ['HHmmss', /\d\d\d\d\d\d/, false],
-    ['HHmm', /\d\d\d\d/, false],
-    ['HH', /\d\d/, null]
+    ['HH:mm:ss.SSSS', /\d\d:\d\d:\d\d\.\d+/],
+    ['HH:mm:ss,SSSS', /\d\d:\d\d:\d\d,\d+/],
+    ['HH:mm:ss', /\d\d:\d\d:\d\d/],
+    ['HH:mm', /\d\d:\d\d/],
+    ['HHmmss.SSSS', /\d\d\d\d\d\d\.\d+/],
+    ['HHmmss,SSSS', /\d\d\d\d\d\d,\d+/],
+    ['HHmmss', /\d\d\d\d\d\d/],
+    ['HHmm', /\d\d\d\d/],
+    ['HH', /\d\d/]
 ];
 
 var aspNetJsonRegex = /^\/?Date\((\-?\d+)/i;
@@ -43,8 +44,8 @@ var aspNetJsonRegex = /^\/?Date\((\-?\d+)/i;
 export function configFromISO(config) {
     var i, l,
         string = config._i,
-        match = isoRegex.exec(string),
-        extendedDate, extendedTime, allowTime, dateFormat, timeFormat, tzFormat;
+        match = extendedIsoRegex.exec(string) || basicIsoRegex.exec(string),
+        allowTime, dateFormat, timeFormat, tzFormat;
 
     if (match) {
         getParsingFlags(config).iso = true;
@@ -52,8 +53,7 @@ export function configFromISO(config) {
         for (i = 0, l = isoDates.length; i < l; i++) {
             if (isoDates[i][1].exec(match[1])) {
                 dateFormat = isoDates[i][0];
-                extendedDate = isoDates[i][2];
-                allowTime = isoDates[i][3] !== false;
+                allowTime = isoDates[i][2] !== false;
                 break;
             }
         }
@@ -66,7 +66,6 @@ export function configFromISO(config) {
                 if (isoTimes[i][1].exec(match[3])) {
                     // match[2] should be 'T' or space
                     timeFormat = (match[2] || ' ') + isoTimes[i][0];
-                    extendedTime = isoTimes[i][2];
                     break;
                 }
             }
@@ -79,13 +78,6 @@ export function configFromISO(config) {
             config._isValid = false;
             return;
         }
-        if (extendedDate != null &&
-                extendedTime != null &&
-                extendedDate !== extendedTime) {
-            // extended and basic formats for date and time can NOT be mixed
-            config._isValid = false;
-            return;
-        }
         if (match[4] != null) {
             if (tzRegex.exec(match[4])) {
                 tzFormat = 'Z';
index ad9757d48c5fdc73cff6d8ccefe6765391f33727..ac48dba317a4409da7bf0cfd7139a35396374e5d 100644 (file)
@@ -583,12 +583,12 @@ test('parsing iso', function (assert) {
 });
 
 test('non iso 8601 strings', function (assert) {
-    assert.ok(!moment('2015-10T10:15', moment.ISO_8601).isValid(), 'incomplete date with time');
-    assert.ok(!moment('2015-W10T10:15', moment.ISO_8601).isValid(), 'incomplete week date with time');
-    assert.ok(!moment('201510', moment.ISO_8601).isValid(), 'basic YYYYMM is not allowed');
-    assert.ok(!moment('2015W10T1015', moment.ISO_8601).isValid(), 'incomplete week date with time (basic)');
-    assert.ok(!moment('2015-10-08T1015', moment.ISO_8601).isValid(), 'mixing extended and basic format');
-    assert.ok(!moment('20151008T10:15', moment.ISO_8601).isValid(), 'mixing basic and extended format');
+    assert.ok(!moment('2015-10T10:15', moment.ISO_8601, true).isValid(), 'incomplete date with time');
+    assert.ok(!moment('2015-W10T10:15', moment.ISO_8601, true).isValid(), 'incomplete week date with time');
+    assert.ok(!moment('201510', moment.ISO_8601, true).isValid(), 'basic YYYYMM is not allowed');
+    assert.ok(!moment('2015W10T1015', moment.ISO_8601, true).isValid(), 'incomplete week date with time (basic)');
+    assert.ok(!moment('2015-10-08T1015', moment.ISO_8601, true).isValid(), 'mixing extended and basic format');
+    assert.ok(!moment('20151008T10:15', moment.ISO_8601, true).isValid(), 'mixing basic and extended format');
 });
 
 test('parsing iso week year/week/weekday', function (assert) {