d3-time.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // https://d3js.org/d3-time/ v3.0.0 Copyright 2010-2021 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array')) :
  4. typeof define === 'function' && define.amd ? define(['exports', 'd3-array'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}, global.d3));
  6. }(this, (function (exports, d3Array) { 'use strict';
  7. var t0 = new Date,
  8. t1 = new Date;
  9. function newInterval(floori, offseti, count, field) {
  10. function interval(date) {
  11. return floori(date = arguments.length === 0 ? new Date : new Date(+date)), date;
  12. }
  13. interval.floor = function(date) {
  14. return floori(date = new Date(+date)), date;
  15. };
  16. interval.ceil = function(date) {
  17. return floori(date = new Date(date - 1)), offseti(date, 1), floori(date), date;
  18. };
  19. interval.round = function(date) {
  20. var d0 = interval(date),
  21. d1 = interval.ceil(date);
  22. return date - d0 < d1 - date ? d0 : d1;
  23. };
  24. interval.offset = function(date, step) {
  25. return offseti(date = new Date(+date), step == null ? 1 : Math.floor(step)), date;
  26. };
  27. interval.range = function(start, stop, step) {
  28. var range = [], previous;
  29. start = interval.ceil(start);
  30. step = step == null ? 1 : Math.floor(step);
  31. if (!(start < stop) || !(step > 0)) return range; // also handles Invalid Date
  32. do range.push(previous = new Date(+start)), offseti(start, step), floori(start);
  33. while (previous < start && start < stop);
  34. return range;
  35. };
  36. interval.filter = function(test) {
  37. return newInterval(function(date) {
  38. if (date >= date) while (floori(date), !test(date)) date.setTime(date - 1);
  39. }, function(date, step) {
  40. if (date >= date) {
  41. if (step < 0) while (++step <= 0) {
  42. while (offseti(date, -1), !test(date)) {} // eslint-disable-line no-empty
  43. } else while (--step >= 0) {
  44. while (offseti(date, +1), !test(date)) {} // eslint-disable-line no-empty
  45. }
  46. }
  47. });
  48. };
  49. if (count) {
  50. interval.count = function(start, end) {
  51. t0.setTime(+start), t1.setTime(+end);
  52. floori(t0), floori(t1);
  53. return Math.floor(count(t0, t1));
  54. };
  55. interval.every = function(step) {
  56. step = Math.floor(step);
  57. return !isFinite(step) || !(step > 0) ? null
  58. : !(step > 1) ? interval
  59. : interval.filter(field
  60. ? function(d) { return field(d) % step === 0; }
  61. : function(d) { return interval.count(0, d) % step === 0; });
  62. };
  63. }
  64. return interval;
  65. }
  66. var millisecond = newInterval(function() {
  67. // noop
  68. }, function(date, step) {
  69. date.setTime(+date + step);
  70. }, function(start, end) {
  71. return end - start;
  72. });
  73. // An optimized implementation for this simple case.
  74. millisecond.every = function(k) {
  75. k = Math.floor(k);
  76. if (!isFinite(k) || !(k > 0)) return null;
  77. if (!(k > 1)) return millisecond;
  78. return newInterval(function(date) {
  79. date.setTime(Math.floor(date / k) * k);
  80. }, function(date, step) {
  81. date.setTime(+date + step * k);
  82. }, function(start, end) {
  83. return (end - start) / k;
  84. });
  85. };
  86. var milliseconds = millisecond.range;
  87. const durationSecond = 1000;
  88. const durationMinute = durationSecond * 60;
  89. const durationHour = durationMinute * 60;
  90. const durationDay = durationHour * 24;
  91. const durationWeek = durationDay * 7;
  92. const durationMonth = durationDay * 30;
  93. const durationYear = durationDay * 365;
  94. var second = newInterval(function(date) {
  95. date.setTime(date - date.getMilliseconds());
  96. }, function(date, step) {
  97. date.setTime(+date + step * durationSecond);
  98. }, function(start, end) {
  99. return (end - start) / durationSecond;
  100. }, function(date) {
  101. return date.getUTCSeconds();
  102. });
  103. var seconds = second.range;
  104. var minute = newInterval(function(date) {
  105. date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond);
  106. }, function(date, step) {
  107. date.setTime(+date + step * durationMinute);
  108. }, function(start, end) {
  109. return (end - start) / durationMinute;
  110. }, function(date) {
  111. return date.getMinutes();
  112. });
  113. var minutes = minute.range;
  114. var hour = newInterval(function(date) {
  115. date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond - date.getMinutes() * durationMinute);
  116. }, function(date, step) {
  117. date.setTime(+date + step * durationHour);
  118. }, function(start, end) {
  119. return (end - start) / durationHour;
  120. }, function(date) {
  121. return date.getHours();
  122. });
  123. var hours = hour.range;
  124. var day = newInterval(
  125. date => date.setHours(0, 0, 0, 0),
  126. (date, step) => date.setDate(date.getDate() + step),
  127. (start, end) => (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationDay,
  128. date => date.getDate() - 1
  129. );
  130. var days = day.range;
  131. function weekday(i) {
  132. return newInterval(function(date) {
  133. date.setDate(date.getDate() - (date.getDay() + 7 - i) % 7);
  134. date.setHours(0, 0, 0, 0);
  135. }, function(date, step) {
  136. date.setDate(date.getDate() + step * 7);
  137. }, function(start, end) {
  138. return (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationWeek;
  139. });
  140. }
  141. var sunday = weekday(0);
  142. var monday = weekday(1);
  143. var tuesday = weekday(2);
  144. var wednesday = weekday(3);
  145. var thursday = weekday(4);
  146. var friday = weekday(5);
  147. var saturday = weekday(6);
  148. var sundays = sunday.range;
  149. var mondays = monday.range;
  150. var tuesdays = tuesday.range;
  151. var wednesdays = wednesday.range;
  152. var thursdays = thursday.range;
  153. var fridays = friday.range;
  154. var saturdays = saturday.range;
  155. var month = newInterval(function(date) {
  156. date.setDate(1);
  157. date.setHours(0, 0, 0, 0);
  158. }, function(date, step) {
  159. date.setMonth(date.getMonth() + step);
  160. }, function(start, end) {
  161. return end.getMonth() - start.getMonth() + (end.getFullYear() - start.getFullYear()) * 12;
  162. }, function(date) {
  163. return date.getMonth();
  164. });
  165. var months = month.range;
  166. var year = newInterval(function(date) {
  167. date.setMonth(0, 1);
  168. date.setHours(0, 0, 0, 0);
  169. }, function(date, step) {
  170. date.setFullYear(date.getFullYear() + step);
  171. }, function(start, end) {
  172. return end.getFullYear() - start.getFullYear();
  173. }, function(date) {
  174. return date.getFullYear();
  175. });
  176. // An optimized implementation for this simple case.
  177. year.every = function(k) {
  178. return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : newInterval(function(date) {
  179. date.setFullYear(Math.floor(date.getFullYear() / k) * k);
  180. date.setMonth(0, 1);
  181. date.setHours(0, 0, 0, 0);
  182. }, function(date, step) {
  183. date.setFullYear(date.getFullYear() + step * k);
  184. });
  185. };
  186. var years = year.range;
  187. var utcMinute = newInterval(function(date) {
  188. date.setUTCSeconds(0, 0);
  189. }, function(date, step) {
  190. date.setTime(+date + step * durationMinute);
  191. }, function(start, end) {
  192. return (end - start) / durationMinute;
  193. }, function(date) {
  194. return date.getUTCMinutes();
  195. });
  196. var utcMinutes = utcMinute.range;
  197. var utcHour = newInterval(function(date) {
  198. date.setUTCMinutes(0, 0, 0);
  199. }, function(date, step) {
  200. date.setTime(+date + step * durationHour);
  201. }, function(start, end) {
  202. return (end - start) / durationHour;
  203. }, function(date) {
  204. return date.getUTCHours();
  205. });
  206. var utcHours = utcHour.range;
  207. var utcDay = newInterval(function(date) {
  208. date.setUTCHours(0, 0, 0, 0);
  209. }, function(date, step) {
  210. date.setUTCDate(date.getUTCDate() + step);
  211. }, function(start, end) {
  212. return (end - start) / durationDay;
  213. }, function(date) {
  214. return date.getUTCDate() - 1;
  215. });
  216. var utcDays = utcDay.range;
  217. function utcWeekday(i) {
  218. return newInterval(function(date) {
  219. date.setUTCDate(date.getUTCDate() - (date.getUTCDay() + 7 - i) % 7);
  220. date.setUTCHours(0, 0, 0, 0);
  221. }, function(date, step) {
  222. date.setUTCDate(date.getUTCDate() + step * 7);
  223. }, function(start, end) {
  224. return (end - start) / durationWeek;
  225. });
  226. }
  227. var utcSunday = utcWeekday(0);
  228. var utcMonday = utcWeekday(1);
  229. var utcTuesday = utcWeekday(2);
  230. var utcWednesday = utcWeekday(3);
  231. var utcThursday = utcWeekday(4);
  232. var utcFriday = utcWeekday(5);
  233. var utcSaturday = utcWeekday(6);
  234. var utcSundays = utcSunday.range;
  235. var utcMondays = utcMonday.range;
  236. var utcTuesdays = utcTuesday.range;
  237. var utcWednesdays = utcWednesday.range;
  238. var utcThursdays = utcThursday.range;
  239. var utcFridays = utcFriday.range;
  240. var utcSaturdays = utcSaturday.range;
  241. var utcMonth = newInterval(function(date) {
  242. date.setUTCDate(1);
  243. date.setUTCHours(0, 0, 0, 0);
  244. }, function(date, step) {
  245. date.setUTCMonth(date.getUTCMonth() + step);
  246. }, function(start, end) {
  247. return end.getUTCMonth() - start.getUTCMonth() + (end.getUTCFullYear() - start.getUTCFullYear()) * 12;
  248. }, function(date) {
  249. return date.getUTCMonth();
  250. });
  251. var utcMonths = utcMonth.range;
  252. var utcYear = newInterval(function(date) {
  253. date.setUTCMonth(0, 1);
  254. date.setUTCHours(0, 0, 0, 0);
  255. }, function(date, step) {
  256. date.setUTCFullYear(date.getUTCFullYear() + step);
  257. }, function(start, end) {
  258. return end.getUTCFullYear() - start.getUTCFullYear();
  259. }, function(date) {
  260. return date.getUTCFullYear();
  261. });
  262. // An optimized implementation for this simple case.
  263. utcYear.every = function(k) {
  264. return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : newInterval(function(date) {
  265. date.setUTCFullYear(Math.floor(date.getUTCFullYear() / k) * k);
  266. date.setUTCMonth(0, 1);
  267. date.setUTCHours(0, 0, 0, 0);
  268. }, function(date, step) {
  269. date.setUTCFullYear(date.getUTCFullYear() + step * k);
  270. });
  271. };
  272. var utcYears = utcYear.range;
  273. function ticker(year, month, week, day, hour, minute) {
  274. const tickIntervals = [
  275. [second, 1, durationSecond],
  276. [second, 5, 5 * durationSecond],
  277. [second, 15, 15 * durationSecond],
  278. [second, 30, 30 * durationSecond],
  279. [minute, 1, durationMinute],
  280. [minute, 5, 5 * durationMinute],
  281. [minute, 15, 15 * durationMinute],
  282. [minute, 30, 30 * durationMinute],
  283. [ hour, 1, durationHour ],
  284. [ hour, 3, 3 * durationHour ],
  285. [ hour, 6, 6 * durationHour ],
  286. [ hour, 12, 12 * durationHour ],
  287. [ day, 1, durationDay ],
  288. [ day, 2, 2 * durationDay ],
  289. [ week, 1, durationWeek ],
  290. [ month, 1, durationMonth ],
  291. [ month, 3, 3 * durationMonth ],
  292. [ year, 1, durationYear ]
  293. ];
  294. function ticks(start, stop, count) {
  295. const reverse = stop < start;
  296. if (reverse) [start, stop] = [stop, start];
  297. const interval = count && typeof count.range === "function" ? count : tickInterval(start, stop, count);
  298. const ticks = interval ? interval.range(start, +stop + 1) : []; // inclusive stop
  299. return reverse ? ticks.reverse() : ticks;
  300. }
  301. function tickInterval(start, stop, count) {
  302. const target = Math.abs(stop - start) / count;
  303. const i = d3Array.bisector(([,, step]) => step).right(tickIntervals, target);
  304. if (i === tickIntervals.length) return year.every(d3Array.tickStep(start / durationYear, stop / durationYear, count));
  305. if (i === 0) return millisecond.every(Math.max(d3Array.tickStep(start, stop, count), 1));
  306. const [t, step] = tickIntervals[target / tickIntervals[i - 1][2] < tickIntervals[i][2] / target ? i - 1 : i];
  307. return t.every(step);
  308. }
  309. return [ticks, tickInterval];
  310. }
  311. const [utcTicks, utcTickInterval] = ticker(utcYear, utcMonth, utcSunday, utcDay, utcHour, utcMinute);
  312. const [timeTicks, timeTickInterval] = ticker(year, month, sunday, day, hour, minute);
  313. exports.timeDay = day;
  314. exports.timeDays = days;
  315. exports.timeFriday = friday;
  316. exports.timeFridays = fridays;
  317. exports.timeHour = hour;
  318. exports.timeHours = hours;
  319. exports.timeInterval = newInterval;
  320. exports.timeMillisecond = millisecond;
  321. exports.timeMilliseconds = milliseconds;
  322. exports.timeMinute = minute;
  323. exports.timeMinutes = minutes;
  324. exports.timeMonday = monday;
  325. exports.timeMondays = mondays;
  326. exports.timeMonth = month;
  327. exports.timeMonths = months;
  328. exports.timeSaturday = saturday;
  329. exports.timeSaturdays = saturdays;
  330. exports.timeSecond = second;
  331. exports.timeSeconds = seconds;
  332. exports.timeSunday = sunday;
  333. exports.timeSundays = sundays;
  334. exports.timeThursday = thursday;
  335. exports.timeThursdays = thursdays;
  336. exports.timeTickInterval = timeTickInterval;
  337. exports.timeTicks = timeTicks;
  338. exports.timeTuesday = tuesday;
  339. exports.timeTuesdays = tuesdays;
  340. exports.timeWednesday = wednesday;
  341. exports.timeWednesdays = wednesdays;
  342. exports.timeWeek = sunday;
  343. exports.timeWeeks = sundays;
  344. exports.timeYear = year;
  345. exports.timeYears = years;
  346. exports.utcDay = utcDay;
  347. exports.utcDays = utcDays;
  348. exports.utcFriday = utcFriday;
  349. exports.utcFridays = utcFridays;
  350. exports.utcHour = utcHour;
  351. exports.utcHours = utcHours;
  352. exports.utcMillisecond = millisecond;
  353. exports.utcMilliseconds = milliseconds;
  354. exports.utcMinute = utcMinute;
  355. exports.utcMinutes = utcMinutes;
  356. exports.utcMonday = utcMonday;
  357. exports.utcMondays = utcMondays;
  358. exports.utcMonth = utcMonth;
  359. exports.utcMonths = utcMonths;
  360. exports.utcSaturday = utcSaturday;
  361. exports.utcSaturdays = utcSaturdays;
  362. exports.utcSecond = second;
  363. exports.utcSeconds = seconds;
  364. exports.utcSunday = utcSunday;
  365. exports.utcSundays = utcSundays;
  366. exports.utcThursday = utcThursday;
  367. exports.utcThursdays = utcThursdays;
  368. exports.utcTickInterval = utcTickInterval;
  369. exports.utcTicks = utcTicks;
  370. exports.utcTuesday = utcTuesday;
  371. exports.utcTuesdays = utcTuesdays;
  372. exports.utcWednesday = utcWednesday;
  373. exports.utcWednesdays = utcWednesdays;
  374. exports.utcWeek = utcSunday;
  375. exports.utcWeeks = utcSundays;
  376. exports.utcYear = utcYear;
  377. exports.utcYears = utcYears;
  378. Object.defineProperty(exports, '__esModule', { value: true });
  379. })));