]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fixing add and subtract over DST
authorTim Wood <washwithcare@gmail.com>
Thu, 1 Dec 2011 21:40:58 +0000 (13:40 -0800)
committerTim Wood <washwithcare@gmail.com>
Thu, 1 Dec 2011 21:40:58 +0000 (13:40 -0800)
#78

moment.js
sitesrc/docs.jade
sitesrc/js/unit-tests.js

index f5954b742e613fccdf601e04aec15f6739712a77..1aa3a122060131429907b821f714c92a8a016ab2 100644 (file)
--- a/moment.js
+++ b/moment.js
     function dateAddRemove(date, _input, adding, val) {
         var isString = (typeof _input === 'string'),
             input = isString ? {} : _input,
-            ms, M, currentDate;
+            ms, d, M, currentDate;
         if (isString && val) {
             input[_input] = val;
         }
         ms = (input.ms || input.milliseconds || 0) +
             (input.s || input.seconds || 0) * 1e3 + // 1000
             (input.m || input.minutes || 0) * 6e4 + // 1000 * 60
-            (input.h || input.hours || 0) * 36e5 + // 1000 * 60 * 60
-            (input.d || input.days || 0) * 864e5 + // 1000 * 60 * 60 * 24
-            (input.w || input.weeks || 0) * 6048e5; // 1000 * 60 * 60 * 24 * 7
+            (input.h || input.hours || 0) * 36e5; // 1000 * 60 * 60
+        d = (input.d || input.days || 0) +
+            (input.w || input.weeks || 0) * 7;
         M = (input.M || input.months || 0) +
             (input.y || input.years || 0) * 12;
         if (ms) {
             date.setTime(+date + ms * adding);
         }
+        if (d) {
+            date.setDate(date.getDate() + d * adding);
+        }
         if (M) {
             currentDate = date.getDate();
             date.setDate(1);
index 2c1f55b0e38dc0fb5ddfc785f42181bccf71b116..75e30c2d50e35cbfa96e0f6b490e3e154db89ca6 100644 (file)
@@ -388,6 +388,15 @@ block content
       p Example:
       pre moment([2010, 0, 31]);                  // January 31 \n
         | moment([2010, 0, 31]).add('months', 1); // February 28
+      p There are also special considerations to keep in mind when adding time that crosses over Daylight Savings Time.
+        | If you are adding years, months, weeks, or days, the original hour will always match the added hour.
+      pre var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US\n
+        | m.hours(); // 5
+        | m.add('days', 1).hours(); // 5
+      p If you are adding hours, minutes, seconds, or milliseconds, the assumption is that you want precision to the hour, and will result in a different hour.
+      pre var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US\n
+        | m.hours(); // 5
+        | m.add('hours', 24).hours(); // 6
 
 
       a(name="/manipulation/subtract")
@@ -904,7 +913,7 @@ block content
       p Once you load a language, it becomes the active language. To change active languages, simply call 
         code moment.lang
         | with the key of a loaded language.
-      pre moment.lang('fr');
+      pre moment.lang('fr');\n
         | moment(1316116057189).fromNow() // il y a une heure
         | moment.lang('en');
         | moment(1316116057189).fromNow() // an hour ago
@@ -919,7 +928,7 @@ block content
         code moment.lang
         | will load it.
       pre var moment = require('moment');\n
-        | moment.lang('fr');\n
+        | moment.lang('fr');
         | moment(1316116057189).fromNow(); // il y a une heure
       p Right now, there is only support for English, French, Italian, and Portuguese. 
         | If you want your language supported, create a pull request or send me an email with the 
index 646e83664a112b452aa4b5597af77e23928a7ee9..da8d63f0d17981cb868e91cecfc942530725e8a0 100755 (executable)
@@ -219,6 +219,18 @@ test("add and subtract string short", 8, function() {
     equal(a.add('y', 1).year(), 2012, 'Add year');
 });
 
+test("adding across DST", 3, function(){
+    var a = moment(new Date(2011, 2, 12, 5, 0, 0));
+    var b = moment(new Date(2011, 2, 12, 5, 0, 0));
+    var c = moment(new Date(2011, 2, 12, 5, 0, 0));
+    a.add('days', 1);
+    b.add('hours', 24);
+    c.add('months', 1);
+    equal(a.hours(), 5, 'adding days over DST difference should result in the same hour');
+    equal(b.hours(), 6, 'adding hours over DST difference should result in a different hour');
+    equal(c.hours(), 5, 'adding months over DST difference should result in the same hour');
+});
+
 module("diff");