@unchainedshop/core 4.6.2 → 4.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/utils/schedule.js +164 -28
  2. package/package.json +1 -1
@@ -99,37 +99,173 @@ function parseText(text) {
99
99
  throw new Error(`Unsupported time unit: ${unit}`);
100
100
  }
101
101
  }
102
+ function findNextInSorted(sorted, minVal) {
103
+ for (const v of sorted) {
104
+ if (v >= minVal)
105
+ return v;
106
+ }
107
+ return null;
108
+ }
109
+ function daysInMonth(year, month) {
110
+ return new Date(year, month, 0).getDate();
111
+ }
102
112
  function getNextOccurrences(scheduleData, count, referenceDate) {
103
113
  const results = [];
104
- const schedule = scheduleData.schedules[0];
105
- const current = new Date(referenceDate.getTime());
106
- current.setMilliseconds(0);
107
- current.setSeconds(current.getSeconds() + 1);
108
- const maxIterations = 366 * 24 * 60 * 60;
109
- let iterations = 0;
110
- while (results.length < count && iterations < maxIterations) {
111
- iterations++;
112
- const month = current.getMonth() + 1;
113
- const dayOfMonth = current.getDate();
114
- const dayOfWeek = current.getDay();
115
- const hour = current.getHours();
116
- const minute = current.getMinutes();
117
- const second = current.getSeconds();
118
- const matchesMonth = !schedule.M || schedule.M.includes(month);
119
- const matchesDay = !schedule.D || schedule.D.includes(dayOfMonth);
120
- const matchesDayOfWeek = !schedule.d || schedule.d.includes(dayOfWeek);
121
- const matchesHour = !schedule.h || schedule.h.includes(hour);
122
- const matchesMinute = !schedule.m || schedule.m.includes(minute);
123
- const matchesSecond = !schedule.s || schedule.s.includes(second);
124
- if (matchesMonth &&
125
- matchesDay &&
126
- matchesDayOfWeek &&
127
- matchesHour &&
128
- matchesMinute &&
129
- matchesSecond) {
130
- results.push(new Date(current));
114
+ const sched = scheduleData.schedules[0];
115
+ const start = new Date(referenceDate.getTime());
116
+ start.setMilliseconds(0);
117
+ start.setSeconds(start.getSeconds() + 1);
118
+ let year = start.getFullYear();
119
+ let month = start.getMonth() + 1;
120
+ let day = start.getDate();
121
+ let hour = start.getHours();
122
+ let minute = start.getMinutes();
123
+ let second = start.getSeconds();
124
+ const maxYear = year + 5;
125
+ search: while (results.length < count && year <= maxYear) {
126
+ if (sched.M) {
127
+ const nextMonth = findNextInSorted(sched.M, month);
128
+ if (nextMonth === null) {
129
+ year++;
130
+ month = sched.M[0];
131
+ day = sched.D ? sched.D[0] : 1;
132
+ hour = sched.h ? sched.h[0] : 0;
133
+ minute = sched.m ? sched.m[0] : 0;
134
+ second = sched.s ? sched.s[0] : 0;
135
+ continue search;
136
+ }
137
+ if (nextMonth > month) {
138
+ month = nextMonth;
139
+ day = sched.D ? sched.D[0] : 1;
140
+ hour = sched.h ? sched.h[0] : 0;
141
+ minute = sched.m ? sched.m[0] : 0;
142
+ second = sched.s ? sched.s[0] : 0;
143
+ }
144
+ }
145
+ const maxDay = daysInMonth(year, month);
146
+ if (sched.D || sched.d) {
147
+ let found = false;
148
+ let candidateDay = day;
149
+ while (candidateDay <= maxDay) {
150
+ const matchesD = !sched.D || sched.D.includes(candidateDay);
151
+ if (matchesD) {
152
+ const dow = new Date(year, month - 1, candidateDay).getDay();
153
+ const matchesDow = !sched.d || sched.d.includes(dow);
154
+ if (matchesDow) {
155
+ if (candidateDay > day) {
156
+ hour = sched.h ? sched.h[0] : 0;
157
+ minute = sched.m ? sched.m[0] : 0;
158
+ second = sched.s ? sched.s[0] : 0;
159
+ }
160
+ day = candidateDay;
161
+ found = true;
162
+ break;
163
+ }
164
+ }
165
+ candidateDay++;
166
+ }
167
+ if (!found) {
168
+ month++;
169
+ if (month > 12) {
170
+ year++;
171
+ month = sched.M ? sched.M[0] : 1;
172
+ }
173
+ else if (sched.M) {
174
+ const nextM = findNextInSorted(sched.M, month);
175
+ if (nextM === null) {
176
+ year++;
177
+ month = sched.M[0];
178
+ }
179
+ else {
180
+ month = nextM;
181
+ }
182
+ }
183
+ day = sched.D ? sched.D[0] : 1;
184
+ hour = sched.h ? sched.h[0] : 0;
185
+ minute = sched.m ? sched.m[0] : 0;
186
+ second = sched.s ? sched.s[0] : 0;
187
+ continue search;
188
+ }
189
+ }
190
+ else if (day > maxDay) {
191
+ month++;
192
+ if (month > 12) {
193
+ year++;
194
+ month = sched.M ? sched.M[0] : 1;
195
+ }
196
+ else if (sched.M) {
197
+ const nextM = findNextInSorted(sched.M, month);
198
+ if (nextM === null) {
199
+ year++;
200
+ month = sched.M[0];
201
+ }
202
+ else {
203
+ month = nextM;
204
+ }
205
+ }
206
+ day = 1;
207
+ hour = sched.h ? sched.h[0] : 0;
208
+ minute = sched.m ? sched.m[0] : 0;
209
+ second = sched.s ? sched.s[0] : 0;
210
+ continue search;
211
+ }
212
+ if (hour > 23) {
213
+ day++;
214
+ hour = sched.h ? sched.h[0] : 0;
215
+ minute = sched.m ? sched.m[0] : 0;
216
+ second = sched.s ? sched.s[0] : 0;
217
+ continue search;
218
+ }
219
+ if (sched.h) {
220
+ const nextHour = findNextInSorted(sched.h, hour);
221
+ if (nextHour === null) {
222
+ day++;
223
+ hour = sched.h[0];
224
+ minute = sched.m ? sched.m[0] : 0;
225
+ second = sched.s ? sched.s[0] : 0;
226
+ continue search;
227
+ }
228
+ if (nextHour > hour) {
229
+ hour = nextHour;
230
+ minute = sched.m ? sched.m[0] : 0;
231
+ second = sched.s ? sched.s[0] : 0;
232
+ }
233
+ }
234
+ if (minute > 59) {
235
+ hour++;
236
+ minute = sched.m ? sched.m[0] : 0;
237
+ second = sched.s ? sched.s[0] : 0;
238
+ continue search;
239
+ }
240
+ if (sched.m) {
241
+ const nextMinute = findNextInSorted(sched.m, minute);
242
+ if (nextMinute === null) {
243
+ hour++;
244
+ minute = sched.m[0];
245
+ second = sched.s ? sched.s[0] : 0;
246
+ continue search;
247
+ }
248
+ if (nextMinute > minute) {
249
+ minute = nextMinute;
250
+ second = sched.s ? sched.s[0] : 0;
251
+ }
252
+ }
253
+ if (second > 59) {
254
+ minute++;
255
+ second = sched.s ? sched.s[0] : 0;
256
+ continue search;
257
+ }
258
+ if (sched.s) {
259
+ const nextSecond = findNextInSorted(sched.s, second);
260
+ if (nextSecond === null) {
261
+ minute++;
262
+ second = sched.s[0];
263
+ continue search;
264
+ }
265
+ second = nextSecond;
131
266
  }
132
- current.setSeconds(current.getSeconds() + 1);
267
+ results.push(new Date(year, month - 1, day, hour, minute, second));
268
+ second++;
133
269
  }
134
270
  return results;
135
271
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unchainedshop/core",
3
3
  "description": "Core orchestration package for the Unchained Engine with business services and directors",
4
- "version": "4.6.2",
4
+ "version": "4.8.0",
5
5
  "main": "lib/core-index.js",
6
6
  "types": "lib/core-index.d.ts",
7
7
  "type": "module",