]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
#371 don't add timezone to string + format in moment.utc if it already has a timezone
authorTim Wood <washwithcare@gmail.com>
Wed, 25 Jul 2012 17:12:53 +0000 (10:12 -0700)
committerTim Wood <washwithcare@gmail.com>
Wed, 25 Jul 2012 17:12:53 +0000 (10:12 -0700)
moment.js
test/moment/create.js

index c959ab31debf2f52053d38f3f31e6ef8bde6d7a8..78270bf9148cc42a0965a4d1246773a260b7b973 100644 (file)
--- a/moment.js
+++ b/moment.js
         if (isArray(input)) {
             return new Moment(dateFromArray(input, true), true);
         }
-        return (format && input) ?
-            moment(input + ' +0000', format + ' Z').utc() :
-            moment(input && isoRegex.exec(input) && !parseTokenTimezone.exec(input) ? input + '+0000' : input).utc();
+        // if we don't have a timezone, we need to add one to trigger parsing into utc
+        if (typeof input === 'string' && !parseTokenTimezone.exec(input)) {
+            input += ' +0000';
+            if (format) {
+                format += ' Z';
+            }
+        }
+        return moment(input, format).utc();
     };
 
     // creating with unix timestamp (in seconds)
index a6686ae9bc1ce1624ac88e33bd36c623ddf58d1c..bb58952c5d74f9de2987fb029a81b18d175cee78 100644 (file)
@@ -171,7 +171,7 @@ exports.create = {
     },
 
     "string with format (timezone offset)" : function(test) {
-        test.expect(3);
+        test.expect(4);
         var a = new Date(Date.UTC(2011, 0, 1, 1));
         var b = moment('2011 1 1 0 -01:00', 'YYYY MM DD HH Z');
         test.equal(a.getHours(), b.hours(), 'date created with utc == parsed string with timezone offset');
@@ -179,6 +179,9 @@ exports.create = {
         var c = moment('2011 2 1 10 -05:00', 'YYYY MM DD HH Z');
         var d = moment('2011 2 1 8 -07:00', 'YYYY MM DD HH Z');
         test.equal(c.hours(), d.hours(), '10 am central time == 8 am pacific time');
+        var e = moment.utc('Fri, 20 Jul 2012 17:15:00', 'ddd, DD MMM YYYY HH:mm:ss');
+        var f = moment.utc('Fri, 20 Jul 2012 10:15:00 -0700', 'ddd, DD MMM YYYY HH:mm:ss ZZ');
+        test.equal(e.hours(), f.hours(), 'parse timezone offset in utc');
         test.done();
     },