constructor.js (1593B)
1import { normalizeObjectUnits } from '../units/aliases'; 2import { getLocale } from '../locale/locales'; 3import isDurationValid from './valid.js'; 4 5export function Duration(duration) { 6 var normalizedInput = normalizeObjectUnits(duration), 7 years = normalizedInput.year || 0, 8 quarters = normalizedInput.quarter || 0, 9 months = normalizedInput.month || 0, 10 weeks = normalizedInput.week || normalizedInput.isoWeek || 0, 11 days = normalizedInput.day || 0, 12 hours = normalizedInput.hour || 0, 13 minutes = normalizedInput.minute || 0, 14 seconds = normalizedInput.second || 0, 15 milliseconds = normalizedInput.millisecond || 0; 16 17 this._isValid = isDurationValid(normalizedInput); 18 19 // representation for dateAddRemove 20 this._milliseconds = 21 +milliseconds + 22 seconds * 1e3 + // 1000 23 minutes * 6e4 + // 1000 * 60 24 hours * 1000 * 60 * 60; //using 1000 * 60 * 60 instead of 36e5 to avoid floating point rounding errors https://github.com/moment/moment/issues/2978 25 // Because of dateAddRemove treats 24 hours as different from a 26 // day when working around DST, we need to store them separately 27 this._days = +days + weeks * 7; 28 // It is impossible to translate months into days without knowing 29 // which months you are are talking about, so we have to store 30 // it separately. 31 this._months = +months + quarters * 3 + years * 12; 32 33 this._data = {}; 34 35 this._locale = getLocale(); 36 37 this._bubble(); 38} 39 40export function isDuration(obj) { 41 return obj instanceof Duration; 42}