-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.ts
190 lines (170 loc) · 4.59 KB
/
next.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* @packageDocumentation
* @ignore
*/
import { Field, Schedule } from "./parse";
function findNext(
current: number,
schedule: undefined | number[]
): [val: number, carry: boolean, changed: boolean] {
if (!schedule || schedule.length === 0) {
return [current, false, false];
}
for (const n of schedule) {
if (n >= current) {
return [n, false, n !== current];
}
}
return [schedule[0], true, true];
}
/** @returns Boolean indicating if we've moved to a new day */
function moveToNextValidTime(current: Date, schedule: Schedule): boolean {
let [hour, carryDay, hourChange] = findNext(
current.getHours(),
schedule[Field.hour]
);
if (hourChange) {
current.setHours(
hour,
findNext(0, schedule[Field.minute])[0],
findNext(0, schedule[Field.second])[0]
);
return carryDay;
}
let [minute, carryHour, minuteChange] = findNext(
current.getMinutes(),
schedule[Field.minute]
);
if (minuteChange) {
if (carryHour) {
let [hour, carryDay] = findNext(
current.getHours() + 1,
schedule[Field.hour]
);
current.setHours(hour, minute, findNext(0, schedule[Field.second])[0]);
return carryDay;
} else {
current.setMinutes(minute, findNext(0, schedule[Field.second])[0]);
return false;
}
}
const [second, carryMinute, secondChange] = findNext(
current.getSeconds(),
schedule[Field.second]
);
if (!carryMinute) {
if (secondChange) {
current.setSeconds(second);
}
return false;
}
[minute, carryHour] = findNext(
current.getMinutes() + 1,
schedule[Field.minute]
);
if (!carryHour) {
current.setMinutes(minute, second);
return false;
}
[hour, carryDay] = findNext(current.getHours() + 1, schedule[Field.hour]);
current.setHours(hour, minute, second);
return carryDay;
}
function isDateValid(current: Date, schedule: Schedule): boolean {
const year = schedule[Field.year];
const month = schedule[Field.month];
const dayOfMonth = schedule[Field.dayOfMonth];
const dayOfWeek = schedule[Field.dayOfWeek];
return (
(!year || year.indexOf(current.getFullYear()) > -1) &&
(!month || month.indexOf(current.getMonth()) > -1) &&
(!dayOfMonth || dayOfMonth.indexOf(current.getDate()) > -1) &&
(!dayOfWeek || dayOfWeek.indexOf(current.getDay()) > -1)
);
}
/** @returns Boolean indicating if we've found a next possible day */
function moveToNextPossibleDate(current: Date, schedule: Schedule): boolean {
// Increment date
current.setDate(current.getDate() + 1);
let [year, noMoreYears, yearChanged] = findNext(
current.getFullYear(),
schedule[Field.year]
);
if (noMoreYears) return false;
if (yearChanged) {
current.setFullYear(
year,
findNext(0, schedule[Field.month])[0],
findNext(1, schedule[Field.dayOfMonth])[0]
);
return true;
}
let [month, carryYear, monthChanged] = findNext(
current.getMonth(),
schedule[Field.month]
);
if (carryYear) {
current.setFullYear(
current.getFullYear() + 1,
month,
findNext(1, schedule[Field.dayOfMonth])[0]
);
return true;
}
if (monthChanged) {
current.setMonth(month, findNext(1, schedule[Field.dayOfMonth])[0]);
return true;
}
const [date, carryMonth, dateChanged] = findNext(
current.getDate(),
schedule[Field.dayOfMonth]
);
if (carryMonth) {
current.setMonth(current.getMonth() + 1, date);
return true;
}
if (dateChanged) {
current.setDate(date);
return true;
}
return true;
}
/** @returns 0 if unchanged, 1 if changed, -1 if unavailable */
function moveToNextValidDate(current: Date, schedule: Schedule): 0 | 1 | -1 {
let isValid = isDateValid(current, schedule);
if (isValid) return 0;
do {
if (!moveToNextPossibleDate(current, schedule)) {
return -1;
}
} while (!isDateValid(current, schedule));
return 1;
}
/**
* Internal function for finding the next date from a parsed schedule.
*
* Avoid depending on this method directly as it might change without notice.
* @internal
*/
export const next = (schedule: Schedule, from: Date): Date | undefined => {
const date = new Date(from.getTime());
switch (moveToNextValidDate(date, schedule)) {
case -1:
return undefined;
case 0:
date.setSeconds(date.getSeconds() + 1, 0);
break;
case 1:
date.setHours(0, 0, 0, 0);
break;
}
const carryDay = moveToNextValidTime(date, schedule);
if (!carryDay) {
return date;
}
date.setDate(date.getDate() + 1);
if (moveToNextValidDate(date, schedule) === -1) {
return undefined;
}
return date;
};