source: node_modules/d3-time/dist/d3-time.js@ ba17441

Last change on this file since ba17441 was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

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