c617b14b64d763a711aec4cd2afb5558411dc92c1fb1de42a42c1d8584b756cbe56489f71adf8ac84977d80ea5376abfd7f20b6c2e5bdc152576fa9d60c18e 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import absFloor from '../utils/abs-floor';
  2. import absCeil from '../utils/abs-ceil';
  3. import { createUTCDate } from '../create/date-from-array';
  4. export function bubble () {
  5. var milliseconds = this._milliseconds;
  6. var days = this._days;
  7. var months = this._months;
  8. var data = this._data;
  9. var seconds, minutes, hours, years, monthsFromDays;
  10. // if we have a mix of positive and negative values, bubble down first
  11. // check: https://github.com/moment/moment/issues/2166
  12. if (!((milliseconds >= 0 && days >= 0 && months >= 0) ||
  13. (milliseconds <= 0 && days <= 0 && months <= 0))) {
  14. milliseconds += absCeil(monthsToDays(months) + days) * 864e5;
  15. days = 0;
  16. months = 0;
  17. }
  18. // The following code bubbles up values, see the tests for
  19. // examples of what that means.
  20. data.milliseconds = milliseconds % 1000;
  21. seconds = absFloor(milliseconds / 1000);
  22. data.seconds = seconds % 60;
  23. minutes = absFloor(seconds / 60);
  24. data.minutes = minutes % 60;
  25. hours = absFloor(minutes / 60);
  26. data.hours = hours % 24;
  27. days += absFloor(hours / 24);
  28. // convert days to months
  29. monthsFromDays = absFloor(daysToMonths(days));
  30. months += monthsFromDays;
  31. days -= absCeil(monthsToDays(monthsFromDays));
  32. // 12 months -> 1 year
  33. years = absFloor(months / 12);
  34. months %= 12;
  35. data.days = days;
  36. data.months = months;
  37. data.years = years;
  38. return this;
  39. }
  40. export function daysToMonths (days) {
  41. // 400 years have 146097 days (taking into account leap year rules)
  42. // 400 years have 12 months === 4800
  43. return days * 4800 / 146097;
  44. }
  45. export function monthsToDays (months) {
  46. // the reverse of daysToMonths
  47. return months * 146097 / 4800;
  48. }