You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

176 lines
4.7 KiB

  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  3. exports.__esModule = true;
  4. exports.monthsInYear = monthsInYear;
  5. exports.firstVisibleDay = firstVisibleDay;
  6. exports.lastVisibleDay = lastVisibleDay;
  7. exports.visibleDays = visibleDays;
  8. exports.ceil = ceil;
  9. exports.range = range;
  10. exports.merge = merge;
  11. exports.eqTime = eqTime;
  12. exports.isJustDate = isJustDate;
  13. exports.duration = duration;
  14. exports.diff = diff;
  15. exports.total = total;
  16. exports.week = week;
  17. exports.today = today;
  18. exports.yesterday = yesterday;
  19. exports.tomorrow = tomorrow;
  20. exports.max = exports.min = exports.inRange = exports.lt = exports.lte = exports.gt = exports.gte = exports.eq = exports.add = exports.endOf = exports.startOf = exports.month = exports.hours = exports.minutes = exports.seconds = exports.milliseconds = void 0;
  21. var dates = _interopRequireWildcard(require("date-arithmetic"));
  22. exports.milliseconds = dates.milliseconds;
  23. exports.seconds = dates.seconds;
  24. exports.minutes = dates.minutes;
  25. exports.hours = dates.hours;
  26. exports.month = dates.month;
  27. exports.startOf = dates.startOf;
  28. exports.endOf = dates.endOf;
  29. exports.add = dates.add;
  30. exports.eq = dates.eq;
  31. exports.gte = dates.gte;
  32. exports.gt = dates.gt;
  33. exports.lte = dates.lte;
  34. exports.lt = dates.lt;
  35. exports.inRange = dates.inRange;
  36. exports.min = dates.min;
  37. exports.max = dates.max;
  38. /* eslint no-fallthrough: off */
  39. var MILLI = {
  40. seconds: 1000,
  41. minutes: 1000 * 60,
  42. hours: 1000 * 60 * 60,
  43. day: 1000 * 60 * 60 * 24
  44. };
  45. var MONTHS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  46. function monthsInYear(year) {
  47. var date = new Date(year, 0, 1);
  48. return MONTHS.map(function (i) {
  49. return dates.month(date, i);
  50. });
  51. }
  52. function firstVisibleDay(date, localizer) {
  53. var firstOfMonth = dates.startOf(date, 'month');
  54. return dates.startOf(firstOfMonth, 'week', localizer.startOfWeek());
  55. }
  56. function lastVisibleDay(date, localizer) {
  57. var endOfMonth = dates.endOf(date, 'month');
  58. return dates.endOf(endOfMonth, 'week', localizer.startOfWeek());
  59. }
  60. function visibleDays(date, localizer) {
  61. var current = firstVisibleDay(date, localizer),
  62. last = lastVisibleDay(date, localizer),
  63. days = [];
  64. while (dates.lte(current, last, 'day')) {
  65. days.push(current);
  66. current = dates.add(current, 1, 'day');
  67. }
  68. return days;
  69. }
  70. function ceil(date, unit) {
  71. var floor = dates.startOf(date, unit);
  72. return dates.eq(floor, date) ? floor : dates.add(floor, 1, unit);
  73. }
  74. function range(start, end, unit) {
  75. if (unit === void 0) {
  76. unit = 'day';
  77. }
  78. var current = start,
  79. days = [];
  80. while (dates.lte(current, end, unit)) {
  81. days.push(current);
  82. current = dates.add(current, 1, unit);
  83. }
  84. return days;
  85. }
  86. function merge(date, time) {
  87. if (time == null && date == null) return null;
  88. if (time == null) time = new Date();
  89. if (date == null) date = new Date();
  90. date = dates.startOf(date, 'day');
  91. date = dates.hours(date, dates.hours(time));
  92. date = dates.minutes(date, dates.minutes(time));
  93. date = dates.seconds(date, dates.seconds(time));
  94. return dates.milliseconds(date, dates.milliseconds(time));
  95. }
  96. function eqTime(dateA, dateB) {
  97. return dates.hours(dateA) === dates.hours(dateB) && dates.minutes(dateA) === dates.minutes(dateB) && dates.seconds(dateA) === dates.seconds(dateB);
  98. }
  99. function isJustDate(date) {
  100. return dates.hours(date) === 0 && dates.minutes(date) === 0 && dates.seconds(date) === 0 && dates.milliseconds(date) === 0;
  101. }
  102. function duration(start, end, unit, firstOfWeek) {
  103. if (unit === 'day') unit = 'date';
  104. return Math.abs(dates[unit](start, undefined, firstOfWeek) - dates[unit](end, undefined, firstOfWeek));
  105. }
  106. function diff(dateA, dateB, unit) {
  107. if (!unit || unit === 'milliseconds') return Math.abs(+dateA - +dateB); // the .round() handles an edge case
  108. // with DST where the total won't be exact
  109. // since one day in the range may be shorter/longer by an hour
  110. return Math.round(Math.abs(+dates.startOf(dateA, unit) / MILLI[unit] - +dates.startOf(dateB, unit) / MILLI[unit]));
  111. }
  112. function total(date, unit) {
  113. var ms = date.getTime(),
  114. div = 1;
  115. switch (unit) {
  116. case 'week':
  117. div *= 7;
  118. case 'day':
  119. div *= 24;
  120. case 'hours':
  121. div *= 60;
  122. case 'minutes':
  123. div *= 60;
  124. case 'seconds':
  125. div *= 1000;
  126. }
  127. return ms / div;
  128. }
  129. function week(date) {
  130. var d = new Date(date);
  131. d.setHours(0, 0, 0);
  132. d.setDate(d.getDate() + 4 - (d.getDay() || 7));
  133. return Math.ceil(((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7 + 1) / 7);
  134. }
  135. function today() {
  136. return dates.startOf(new Date(), 'day');
  137. }
  138. function yesterday() {
  139. return dates.add(dates.startOf(new Date(), 'day'), -1, 'day');
  140. }
  141. function tomorrow() {
  142. return dates.add(dates.startOf(new Date(), 'day'), 1, 'day');
  143. }