ed5511003a42ec22b8314eabed135f02c9afda2e25803431b6c0aed3dcffbda965a44a993b13ba4950ebd36b742e5f5ca5264eadb77e78fce37ed03ea63449 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { normalizeUnits } from '../units/aliases';
  2. import { hooks } from '../utils/hooks';
  3. var MS_PER_SECOND = 1000,
  4. MS_PER_MINUTE = 60 * MS_PER_SECOND,
  5. MS_PER_HOUR = 60 * MS_PER_MINUTE,
  6. MS_PER_400_YEARS = (365 * 400 + 97) * 24 * MS_PER_HOUR;
  7. // actual modulo - handles negative numbers (for dates before 1970):
  8. function mod(dividend, divisor) {
  9. return ((dividend % divisor) + divisor) % divisor;
  10. }
  11. function localStartOfDate(y, m, d) {
  12. // the date constructor remaps years 0-99 to 1900-1999
  13. if (y < 100 && y >= 0) {
  14. // preserve leap years using a full 400 year cycle, then reset
  15. return new Date(y + 400, m, d) - MS_PER_400_YEARS;
  16. } else {
  17. return new Date(y, m, d).valueOf();
  18. }
  19. }
  20. function utcStartOfDate(y, m, d) {
  21. // Date.UTC remaps years 0-99 to 1900-1999
  22. if (y < 100 && y >= 0) {
  23. // preserve leap years using a full 400 year cycle, then reset
  24. return Date.UTC(y + 400, m, d) - MS_PER_400_YEARS;
  25. } else {
  26. return Date.UTC(y, m, d);
  27. }
  28. }
  29. export function startOf(units) {
  30. var time, startOfDate;
  31. units = normalizeUnits(units);
  32. if (units === undefined || units === 'millisecond' || !this.isValid()) {
  33. return this;
  34. }
  35. startOfDate = this._isUTC ? utcStartOfDate : localStartOfDate;
  36. switch (units) {
  37. case 'year':
  38. time = startOfDate(this.year(), 0, 1);
  39. break;
  40. case 'quarter':
  41. time = startOfDate(
  42. this.year(),
  43. this.month() - (this.month() % 3),
  44. 1
  45. );
  46. break;
  47. case 'month':
  48. time = startOfDate(this.year(), this.month(), 1);
  49. break;
  50. case 'week':
  51. time = startOfDate(
  52. this.year(),
  53. this.month(),
  54. this.date() - this.weekday()
  55. );
  56. break;
  57. case 'isoWeek':
  58. time = startOfDate(
  59. this.year(),
  60. this.month(),
  61. this.date() - (this.isoWeekday() - 1)
  62. );
  63. break;
  64. case 'day':
  65. case 'date':
  66. time = startOfDate(this.year(), this.month(), this.date());
  67. break;
  68. case 'hour':
  69. time = this._d.valueOf();
  70. time -= mod(
  71. time + (this._isUTC ? 0 : this.utcOffset() * MS_PER_MINUTE),
  72. MS_PER_HOUR
  73. );
  74. break;
  75. case 'minute':
  76. time = this._d.valueOf();
  77. time -= mod(time, MS_PER_MINUTE);
  78. break;
  79. case 'second':
  80. time = this._d.valueOf();
  81. time -= mod(time, MS_PER_SECOND);
  82. break;
  83. }
  84. this._d.setTime(time);
  85. hooks.updateOffset(this, true);
  86. return this;
  87. }
  88. export function endOf(units) {
  89. var time, startOfDate;
  90. units = normalizeUnits(units);
  91. if (units === undefined || units === 'millisecond' || !this.isValid()) {
  92. return this;
  93. }
  94. startOfDate = this._isUTC ? utcStartOfDate : localStartOfDate;
  95. switch (units) {
  96. case 'year':
  97. time = startOfDate(this.year() + 1, 0, 1) - 1;
  98. break;
  99. case 'quarter':
  100. time =
  101. startOfDate(
  102. this.year(),
  103. this.month() - (this.month() % 3) + 3,
  104. 1
  105. ) - 1;
  106. break;
  107. case 'month':
  108. time = startOfDate(this.year(), this.month() + 1, 1) - 1;
  109. break;
  110. case 'week':
  111. time =
  112. startOfDate(
  113. this.year(),
  114. this.month(),
  115. this.date() - this.weekday() + 7
  116. ) - 1;
  117. break;
  118. case 'isoWeek':
  119. time =
  120. startOfDate(
  121. this.year(),
  122. this.month(),
  123. this.date() - (this.isoWeekday() - 1) + 7
  124. ) - 1;
  125. break;
  126. case 'day':
  127. case 'date':
  128. time = startOfDate(this.year(), this.month(), this.date() + 1) - 1;
  129. break;
  130. case 'hour':
  131. time = this._d.valueOf();
  132. time +=
  133. MS_PER_HOUR -
  134. mod(
  135. time + (this._isUTC ? 0 : this.utcOffset() * MS_PER_MINUTE),
  136. MS_PER_HOUR
  137. ) -
  138. 1;
  139. break;
  140. case 'minute':
  141. time = this._d.valueOf();
  142. time += MS_PER_MINUTE - mod(time, MS_PER_MINUTE) - 1;
  143. break;
  144. case 'second':
  145. time = this._d.valueOf();
  146. time += MS_PER_SECOND - mod(time, MS_PER_SECOND) - 1;
  147. break;
  148. }
  149. this._d.setTime(time);
  150. hooks.updateOffset(this, true);
  151. return this;
  152. }