]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
adding tests for diff and from
authorTim Wood <washwithcare@gmail.com>
Fri, 8 Mar 2013 03:42:37 +0000 (19:42 -0800)
committerTim Wood <washwithcare@gmail.com>
Fri, 8 Mar 2013 03:42:37 +0000 (19:42 -0800)
moment.js
test/moment/utc.js
test/moment/zones.js

index 654d41c84662a4ac6b244abbfb6c3b62a78369a0..3e7bee16520b669ce3f757ad9c60bc4cd1768ee7 100644 (file)
--- a/moment.js
+++ b/moment.js
         },
 
         diff : function (input, units, asFloat) {
-            var that = this._isUTC ? moment(input).utc() : moment(input).local(),
+            var that = this._isUTC ? moment(input).zone(this._offset || 0) : moment(input).local(),
                 zoneDiff = (this.zone() - that.zone()) * 6e4,
                 diff, output;
 
             } else {
                 return this._isUTC ? offset : this._d.getTimezoneOffset();
             }
+            return this;
         },
 
         daysInMonth : function () {
index 60457f2768e2a6d8518136fd45cc3399a4e69fe8..a064ee02b88b22f6bb0c1ad24ae47d73fb5ef77f 100644 (file)
@@ -1,6 +1,16 @@
 var moment = require("../../moment");
 
 exports.utc = {
+    setUp : function (cb) {
+        moment.lang('en');
+        cb();
+    },
+
+    tearDown : function (cb) {
+        moment.lang('en');
+        cb();
+    },
+
     "utc and local" : function(test) {
         test.expect(7);
 
index 504b23d392c25c321a2985a2838f86a6c581ea20..0644949d15472c2d03525e9576a819e8e0418bc4 100644 (file)
@@ -104,6 +104,79 @@ exports.zones = {
 
         moment.updateOffset = oldOffset;
 
+        test.done();
+    },
+
+    "getters and setters" : function (test) {
+        var a = moment([2011]);
+
+        test.equal(a.clone().zone(120).year(2012).year(), 2012, "should get and set year correctly");
+        test.equal(a.clone().zone(120).month(1).month(), 1, "should get and set month correctly");
+        test.equal(a.clone().zone(120).date(2).date(), 2, "should get and set date correctly");
+        test.equal(a.clone().zone(120).day(1).day(), 1, "should get and set day correctly");
+        test.equal(a.clone().zone(120).hour(1).hour(), 1, "should get and set hour correctly");
+        test.equal(a.clone().zone(120).minute(1).minute(), 1, "should get and set minute correctly");
+
+        test.done();
+    },
+
+    "getters" : function (test) {
+        var a = moment.utc([2012, 0, 1, 0, 0, 0]);
+
+        test.equal(a.clone().zone(120).year(),  2011, "should get year correctly");
+        test.equal(a.clone().zone(120).month(),   11, "should get month correctly");
+        test.equal(a.clone().zone(120).date(),    31, "should get date correctly");
+        test.equal(a.clone().zone(120).hour(),    22, "should get hour correctly");
+        test.equal(a.clone().zone(120).minute(),   0, "should get minute correctly");
+
+        test.equal(a.clone().zone(-120).year(),  2012, "should get year correctly");
+        test.equal(a.clone().zone(-120).month(),    0, "should get month correctly");
+        test.equal(a.clone().zone(-120).date(),     1, "should get date correctly");
+        test.equal(a.clone().zone(-120).hour(),     2, "should get hour correctly");
+        test.equal(a.clone().zone(-120).minute(),   0, "should get minute correctly");
+
+        test.equal(a.clone().zone(-90).year(),  2012, "should get year correctly");
+        test.equal(a.clone().zone(-90).month(),    0, "should get month correctly");
+        test.equal(a.clone().zone(-90).date(),     1, "should get date correctly");
+        test.equal(a.clone().zone(-90).hour(),     1, "should get hour correctly");
+        test.equal(a.clone().zone(-90).minute(),  30, "should get minute correctly");
+
+        test.done();
+    },
+
+    "from" : function (test) {
+        var zoneA = moment(),
+            zoneB = moment(zoneA).zone(720),
+            zoneC = moment(zoneA).zone(360),
+            zoneD = moment(zoneA).zone(-690),
+            other = moment(zoneA).add('m', 35);
+
+        test.equal(zoneA.from(other), zoneB.from(other), "moment#from should be the same in all zones");
+        test.equal(zoneA.from(other), zoneC.from(other), "moment#from should be the same in all zones");
+        test.equal(zoneA.from(other), zoneD.from(other), "moment#from should be the same in all zones");
+
+        test.done();
+    },
+
+    "diff" : function (test) {
+        var zoneA = moment(),
+            zoneB = moment(zoneA).zone(720),
+            zoneC = moment(zoneA).zone(360),
+            zoneD = moment(zoneA).zone(-690),
+            other = moment(zoneA).add('m', 35);
+
+        test.equal(zoneA.diff(other), zoneB.diff(other), "moment#diff should be the same in all zones");
+        test.equal(zoneA.diff(other), zoneC.diff(other), "moment#diff should be the same in all zones");
+        test.equal(zoneA.diff(other), zoneD.diff(other), "moment#diff should be the same in all zones");
+
+        test.equal(zoneA.diff(other, 'minute', true), zoneB.diff(other, 'minute', true), "moment#diff should be the same in all zones");
+        test.equal(zoneA.diff(other, 'minute', true), zoneC.diff(other, 'minute', true), "moment#diff should be the same in all zones");
+        test.equal(zoneA.diff(other, 'minute', true), zoneD.diff(other, 'minute', true), "moment#diff should be the same in all zones");
+
+        test.equal(zoneA.diff(other, 'hour', true), zoneB.diff(other, 'hour', true), "moment#diff should be the same in all zones");
+        test.equal(zoneA.diff(other, 'hour', true), zoneC.diff(other, 'hour', true), "moment#diff should be the same in all zones");
+        test.equal(zoneA.diff(other, 'hour', true), zoneD.diff(other, 'hour', true), "moment#diff should be the same in all zones");
+
         test.done();
     }