]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix isSame for differently zoned moments
authorIskren Chernev <iskren.chernev@gmail.com>
Mon, 23 Dec 2013 06:07:00 +0000 (22:07 -0800)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 23 Dec 2013 06:07:00 +0000 (22:07 -0800)
isSame uses startOf and then compares milliseconds, but this misses the case of
different zones across the moments. startOf('day') in one timezone is different
than the other, so it was never successful.

moment.js
test/moment/is_same.js

index b97dff98b968d9c6cd558f098cf89ea278d9485c..c29b111b787e2e66c9a35960349c034d950dbae2 100644 (file)
--- a/moment.js
+++ b/moment.js
         return key ? key.toLowerCase().replace('_', '-') : key;
     }
 
+    // Return a moment from input, that is local/utc/zone equivalent to model.
+    function makeAs(input, model) {
+        return model._isUTC ? moment(input).zone(model._offset || 0) :
+            moment(input).local();
+    }
+
     /************************************
         Languages
     ************************************/
         },
 
         diff : function (input, units, asFloat) {
-            var that = this._isUTC ? moment(input).zone(this._offset || 0) : moment(input).local(),
+            var that = makeAs(input, this),
                 zoneDiff = (this.zone() - that.zone()) * 6e4,
                 diff, output;
 
         },
 
         isSame: function (input, units) {
-            units = typeof units !== 'undefined' ? units : 'millisecond';
-            return +this.clone().startOf(units) === +moment(input).startOf(units);
+            units = units || 'ms';
+            return +this.clone().startOf(units) === +makeAs(input, this).startOf(units);
         },
 
         min: function (other) {
index 108141669c32284b5be312069e55ba4af1f4817c..71fc25bb91f84de16886176ea16513cd11e2bebe 100644 (file)
@@ -159,5 +159,14 @@ exports.is_same = {
         test.equal(m.isSame(m, 'millisecond'), true, "same moments are in the same millisecond");
         test.equal(+m, +mCopy, "isSame millisecond should not change moment");
         test.done();
+    },
+
+    "is same with zone'd moments" : function (test) {
+        test.expect(3);
+        test.ok(moment.parseZone('2013-01-01T-05:00').isSame(moment('2013-01-01'), 'year'), "zoned vs local moment");
+        test.ok(moment('2013-01-01').isSame(moment('2013-01-01').zone('-05:00'), 'year'), "local vs zoned moment");
+        test.ok(moment.parseZone('2013-01-01T-05:00').isSame(moment.parseZone('2013-01-01T-06:30'), 'year'),
+                "zoned vs (differently) zoned moment");
+        test.done();
     }
 };