]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added automatic iso parsing
authorTim Wood <washwithcare@gmail.com>
Mon, 20 Feb 2012 20:40:31 +0000 (12:40 -0800)
committerTim Wood <washwithcare@gmail.com>
Mon, 20 Feb 2012 20:40:57 +0000 (12:40 -0800)
#153

moment.js
test/moment/create.js

index 989295a69ffae58024a20b237516a04bafe106c8..c868e5df8901bff25d80e83698feb9bfccfaa251 100644 (file)
--- a/moment.js
+++ b/moment.js
         timezoneRegex = /\([A-Za-z ]+\)|:[0-9]{2} [A-Z]{3} /g,
         tokenCharacters = /(\\)?(MM?M?M?|dd?d?d|DD?D?D?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|ZZ?|T)/g,
         inputCharacters = /(\\)?([0-9]+|([a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+|([\+\-]\d\d:?\d\d))/gi,
+        isoRegex = /\d{4}.\d\d.\d\d(T(\d\d(.\d\d(.\d\d)?)?)?([\+\-]\d\d:?\d\d)?)?/,
         isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',
+        isoTimes = [
+            ['HH:mm:ss', /T\d\d:\d\d:\d\d/],
+            ['HH:mm', /T\d\d:\d\d/],
+            ['HH', /T\d\d/]
+        ],
         timezoneParseRegex = /([\+\-]|\d\d)/gi,
         VERSION = "1.4.0",
         shortcuts = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|');
             isUsingUTC = false,
             inputParts = string.match(inputCharacters),
             formatParts = format.match(tokenCharacters),
+            len = Math.min(inputParts.length, formatParts.length),
             i,
             isPm;
 
                 break;
             }
         }
-        for (i = 0; i < formatParts.length; i++) {
+        for (i = 0; i < len; i++) {
             addTime(formatParts[i], inputParts[i]);
         }
         // handle am pm
         return output;
     }
 
+    // date from iso format
+    function makeDateFromString(string) {
+        var format = 'YYYY-MM-DDT',
+            i;
+        if (isoRegex.exec(string)) {
+            for (i = 0; i < 3; i++) {
+                if (isoTimes[i][1].exec(string)) {
+                    format += isoTimes[i][0];
+                    break;
+                }
+            }
+            return makeDateFromStringAndFormat(string, format + 'Z');
+        }
+        return new Date(string);
+    }
+
     moment = function (input, format) {
         if (input === null) {
             return null;
                 matched ? new Date(+matched[1]) :
                 input instanceof Date ? input :
                 isArray(input) ? dateFromArray(input) :
+                typeof input === 'string' ? makeDateFromString(input) :
                 new Date(input);
         }
         return new Moment(date);
                 d : 1,
                 ms : -1
             });
+        },
+
+        zone : function () {
+            return this._d.getTimezoneOffset();
         }
     };
 
     // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear')
     makeShortcut('year', 'FullYear');
 
-    // add shortcut for timezone offset (no setter)
-    moment.fn.zone = function () {
-        return this._d.getTimezoneOffset();
-    };
-
     // CommonJS module is defined
     if (hasModule) {
         module.exports = moment;
index c8a0deb109ed2dba37ac518334bfe87b7bb07cec..4d46f4d2c77f430dae02ce6250d96d1b77d69a9f 100644 (file)
@@ -151,4 +151,33 @@ exports.create = {
         test.equal(momentA.month(), 5, "Calling moment() on a moment will create a clone");
         test.done();
     },
+
+    "parsing iso" : function(test) {
+        var offset = moment([2011, 9, 08]).zone();
+        var pad = function(input) {
+            if (input < 10) {
+                return '0' + input;
+            }
+            return '' + input;
+        }
+        var hourOffset = Math.floor(offset / 60);
+        var minOffset = offset - (hourOffset * 60);
+        var tz = (offset > 0) ? '-' + pad(hourOffset) + ':' + pad(minOffset) : '+' + pad(-hourOffset) + ':' + pad(-minOffset);
+        var tz2 = tz.replace(':', '');
+        var formats = [
+            ['2011-10-08',                '2011-10-08T00:00:00' + tz],
+            ['2011-10-08T18',             '2011-10-08T18:00:00' + tz],
+            ['2011-10-08T18:04',          '2011-10-08T18:04:00' + tz],
+            ['2011-10-08T18:04:20',       '2011-10-08T18:04:20' + tz],
+            ['2011-10-08T18:04' + tz,     '2011-10-08T18:04:00' + tz],
+            ['2011-10-08T18:04:20' + tz,  '2011-10-08T18:04:20' + tz],
+            ['2011-10-08T18:04' + tz2,    '2011-10-08T18:04:00' + tz],
+            ['2011-10-08T18:04:20' + tz2, '2011-10-08T18:04:20' + tz],
+        ];
+        test.expect(formats.length);
+        for (var i = 0; i < formats.length; i++) {
+            test.equal(moment(formats[i][0]).format('YYYY-MM-DDTHH:mm:ssZ'), formats[i][1], "moment should be able to parse ISO " + formats[i][0]);
+        }
+        test.done();
+    }
 };