]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
adding tests for parsing strings for timezone info
authorTim Wood <washwithcare@gmail.com>
Sat, 9 Mar 2013 21:11:59 +0000 (13:11 -0800)
committerTim Wood <washwithcare@gmail.com>
Sat, 9 Mar 2013 21:11:59 +0000 (13:11 -0800)
moment.js
test/moment/zones.js

index 5be161c0b15fa0b58bfb39409174e83fc5425b63..551fba071824de683d6071481a26fe65d8dae55f 100644 (file)
--- a/moment.js
+++ b/moment.js
         }
     }
 
+    function timezoneMinutesFromString(string) {
+        var tzchunk = (parseTokenTimezone.exec(string) || [])[0],
+            parts = (tzchunk + '').match(parseTimezoneChunker) || ['-', 0, 0],
+            minutes = +(parts[1] * 60) + ~~parts[2];
+
+        return parts[0] === '+' ? -minutes : minutes;
+    }
+
     // function to convert string input to date
     function addTimeToArrayFromToken(token, input, config) {
         var a, b,
         case 'Z' : // fall through to ZZ
         case 'ZZ' :
             config._useUTC = true;
-            a = (input + '').match(parseTimezoneChunker);
-            if (a && a[1]) {
-                config._tzh = ~~a[1];
-            }
-            if (a && a[2]) {
-                config._tzm = ~~a[2];
-            }
-            // reverse offsets
-            if (a && a[0] === '+') {
-                config._tzh = -config._tzh;
-                config._tzm = -config._tzm;
-            }
+            config._tzm = timezoneMinutesFromString(input);
             break;
         }
 
         }
 
         // add the offsets to the time to be parsed so that we can have a clean array for checking isValid
-        input[3] += config._tzh || 0;
-        input[4] += config._tzm || 0;
+        input[3] += ~~((config._tzm || 0) / 60);
+        input[4] += ~~((config._tzm || 0) % 60);
 
         date = new Date(0);
 
         zone : function (input) {
             var offset = this._offset || 0;
             if (input != null) {
+                if (typeof input === "string") {
+                    input = timezoneMinutesFromString(input);
+                }
                 this._offset = input;
                 this._isUTC = true;
                 if (offset !== input) {
index 622d94bbc60258518db0e4001b06962a6ce8ea68..7cb2b3a6de2e2b33d23c394d063da6a2ee1447c4 100644 (file)
@@ -26,6 +26,21 @@ exports.zones = {
         test.done();
     },
 
+    "set zone with string" : function (test) {
+        var zone = moment();
+
+        zone.zone("+00:00");
+        test.equal(zone.zone(), 0, "set the zone with a timezone string");
+
+        zone.zone("2013-03-07T07:00:00-08:00");
+        test.equal(zone.zone(), 480, "set the zone with a string that does not begin with the timezone");
+
+        zone.zone("2013-03-07T07:00:00+0100");
+        test.equal(zone.zone(), -60, "set the zone with a string that uses the +0000 syntax");
+
+        test.done();
+    },
+
     "change hours when changing the zone" : function (test) {
         var zone = moment.utc([2000, 0, 1, 6]);