UNPKG

4.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.localIntervalMap = exports.year = exports.week = exports.month = exports.day = exports.hour = exports.minute = exports.second = exports.millisecond = exports.createInterval = exports.DURATION_YEAR = exports.DURATION_MONTH = exports.DURATION_WEEK = exports.DURATION_DAY = exports.DURATION_HOUR = exports.DURATION_MINUTE = exports.DURATION_SECOND = void 0;
4exports.DURATION_SECOND = 1000;
5exports.DURATION_MINUTE = exports.DURATION_SECOND * 60;
6exports.DURATION_HOUR = exports.DURATION_MINUTE * 60;
7exports.DURATION_DAY = exports.DURATION_HOUR * 24;
8exports.DURATION_WEEK = exports.DURATION_DAY * 7;
9exports.DURATION_MONTH = exports.DURATION_DAY * 30;
10exports.DURATION_YEAR = exports.DURATION_DAY * 365;
11function createInterval(duration, floorish, offseti, field) {
12 const adjust = (date, step) => {
13 const test = (date) => field(date) % step === 0;
14 let i = step;
15 while (i && !test(date)) {
16 offseti(date, -1);
17 i -= 1;
18 }
19 return date;
20 };
21 const floori = (date, step) => {
22 if (step)
23 adjust(date, step);
24 floorish(date);
25 };
26 const floor = (date, step) => {
27 const d = new Date(+date);
28 floori(d, step);
29 return d;
30 };
31 const ceil = (date, step) => {
32 const d = new Date(+date - 1);
33 floori(d, step);
34 offseti(d, step);
35 floori(d);
36 return d;
37 };
38 const range = (start, stop, step, shouldAdjust) => {
39 const ticks = [];
40 const roundStep = Math.floor(step);
41 const t = shouldAdjust ? ceil(start, step) : ceil(start);
42 for (let i = t; i < stop; offseti(i, roundStep), floori(i)) {
43 ticks.push(new Date(+i));
44 }
45 return ticks;
46 };
47 return {
48 ceil,
49 floor,
50 range,
51 duration,
52 };
53}
54exports.createInterval = createInterval;
55exports.millisecond = createInterval(1, (date) => date, (date, step = 1) => {
56 date.setTime(+date + step);
57}, (date) => date.getTime());
58exports.second = createInterval(exports.DURATION_SECOND, (date) => {
59 date.setMilliseconds(0);
60}, (date, step = 1) => {
61 date.setTime(+date + exports.DURATION_SECOND * step);
62}, (date) => date.getSeconds());
63exports.minute = createInterval(exports.DURATION_MINUTE, (date) => {
64 date.setSeconds(0, 0);
65}, (date, step = 1) => {
66 date.setTime(+date + exports.DURATION_MINUTE * step);
67}, (date) => date.getMinutes());
68exports.hour = createInterval(exports.DURATION_HOUR, (date) => {
69 date.setMinutes(0, 0, 0);
70}, (date, step = 1) => {
71 date.setTime(+date + exports.DURATION_HOUR * step);
72}, (date) => date.getHours());
73exports.day = createInterval(exports.DURATION_DAY, (date) => {
74 date.setHours(0, 0, 0, 0);
75}, (date, step = 1) => {
76 date.setTime(+date + exports.DURATION_DAY * step);
77}, (date) => date.getDate() - 1);
78exports.month = createInterval(exports.DURATION_MONTH, (date) => {
79 date.setDate(1);
80 date.setHours(0, 0, 0, 0);
81}, (date, step = 1) => {
82 const month = date.getMonth();
83 date.setMonth(month + step);
84}, (date) => date.getMonth());
85exports.week = createInterval(exports.DURATION_WEEK, (date) => {
86 date.setDate(date.getDate() - (date.getDay() % 7));
87 date.setHours(0, 0, 0, 0);
88}, (date, step = 1) => {
89 date.setDate(date.getDate() + 7 * step);
90}, (date) => {
91 const start = exports.month.floor(date);
92 const end = new Date(+date);
93 return Math.floor((+end - +start) / exports.DURATION_WEEK);
94});
95exports.year = createInterval(exports.DURATION_YEAR, (date) => {
96 date.setMonth(0, 1);
97 date.setHours(0, 0, 0, 0);
98}, (date, step = 1) => {
99 const year = date.getFullYear();
100 date.setFullYear(year + step);
101}, (date) => date.getFullYear());
102exports.localIntervalMap = {
103 millisecond: exports.millisecond,
104 second: exports.second,
105 minute: exports.minute,
106 hour: exports.hour,
107 day: exports.day,
108 week: exports.week,
109 month: exports.month,
110 year: exports.year,
111};
112//# sourceMappingURL=time-interval.js.map
\No newline at end of file