From: Iskren Chernev Date: Sun, 23 Nov 2014 09:26:45 +0000 (-0800) Subject: Implement moment.fn.utcOffset, base moment.fn.zone on top X-Git-Tag: 2.9.0~23^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=563b2ef72cbb020c8f363ba248aecedc8887b3e6;p=thirdparty%2Fmoment.git Implement moment.fn.utcOffset, base moment.fn.zone on top --- diff --git a/moment.js b/moment.js index 1fc621092..8788830f7 100644 --- a/moment.js +++ b/moment.js @@ -89,7 +89,7 @@ ['HH', /(T| )\d\d/] ], - // timezone chunker '+10:00' > ['10', '00'] or '-1530' > ['-15', '30'] + // timezone chunker '+10:00' > ['10', '00'] or '-1530' > ['-', '15', '30'] parseTimezoneChunker = /([\+\-]|\d\d)/gi, // getter and setter names @@ -249,7 +249,7 @@ return leftZeroFill(this.milliseconds(), 3); }, Z : function () { - var a = -this.zone(), + var a = this.utcOffset(), b = '+'; if (a < 0) { a = -a; @@ -258,7 +258,7 @@ return b + leftZeroFill(toInt(a / 60), 2) + ':' + leftZeroFill(toInt(a) % 60, 2); }, ZZ : function () { - var a = -this.zone(), + var a = this.utcOffset(), b = '+'; if (a < 0) { a = -a; @@ -1161,14 +1161,14 @@ } } - function timezoneMinutesFromString(string) { + function utcOffsetFromString(string) { string = string || ''; var possibleTzMatches = (string.match(parseTokenTimezone) || []), tzChunk = possibleTzMatches[possibleTzMatches.length - 1] || [], parts = (tzChunk + '').match(parseTimezoneChunker) || ['-', 0, 0], minutes = +(parts[1] * 60) + toInt(parts[2]); - return parts[0] === '+' ? -minutes : minutes; + return parts[0] === '+' ? minutes : -minutes; } // function to convert string input to date @@ -1272,7 +1272,7 @@ case 'Z' : // fall through to ZZ case 'ZZ' : config._useUTC = true; - config._tzm = timezoneMinutesFromString(input); + config._tzm = utcOffsetFromString(input); break; // WEEKDAY - human case 'dd': @@ -1413,7 +1413,7 @@ // Apply timezone offset from input. The actual zone can be changed // with parseZone. if (config._tzm != null) { - config._d.setUTCMinutes(config._d.getUTCMinutes() + config._tzm); + config._d.setUTCMinutes(config._d.getUTCMinutes() - config._tzm); } if (config._nextDay) { @@ -2134,7 +2134,7 @@ }, valueOf : function () { - return +this._d + ((this._offset || 0) * 60000); + return +this._d - ((this._offset || 0) * 60000); }, unix : function () { @@ -2197,16 +2197,16 @@ }, utc : function (keepLocalTime) { - return this.zone(0, keepLocalTime); + return this.utcOffset(0, keepLocalTime); }, local : function (keepLocalTime) { if (this._isUTC) { - this.zone(0, keepLocalTime); + this.utcOffset(0, keepLocalTime); this._isUTC = false; if (keepLocalTime) { - this.add(this._dateTzOffset(), 'm'); + this.subtract(this._dateUtcOffset(), 'm'); } } return this; @@ -2223,7 +2223,7 @@ diff : function (input, units, asFloat) { var that = makeAs(input, this), - zoneDiff = (this.zone() - that.zone()) * 6e4, + zoneDiff = (that.utcOffset() - this.utcOffset()) * 6e4, diff, output, daysAdjust; units = normalizeUnits(units); @@ -2238,8 +2238,8 @@ daysAdjust = (this - moment(this).startOf('month')) - (that - moment(that).startOf('month')); // same as above but with zones, to negate all dst - daysAdjust -= ((this.zone() - moment(this).startOf('month').zone()) - - (that.zone() - moment(that).startOf('month').zone())) * 6e4; + daysAdjust += ((this.utcOffset() - moment(this).startOf('month').utcOffset()) - + (that.utcOffset() - moment(that).startOf('month').utcOffset())) * 6e4; output += daysAdjust / diff; if (units === 'year') { output = output / 12; @@ -2284,8 +2284,8 @@ }, isDST : function () { - return (this.zone() < this.clone().month(0).zone() || - this.zone() < this.clone().month(5).zone()); + return (this.utcOffset() > this.clone().month(0).utcOffset() || + this.utcOffset() > this.clone().month(5).utcOffset()); }, day : function (input) { @@ -2407,6 +2407,21 @@ } ), + // TODO: Deprecate + zone : function (input, keepLocalTime) { + if (input != null) { + if (typeof input !== 'string') { + input = -input; + } + + this.utcOffset(input, keepLocalTime); + + return this; + } else { + return -this.utcOffset(); + } + }, + // keepLocalTime = true means only change the timezone, without // affecting the local hour. So 5:31:26 +0300 --[zone(2, true)]--> // 5:31:26 +0200 It is possible that 5:31:26 doesn't exist int zone @@ -2417,38 +2432,39 @@ // a second time. In case it wants us to change the offset again // _changeInProgress == true case, then we have to adjust, because // there is no such time in the given timezone. - zone : function (input, keepLocalTime) { + utcOffset : function (input, keepLocalTime) { var offset = this._offset || 0, localAdjust; if (input != null) { if (typeof input === 'string') { - input = timezoneMinutesFromString(input); + input = utcOffsetFromString(input); } if (Math.abs(input) < 16) { input = input * 60; } if (!this._isUTC && keepLocalTime) { - localAdjust = this._dateTzOffset(); + localAdjust = this._dateUtcOffset(); } this._offset = input; this._isUTC = true; if (localAdjust != null) { - this.subtract(localAdjust, 'm'); + this.add(localAdjust, 'm'); } if (offset !== input) { if (!keepLocalTime || this._changeInProgress) { addOrSubtractDurationFromMoment(this, - moment.duration(offset - input, 'm'), 1, false); + moment.duration(input - offset, 'm'), 1, false); } else if (!this._changeInProgress) { this._changeInProgress = true; moment.updateOffset(this, true); this._changeInProgress = null; } } + + return this; } else { - return this._isUTC ? offset : this._dateTzOffset(); + return this._isUTC ? offset : this._dateUtcOffset(); } - return this; }, zoneAbbr : function () { @@ -2461,9 +2477,9 @@ parseZone : function () { if (this._tzm) { - this.zone(this._tzm); + this.utcOffset(this._tzm); } else if (typeof this._i === 'string') { - this.zone(this._i); + this.utcOffset(utcOffsetFromString(this._i)); } return this; }, @@ -2473,10 +2489,10 @@ input = 0; } else { - input = moment(input).zone(); + input = moment(input).utcOffset(); } - return (this.zone() - input) % 60 === 0; + return (this.utcOffset() - input) % 60 === 0; }, daysInMonth : function () { @@ -2578,11 +2594,12 @@ return this._locale; }, - _dateTzOffset : function () { + _dateUtcOffset : function () { // On Firefox.24 Date#getTimezoneOffset returns a floating point. // https://github.com/moment/moment/pull/1871 - return Math.round(this._d.getTimezoneOffset() / 15) * 15; + return -Math.round(this._d.getTimezoneOffset() / 15) * 15; } + }); function rawMonthSetter(mom, value) { diff --git a/test/moment/offset.js b/test/moment/offset.js new file mode 100644 index 000000000..5f0f1e22d --- /dev/null +++ b/test/moment/offset.js @@ -0,0 +1,40 @@ +/*global require, exports */ + +var moment = require('../../moment'); + +exports.offset = { + setUp : function (done) { + moment.createFromInputFallback = function () { + throw new Error('input not handled by moment'); + }; + done(); + }, + + 'setter / getter blackbox' : function (test) { + var m = moment([2010]); + + test.equal(m.clone().utcOffset(0).utcOffset(), 0, 'utcOffset 0'); + + test.equal(m.clone().utcOffset(1).utcOffset(), 60, 'utcOffset 1 is 60'); + test.equal(m.clone().utcOffset(60).utcOffset(), 60, 'utcOffset 60'); + test.equal(m.clone().utcOffset('+01:00').utcOffset(), 60, 'utcOffset +01:00 is 60'); + test.equal(m.clone().utcOffset('+0100').utcOffset(), 60, 'utcOffset +0100 is 60'); + + test.equal(m.clone().utcOffset(-1).utcOffset(), -60, 'utcOffset -1 is -60'); + test.equal(m.clone().utcOffset(-60).utcOffset(), -60, 'utcOffset -60'); + test.equal(m.clone().utcOffset('-01:00').utcOffset(), -60, 'utcOffset -01:00 is -60'); + test.equal(m.clone().utcOffset('-0100').utcOffset(), -60, 'utcOffset -0100 is -60'); + + test.equal(m.clone().utcOffset(1.5).utcOffset(), 90, 'utcOffset 1.5 is 90'); + test.equal(m.clone().utcOffset(90).utcOffset(), 90, 'utcOffset 1.5 is 90'); + test.equal(m.clone().utcOffset('+01:30').utcOffset(), 90, 'utcOffset +01:30 is 90'); + test.equal(m.clone().utcOffset('+0130').utcOffset(), 90, 'utcOffset +0130 is 90'); + + test.equal(m.clone().utcOffset(-1.5).utcOffset(), -90, 'utcOffset -1.5'); + test.equal(m.clone().utcOffset(-90).utcOffset(), -90, 'utcOffset -90'); + test.equal(m.clone().utcOffset('-01:30').utcOffset(), -90, 'utcOffset +01:30 is 90'); + test.equal(m.clone().utcOffset('-0130').utcOffset(), -90, 'utcOffset +0130 is 90'); + + test.done(); + } +};