diff options
Diffstat (limited to 'bower_components/moment/src/lib/duration')
| -rw-r--r-- | bower_components/moment/src/lib/duration/as.js | 6 | ||||
| -rw-r--r-- | bower_components/moment/src/lib/duration/bubble.js | 39 | ||||
| -rw-r--r-- | bower_components/moment/src/lib/duration/iso-string.js | 36 |
3 files changed, 56 insertions, 25 deletions
diff --git a/bower_components/moment/src/lib/duration/as.js b/bower_components/moment/src/lib/duration/as.js index 258c501..03ecd6d 100644 --- a/bower_components/moment/src/lib/duration/as.js +++ b/bower_components/moment/src/lib/duration/as.js @@ -1,4 +1,4 @@ -import { daysToYears, yearsToDays } from './bubble'; +import { daysToMonths, monthsToDays } from './bubble'; import { normalizeUnits } from '../units/aliases'; import toInt from '../utils/to-int'; @@ -11,11 +11,11 @@ export function as (units) { if (units === 'month' || units === 'year') { days = this._days + milliseconds / 864e5; - months = this._months + daysToYears(days) * 12; + months = this._months + daysToMonths(days); return units === 'month' ? months : months / 12; } else { // handle milliseconds separately because of floating point math errors (issue #1867) - days = this._days + Math.round(yearsToDays(this._months / 12)); + days = this._days + Math.round(monthsToDays(this._months)); switch (units) { case 'week' : return days / 7 + milliseconds / 6048e5; case 'day' : return days + milliseconds / 864e5; diff --git a/bower_components/moment/src/lib/duration/bubble.js b/bower_components/moment/src/lib/duration/bubble.js index 3dae6b2..0c4a336 100644 --- a/bower_components/moment/src/lib/duration/bubble.js +++ b/bower_components/moment/src/lib/duration/bubble.js @@ -1,11 +1,22 @@ import absFloor from '../utils/abs-floor'; +import absCeil from '../utils/abs-ceil'; +import { createUTCDate } from '../create/date-from-array'; export function bubble () { var milliseconds = this._milliseconds; var days = this._days; var months = this._months; var data = this._data; - var seconds, minutes, hours, years = 0; + var seconds, minutes, hours, years, monthsFromDays; + + // if we have a mix of positive and negative values, bubble down first + // check: https://github.com/moment/moment/issues/2166 + if (!((milliseconds >= 0 && days >= 0 && months >= 0) || + (milliseconds <= 0 && days <= 0 && months <= 0))) { + milliseconds += absCeil(monthsToDays(months) + days) * 864e5; + days = 0; + months = 0; + } // The following code bubbles up values, see the tests for // examples of what that means. @@ -22,17 +33,13 @@ export function bubble () { days += absFloor(hours / 24); - // Accurately convert days to years, assume start from year 0. - years = absFloor(daysToYears(days)); - days -= absFloor(yearsToDays(years)); - - // 30 days to a month - // TODO (iskren): Use anchor date (like 1st Jan) to compute this. - months += absFloor(days / 30); - days %= 30; + // convert days to months + monthsFromDays = absFloor(daysToMonths(days)); + months += monthsFromDays; + days -= absCeil(monthsToDays(monthsFromDays)); // 12 months -> 1 year - years += absFloor(months / 12); + years = absFloor(months / 12); months %= 12; data.days = days; @@ -42,13 +49,13 @@ export function bubble () { return this; } -export function daysToYears (days) { +export function daysToMonths (days) { // 400 years have 146097 days (taking into account leap year rules) - return days * 400 / 146097; + // 400 years have 12 months === 4800 + return days * 4800 / 146097; } -export function yearsToDays (years) { - // years * 365 + absFloor(years / 4) - - // absFloor(years / 100) + absFloor(years / 400); - return years * 146097 / 400; +export function monthsToDays (months) { + // the reverse of daysToMonths + return months * 146097 / 4800; } diff --git a/bower_components/moment/src/lib/duration/iso-string.js b/bower_components/moment/src/lib/duration/iso-string.js index 2670a9d..f33a968 100644 --- a/bower_components/moment/src/lib/duration/iso-string.js +++ b/bower_components/moment/src/lib/duration/iso-string.js @@ -1,13 +1,37 @@ +import absFloor from '../utils/abs-floor'; var abs = Math.abs; export function toISOString() { + // for ISO strings we do not use the normal bubbling rules: + // * milliseconds bubble up until they become hours + // * days do not bubble at all + // * months bubble up until they become years + // This is because there is no context-free conversion between hours and days + // (think of clock changes) + // and also not between days and months (28-31 days per month) + var seconds = abs(this._milliseconds) / 1000; + var days = abs(this._days); + var months = abs(this._months); + var minutes, hours, years; + + // 3600 seconds -> 60 minutes -> 1 hour + minutes = absFloor(seconds / 60); + hours = absFloor(minutes / 60); + seconds %= 60; + minutes %= 60; + + // 12 months -> 1 year + years = absFloor(months / 12); + months %= 12; + + // inspired by https://github.com/dordille/moment-isoduration/blob/master/moment.isoduration.js - var Y = abs(this.years()); - var M = abs(this.months()); - var D = abs(this.days()); - var h = abs(this.hours()); - var m = abs(this.minutes()); - var s = abs(this.seconds() + this.milliseconds() / 1000); + var Y = years; + var M = months; + var D = days; + var h = hours; + var m = minutes; + var s = seconds; var total = this.asSeconds(); if (!total) { |
