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.

index.cjs.js 7.3 KiB

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var MILI = 'milliseconds'
  4. , SECONDS = 'seconds'
  5. , MINUTES = 'minutes'
  6. , HOURS = 'hours'
  7. , DAY = 'day'
  8. , WEEK = 'week'
  9. , MONTH = 'month'
  10. , YEAR = 'year'
  11. , DECADE = 'decade'
  12. , CENTURY = 'century';
  13. var multiplierMilli = {
  14. 'milliseconds': 1,
  15. 'seconds': 1000,
  16. 'minutes': 60 * 1000,
  17. 'hours': 60 * 60 * 1000,
  18. 'day': 24 * 60 * 60 * 1000,
  19. 'week': 7 * 24 * 60 * 60 * 1000
  20. };
  21. var multiplierMonth = {
  22. 'month': 1,
  23. 'year': 12,
  24. 'decade': 10 * 12,
  25. 'century': 100 * 12
  26. };
  27. function daysOf(year) {
  28. return [31, daysInFeb(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  29. }
  30. function daysInFeb(year) {
  31. return (
  32. year % 4 === 0
  33. && year % 100 !== 0
  34. ) || year % 400 === 0
  35. ? 29
  36. : 28
  37. }
  38. function add(d, num, unit) {
  39. d = new Date(d);
  40. switch (unit){
  41. case MILI:
  42. case SECONDS:
  43. case MINUTES:
  44. case HOURS:
  45. case DAY:
  46. case WEEK:
  47. return addMillis(d, num * multiplierMilli[unit])
  48. case MONTH:
  49. case YEAR:
  50. case DECADE:
  51. case CENTURY:
  52. return addMonths(d, num * multiplierMonth[unit])
  53. }
  54. throw new TypeError('Invalid units: "' + unit + '"')
  55. }
  56. function addMillis(d, num) {
  57. var nextDate = new Date(+(d) + num);
  58. return solveDST(d, nextDate)
  59. }
  60. function addMonths(d, num) {
  61. var year = d.getFullYear()
  62. , month = d.getMonth()
  63. , day = d.getDate()
  64. , totalMonths = year * 12 + month + num
  65. , nextYear = Math.trunc(totalMonths / 12)
  66. , nextMonth = totalMonths % 12
  67. , nextDay = Math.min(day, daysOf(nextYear)[nextMonth]);
  68. var nextDate = new Date(d);
  69. nextDate.setFullYear(nextYear);
  70. // To avoid a bug when sets the Feb month
  71. // with a date > 28 or date > 29 (leap year)
  72. nextDate.setDate(1);
  73. nextDate.setMonth(nextMonth);
  74. nextDate.setDate(nextDay);
  75. return nextDate
  76. }
  77. function solveDST(currentDate, nextDate) {
  78. var currentOffset = currentDate.getTimezoneOffset()
  79. , nextOffset = nextDate.getTimezoneOffset();
  80. // if is DST, add the difference in minutes
  81. // else the difference is zero
  82. var diffMinutes = (nextOffset - currentOffset);
  83. return new Date(+(nextDate) + diffMinutes * multiplierMilli['minutes'])
  84. }
  85. function subtract(d, num, unit) {
  86. return add(d, -num, unit)
  87. }
  88. function startOf(d, unit, firstOfWeek) {
  89. d = new Date(d);
  90. switch (unit) {
  91. case CENTURY:
  92. case DECADE:
  93. case YEAR:
  94. d = month(d, 0);
  95. case MONTH:
  96. d = date(d, 1);
  97. case WEEK:
  98. case DAY:
  99. d = hours(d, 0);
  100. case HOURS:
  101. d = minutes(d, 0);
  102. case MINUTES:
  103. d = seconds(d, 0);
  104. case SECONDS:
  105. d = milliseconds(d, 0);
  106. }
  107. if (unit === DECADE)
  108. d = subtract(d, year(d) % 10, 'year');
  109. if (unit === CENTURY)
  110. d = subtract(d, year(d) % 100, 'year');
  111. if (unit === WEEK)
  112. d = weekday(d, 0, firstOfWeek);
  113. return d
  114. }
  115. function endOf(d, unit, firstOfWeek){
  116. d = new Date(d);
  117. d = startOf(d, unit, firstOfWeek);
  118. switch (unit) {
  119. case CENTURY:
  120. case DECADE:
  121. case YEAR:
  122. case MONTH:
  123. case WEEK:
  124. d = add(d, 1, unit);
  125. d = subtract(d, 1, DAY);
  126. d.setHours(23, 59, 59, 999);
  127. break;
  128. case DAY:
  129. d.setHours(23, 59, 59, 999);
  130. break;
  131. case HOURS:
  132. case MINUTES:
  133. case SECONDS:
  134. d = add(d, 1, unit);
  135. d = subtract(d, 1, MILI);
  136. }
  137. return d
  138. }
  139. var eq = createComparer(function(a, b){ return a === b });
  140. var neq = createComparer(function(a, b){ return a !== b });
  141. var gt = createComparer(function(a, b){ return a > b });
  142. var gte = createComparer(function(a, b){ return a >= b });
  143. var lt = createComparer(function(a, b){ return a < b });
  144. var lte = createComparer(function(a, b){ return a <= b });
  145. function min(){
  146. return new Date(Math.min.apply(Math, arguments))
  147. }
  148. function max(){
  149. return new Date(Math.max.apply(Math, arguments))
  150. }
  151. function inRange(day, min, max, unit){
  152. unit = unit || 'day';
  153. return (!min || gte(day, min, unit))
  154. && (!max || lte(day, max, unit))
  155. }
  156. var milliseconds = createAccessor('Milliseconds');
  157. var seconds = createAccessor('Seconds');
  158. var minutes = createAccessor('Minutes');
  159. var hours = createAccessor('Hours');
  160. var day = createAccessor('Day');
  161. var date = createAccessor('Date');
  162. var month = createAccessor('Month');
  163. var year = createAccessor('FullYear');
  164. function decade(d, val) {
  165. return val === undefined
  166. ? year(startOf(d, DECADE))
  167. : add(d, val + 10, YEAR);
  168. }
  169. function century(d, val) {
  170. return val === undefined
  171. ? year(startOf(d, CENTURY))
  172. : add(d, val + 100, YEAR);
  173. }
  174. function weekday(d, val, firstDay) {
  175. var w = (day(d) + 7 - (firstDay || 0) ) % 7;
  176. return val === undefined
  177. ? w
  178. : add(d, val - w, DAY);
  179. }
  180. function diff(date1, date2, unit, asFloat) {
  181. var dividend, divisor, result;
  182. switch (unit) {
  183. case MILI:
  184. case SECONDS:
  185. case MINUTES:
  186. case HOURS:
  187. case DAY:
  188. case WEEK:
  189. dividend = date2.getTime() - date1.getTime(); break;
  190. case MONTH:
  191. case YEAR:
  192. case DECADE:
  193. case CENTURY:
  194. dividend = (year(date2) - year(date1)) * 12 + month(date2) - month(date1); break;
  195. default:
  196. throw new TypeError('Invalid units: "' + unit + '"');
  197. }
  198. switch (unit) {
  199. case MILI:
  200. divisor = 1; break;
  201. case SECONDS:
  202. divisor = 1000; break;
  203. case MINUTES:
  204. divisor = 1000 * 60; break;
  205. case HOURS:
  206. divisor = 1000 * 60 * 60; break;
  207. case DAY:
  208. divisor = 1000 * 60 * 60 * 24; break;
  209. case WEEK:
  210. divisor = 1000 * 60 * 60 * 24 * 7; break;
  211. case MONTH:
  212. divisor = 1; break;
  213. case YEAR:
  214. divisor = 12; break;
  215. case DECADE:
  216. divisor = 120; break;
  217. case CENTURY:
  218. divisor = 1200; break;
  219. default:
  220. throw new TypeError('Invalid units: "' + unit + '"');
  221. }
  222. result = dividend / divisor;
  223. return asFloat ? result : Math.round(result);
  224. }
  225. function createAccessor(method){
  226. var hourLength = (function(method) {
  227. switch(method) {
  228. case 'Milliseconds':
  229. return 3600000;
  230. case 'Seconds':
  231. return 3600;
  232. case 'Minutes':
  233. return 60;
  234. case 'Hours':
  235. return 1;
  236. default:
  237. return null;
  238. }
  239. })(method);
  240. return function(d, val){
  241. if (val === undefined)
  242. return d['get' + method]()
  243. var dateOut = new Date(d);
  244. dateOut['set' + method](val);
  245. if(hourLength && dateOut['get'+method]() != val && (method === 'Hours' || val >=hourLength && (dateOut.getHours()-d.getHours()<Math.floor(val/hourLength))) ){
  246. //Skip DST hour, if it occurs
  247. dateOut['set'+method](val+hourLength);
  248. }
  249. return dateOut
  250. }
  251. }
  252. function createComparer(operator) {
  253. return function (a, b, unit) {
  254. return operator(+startOf(a, unit), +startOf(b, unit))
  255. };
  256. }
  257. exports.add = add;
  258. exports.century = century;
  259. exports.date = date;
  260. exports.day = day;
  261. exports.decade = decade;
  262. exports.diff = diff;
  263. exports.endOf = endOf;
  264. exports.eq = eq;
  265. exports.gt = gt;
  266. exports.gte = gte;
  267. exports.hours = hours;
  268. exports.inRange = inRange;
  269. exports.lt = lt;
  270. exports.lte = lte;
  271. exports.max = max;
  272. exports.milliseconds = milliseconds;
  273. exports.min = min;
  274. exports.minutes = minutes;
  275. exports.month = month;
  276. exports.neq = neq;
  277. exports.seconds = seconds;
  278. exports.startOf = startOf;
  279. exports.subtract = subtract;
  280. exports.weekday = weekday;
  281. exports.year = year;