From: Elliot Kroo Date: Wed, 15 Jan 2014 02:44:40 +0000 (-0800) Subject: Better timezone support for manipulating moments X-Git-Tag: 2.6.0~35^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31905c1dfe49b115f898e1a5cc9919d7b765d8cc;p=thirdparty%2Fmoment.git Better timezone support for manipulating moments This adds a second argument to setters that provides a hint to updateOffset to not change hours/minutes when crossing a tz boundary. This is frequently desirable when modifying part of an existing moment object directly. It further passes this argument whenever adding / subtracting days or months, fixing an issue with moment-timezone (moment/moment-timezone#28). See the corresponding PR for moment-timezone here: moment/moment-timezone#53. --- diff --git a/moment.js b/moment.js index ed88f4668..edf54c173 100644 --- a/moment.js +++ b/moment.js @@ -435,13 +435,13 @@ hours = mom.hour(); } if (days) { - mom.date(mom.date() + days * isAdding); + mom.date(mom.date() + days * isAdding, true); } if (months) { - mom.month(mom.month() + months * isAdding); + mom.month(mom.month() + months * isAdding, true); } if (milliseconds && !ignoreUpdateOffset) { - moment.updateOffset(mom); + moment.updateOffset(mom, days || months); } // restore the minutes and hours after possibly changing dst if (days || months) { @@ -1960,11 +1960,11 @@ } dayOfMonth = this.date(); - this.date(1); - this._d['set' + utc + 'Month'](input); - this.date(Math.min(dayOfMonth, this.daysInMonth())); + this.date(1, true); + this._d['set' + utc + 'Month'](input, true); + this.date(Math.min(dayOfMonth, this.daysInMonth()), true); - moment.updateOffset(this); + moment.updateOffset(this, true); return this; } else { return this._d['get' + utc + 'Month'](); @@ -2168,11 +2168,14 @@ // helper for adding shortcuts function makeGetterAndSetter(name, key) { - moment.fn[name] = moment.fn[name + 's'] = function (input) { + // ignoreOffsetTransitions provides a hint to updateOffset to not + // change hours/minutes when crossing a tz boundary. This is frequently + // desirable when modifying part of an existing moment object directly. + moment.fn[name] = moment.fn[name + 's'] = function (input, ignoreOffsetTransitions) { var utc = this._isUTC ? 'UTC' : ''; if (input != null) { this._d['set' + utc + key](input); - moment.updateOffset(this); + moment.updateOffset(this, ignoreOffsetTransitions); return this; } else { return this._d['get' + utc + key]();