]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Better timezone support for manipulating moments
authorElliot Kroo <elliot@getaround.com>
Wed, 15 Jan 2014 02:44:40 +0000 (18:44 -0800)
committerIskren Chernev <iskren.chernev@gmail.com>
Thu, 6 Feb 2014 18:16:17 +0000 (10:16 -0800)
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.

moment.js

index ed88f4668d700fad786bb92b719d4cd7d9c75c61..edf54c173bc389b3200e81b3099648ab9a217048 100644 (file)
--- a/moment.js
+++ b/moment.js
             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) {
                 }
 
                 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']();
 
     // 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]();