e8ceee5e538c6aaaeb602fa14104f28ab62e481a59f0435580217802178e6328b15bbec045fb0bfc193e842bab6cbfa50843b9723ddffb5efc38f6ca6d3693 1.6 KB

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