From: Tim Wood Date: Thu, 1 Dec 2011 21:40:58 +0000 (-0800) Subject: Fixing add and subtract over DST X-Git-Tag: 1.2.0~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ea3cf1d586c8698d30ef6399cf67379358636af;p=thirdparty%2Fmoment.git Fixing add and subtract over DST #78 --- diff --git a/moment.js b/moment.js index f5954b742..1aa3a1220 100644 --- a/moment.js +++ b/moment.js @@ -30,21 +30,24 @@ 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); diff --git a/sitesrc/docs.jade b/sitesrc/docs.jade index 2c1f55b0e..75e30c2d5 100644 --- a/sitesrc/docs.jade +++ b/sitesrc/docs.jade @@ -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 diff --git a/sitesrc/js/unit-tests.js b/sitesrc/js/unit-tests.js index 646e83664..da8d63f0d 100755 --- a/sitesrc/js/unit-tests.js +++ b/sitesrc/js/unit-tests.js @@ -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");