From: Tim Wood Date: Fri, 8 Mar 2013 03:42:37 +0000 (-0800) Subject: adding tests for diff and from X-Git-Tag: 2.1.0~32^2~2^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e1ca899c1aec2cf4000f821e6fa0db0f16fde539;p=thirdparty%2Fmoment.git adding tests for diff and from --- diff --git a/moment.js b/moment.js index 654d41c84..3e7bee165 100644 --- a/moment.js +++ b/moment.js @@ -1134,7 +1134,7 @@ }, 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; @@ -1261,6 +1261,7 @@ } else { return this._isUTC ? offset : this._d.getTimezoneOffset(); } + return this; }, daysInMonth : function () { diff --git a/test/moment/utc.js b/test/moment/utc.js index 60457f276..a064ee02b 100644 --- a/test/moment/utc.js +++ b/test/moment/utc.js @@ -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); diff --git a/test/moment/zones.js b/test/moment/zones.js index 504b23d39..0644949d1 100644 --- a/test/moment/zones.js +++ b/test/moment/zones.js @@ -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(); }