croniter-mojo 0.2.5__py3-none-any.whl
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.
- croniter_mojo/__init__.py +35 -0
- croniter_mojo/__main__.py +37 -0
- croniter_mojo/_engine.py +530 -0
- croniter_mojo/_errors.py +29 -0
- croniter_mojo/_fields.py +333 -0
- croniter_mojo/_native.py +270 -0
- croniter_mojo/core.py +124 -0
- croniter_mojo-0.2.5.dist-info/METADATA +92 -0
- croniter_mojo-0.2.5.dist-info/RECORD +10 -0
- croniter_mojo-0.2.5.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""croniter_mojo: drop-in faster 5-field cron iteration, powered by a Mojo kernel.
|
|
2
|
+
|
|
3
|
+
API-compatible with the `croniter` package's get_next/get_prev for the
|
|
4
|
+
supported scope, returning datetimes identical to the oracle. Runs on a
|
|
5
|
+
native Mojo kernel where available and falls back to a pure-Python engine
|
|
6
|
+
everywhere else (including Windows). Force the fallback with
|
|
7
|
+
``CRONITER_MOJO_DISABLE_NATIVE=1``; inspect the active backend with
|
|
8
|
+
:func:`backend_info`.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from ._errors import (
|
|
14
|
+
CroniterBadCronError,
|
|
15
|
+
CroniterBadDateError,
|
|
16
|
+
CroniterMojoError,
|
|
17
|
+
CroniterNotAlphaError,
|
|
18
|
+
CroniterUnsupportedSyntaxError,
|
|
19
|
+
)
|
|
20
|
+
from ._native import backend_info, native_available
|
|
21
|
+
from .core import get_next, get_prev
|
|
22
|
+
|
|
23
|
+
__version__ = "0.2.5" # x-release-please-version
|
|
24
|
+
__all__ = [
|
|
25
|
+
"get_next",
|
|
26
|
+
"get_prev",
|
|
27
|
+
"native_available",
|
|
28
|
+
"backend_info",
|
|
29
|
+
"CroniterMojoError",
|
|
30
|
+
"CroniterBadCronError",
|
|
31
|
+
"CroniterNotAlphaError",
|
|
32
|
+
"CroniterBadDateError",
|
|
33
|
+
"CroniterUnsupportedSyntaxError",
|
|
34
|
+
"__version__",
|
|
35
|
+
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""`python -m croniter_mojo` — quickstart smoke for end users and CI.
|
|
2
|
+
|
|
3
|
+
Runs a handful of get_next/get_prev calls (including DST boundaries), prints
|
|
4
|
+
the results and a checksum, and reports the active backend. Both the native
|
|
5
|
+
and the forced-fallback (CRONITER_MOJO_DISABLE_NATIVE=1) runs must print the
|
|
6
|
+
same datetimes and checksum.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from datetime import datetime
|
|
12
|
+
from zoneinfo import ZoneInfo
|
|
13
|
+
|
|
14
|
+
import croniter_mojo
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def main() -> None:
|
|
18
|
+
et = ZoneInfo("America/New_York")
|
|
19
|
+
results = [
|
|
20
|
+
croniter_mojo.get_next("0 9 * * mon-fri", datetime(2026, 9, 19, 14, 30, 45)),
|
|
21
|
+
croniter_mojo.get_next("0 0 29 2 *", datetime(2026, 9, 19)),
|
|
22
|
+
croniter_mojo.get_next("0 0 * * 5#3", datetime(2026, 9, 19)),
|
|
23
|
+
croniter_mojo.get_prev("0 0 L * *", datetime(2026, 9, 19)),
|
|
24
|
+
croniter_mojo.get_next("30 2 * * *", datetime(2026, 3, 7, 12, 0, tzinfo=et)),
|
|
25
|
+
croniter_mojo.get_next("30 1 * * *", datetime(2026, 10, 31, 12, 0, tzinfo=et)),
|
|
26
|
+
croniter_mojo.get_prev("45 1 * * *", datetime(2026, 11, 1, 1, 30, tzinfo=et, fold=1)),
|
|
27
|
+
]
|
|
28
|
+
for r in results:
|
|
29
|
+
print(r.isoformat())
|
|
30
|
+
total = sum(r.toordinal() * 86400 + r.hour * 3600 + r.minute * 60 for r in results)
|
|
31
|
+
print(f"checksum: {total}")
|
|
32
|
+
info = croniter_mojo.backend_info()
|
|
33
|
+
print(f"native: {info['native_available']} ({info.get('native_source') or info.get('error')})")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
if __name__ == "__main__":
|
|
37
|
+
main()
|
croniter_mojo/_engine.py
ADDED
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
"""Pure-Python fallback engine + the shared datetime/DST driver.
|
|
2
|
+
|
|
3
|
+
Two layers:
|
|
4
|
+
|
|
5
|
+
1. :class:`FallbackEngine` — a pure-Python mirror of the Mojo kernel's
|
|
6
|
+
wall-clock seek (same algorithm, same semantics, integer bitmasks).
|
|
7
|
+
Used when the native library is unavailable.
|
|
8
|
+
|
|
9
|
+
2. :func:`find_next` / :func:`find_prev` — the timezone-aware driver shared
|
|
10
|
+
by BOTH backends. It drives the engine over wall-clock candidates and
|
|
11
|
+
resolves them against a ``zoneinfo`` timezone. The scan is anchored at
|
|
12
|
+
the wall time of the start INSTANT (for an imaginary start — a wall
|
|
13
|
+
time that never occurred — that is the real wall clock across the gap),
|
|
14
|
+
and candidates are filtered by wall order against that anchor:
|
|
15
|
+
|
|
16
|
+
- unambiguous wall time -> one candidate instant
|
|
17
|
+
- ambiguous wall time -> two instants (fall-back folds); a whole
|
|
18
|
+
contiguous ambiguous region is enumerated fold=0 pass then fold=1
|
|
19
|
+
pass for next (reverse for prev). When the anchor sits inside the
|
|
20
|
+
region, the anchor's own fold pass comes first (walls beyond the
|
|
21
|
+
anchor), then the other pass (next) / only the anchor's pass (prev
|
|
22
|
+
with fold=0) / both passes (prev with fold=1) — i.e. fold-major
|
|
23
|
+
ordering, matching the oracle.
|
|
24
|
+
- imaginary wall time -> depends on the hour field (oracle-verified):
|
|
25
|
+
* hour value set restricted (!= full 0..23): an imaginary schedule
|
|
26
|
+
match maps to the gap-end instant (the first real instant after a
|
|
27
|
+
spring-forward gap), even though it may not match the schedule
|
|
28
|
+
* hour value set full ("*", "*/1", "0-23", ...): an imaginary match W
|
|
29
|
+
maps to W+gap for next (W-gap for prev) when that shifted wall time
|
|
30
|
+
itself matches the schedule, otherwise to the gap-end instant
|
|
31
|
+
|
|
32
|
+
The search horizon is +/-50 years from the start year; exhausting it
|
|
33
|
+
raises :class:`CroniterBadDateError`, like the oracle.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
from __future__ import annotations
|
|
37
|
+
|
|
38
|
+
from datetime import datetime, timedelta, timezone
|
|
39
|
+
|
|
40
|
+
from ._errors import CroniterBadDateError
|
|
41
|
+
from ._fields import Schedule
|
|
42
|
+
|
|
43
|
+
_ONE_MINUTE = timedelta(minutes=1)
|
|
44
|
+
_ONE_HOUR = timedelta(hours=1)
|
|
45
|
+
_YEAR_SPAN = 50 # oracle search horizon: +/-50 years around the start year
|
|
46
|
+
|
|
47
|
+
_REAL, _AMBIGUOUS, _IMAGINARY = range(3)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _instant(dt: datetime) -> datetime:
|
|
51
|
+
"""Absolute instant of an aware datetime as a naive UTC datetime.
|
|
52
|
+
|
|
53
|
+
utcoffset() subtraction is exact integer math (no float timestamps),
|
|
54
|
+
and it works for imaginary and ambiguous wall times alike.
|
|
55
|
+
"""
|
|
56
|
+
return dt.replace(tzinfo=None) - dt.utcoffset()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _wall_of_instant(start: datetime, tz) -> datetime:
|
|
60
|
+
"""Wall time in `tz` of the start's absolute instant, minute-truncated.
|
|
61
|
+
|
|
62
|
+
Equals the truncated start wall for real/ambiguous starts; for an
|
|
63
|
+
imaginary start it is the real wall clock of the same instant (across
|
|
64
|
+
the gap). Computed with exact aware-datetime arithmetic (no floats).
|
|
65
|
+
"""
|
|
66
|
+
wall = start.astimezone(timezone.utc).astimezone(tz).replace(tzinfo=None)
|
|
67
|
+
return wall.replace(second=0, microsecond=0)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _is_leap(y: int) -> bool:
|
|
71
|
+
return (y % 4 == 0 and y % 100 != 0) or y % 400 == 0
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _dim(y: int, m: int) -> int:
|
|
75
|
+
if m == 2:
|
|
76
|
+
return 29 if _is_leap(y) else 28
|
|
77
|
+
if m in (4, 6, 9, 11):
|
|
78
|
+
return 30
|
|
79
|
+
return 31
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _cron_dow(y: int, m: int, d: int) -> int:
|
|
83
|
+
"""Cron day-of-week (Sunday == 0); Howard Hinnant's days_from_civil."""
|
|
84
|
+
yy = y - (1 if m <= 2 else 0)
|
|
85
|
+
era = yy // 400 # Python // is floor division
|
|
86
|
+
yoe = yy - era * 400
|
|
87
|
+
mp = (m + 9) % 12
|
|
88
|
+
doy = (153 * mp + 2) // 5 + d - 1
|
|
89
|
+
doe = yoe * 365 + yoe // 4 - yoe // 100 + doy
|
|
90
|
+
days = era * 146097 + doe - 719468
|
|
91
|
+
return (days + 4) % 7
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _day_match(s: Schedule, y: int, m: int, d: int) -> bool:
|
|
95
|
+
dom_side = bool((s.doms >> d) & 1) or (s.dom_has_l and d == _dim(y, m))
|
|
96
|
+
dow = _cron_dow(y, m, d)
|
|
97
|
+
if s.dow_has_hash:
|
|
98
|
+
nth = (d - 1) // 7 + 1
|
|
99
|
+
dow_side = any(hd == dow and hn == nth for hd, hn in s.hash_specs)
|
|
100
|
+
else:
|
|
101
|
+
dow_side = bool((s.dows >> dow) & 1)
|
|
102
|
+
if not s.dom_restricted and not s.dow_restricted:
|
|
103
|
+
return True
|
|
104
|
+
if not s.dow_restricted:
|
|
105
|
+
return dom_side
|
|
106
|
+
if not s.dom_restricted:
|
|
107
|
+
return dow_side
|
|
108
|
+
if s.day_or:
|
|
109
|
+
# Hash + restricted dom + OR: hash rule alone decides (the parser
|
|
110
|
+
# ran the static feasibility gate for this combination).
|
|
111
|
+
if s.dow_has_hash:
|
|
112
|
+
return dow_side
|
|
113
|
+
return dom_side or dow_side
|
|
114
|
+
return dom_side and dow_side
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class FallbackEngine:
|
|
118
|
+
"""Pure-Python wall-clock seek over a parsed schedule. Mirror of the kernel."""
|
|
119
|
+
|
|
120
|
+
def __init__(self, schedule: Schedule) -> None:
|
|
121
|
+
self._s = schedule
|
|
122
|
+
|
|
123
|
+
def seek_next(
|
|
124
|
+
self, y: int, mo: int, d: int, h: int, mi: int, year_bound: int
|
|
125
|
+
) -> tuple[int, int, int, int, int] | None:
|
|
126
|
+
s = self._s
|
|
127
|
+
while y <= year_bound:
|
|
128
|
+
if not (s.months >> mo) & 1:
|
|
129
|
+
mo += 1
|
|
130
|
+
if mo > 12:
|
|
131
|
+
mo = 1
|
|
132
|
+
y += 1
|
|
133
|
+
d, h, mi = 1, 0, 0
|
|
134
|
+
continue
|
|
135
|
+
if d > _dim(y, mo):
|
|
136
|
+
mo += 1
|
|
137
|
+
if mo > 12:
|
|
138
|
+
mo = 1
|
|
139
|
+
y += 1
|
|
140
|
+
d, h, mi = 1, 0, 0
|
|
141
|
+
continue
|
|
142
|
+
if not _day_match(s, y, mo, d):
|
|
143
|
+
d += 1
|
|
144
|
+
h, mi = 0, 0
|
|
145
|
+
continue
|
|
146
|
+
if h > 23:
|
|
147
|
+
d += 1
|
|
148
|
+
h, mi = 0, 0
|
|
149
|
+
continue
|
|
150
|
+
hh = -1
|
|
151
|
+
for cand in range(h, 24):
|
|
152
|
+
if (s.hours >> cand) & 1:
|
|
153
|
+
hh = cand
|
|
154
|
+
break
|
|
155
|
+
if hh < 0:
|
|
156
|
+
d += 1
|
|
157
|
+
h, mi = 0, 0
|
|
158
|
+
continue
|
|
159
|
+
if hh > h:
|
|
160
|
+
h = hh
|
|
161
|
+
mi = 0
|
|
162
|
+
mm = -1
|
|
163
|
+
for cand in range(mi, 60):
|
|
164
|
+
if (s.minutes >> cand) & 1:
|
|
165
|
+
mm = cand
|
|
166
|
+
break
|
|
167
|
+
if mm < 0:
|
|
168
|
+
h += 1
|
|
169
|
+
mi = 0
|
|
170
|
+
continue
|
|
171
|
+
return (y, mo, d, h, mm)
|
|
172
|
+
return None
|
|
173
|
+
|
|
174
|
+
def seek_prev(
|
|
175
|
+
self, y: int, mo: int, d: int, h: int, mi: int, year_bound: int
|
|
176
|
+
) -> tuple[int, int, int, int, int] | None:
|
|
177
|
+
s = self._s
|
|
178
|
+
while y >= year_bound:
|
|
179
|
+
if mo < 1:
|
|
180
|
+
mo = 12
|
|
181
|
+
y -= 1
|
|
182
|
+
d, h, mi = 31, 23, 59
|
|
183
|
+
continue
|
|
184
|
+
if not (s.months >> mo) & 1:
|
|
185
|
+
mo -= 1
|
|
186
|
+
d, h, mi = 31, 23, 59
|
|
187
|
+
continue
|
|
188
|
+
if d > _dim(y, mo):
|
|
189
|
+
d = _dim(y, mo)
|
|
190
|
+
if d < 1:
|
|
191
|
+
mo -= 1
|
|
192
|
+
d, h, mi = 31, 23, 59
|
|
193
|
+
continue
|
|
194
|
+
if not _day_match(s, y, mo, d):
|
|
195
|
+
d -= 1
|
|
196
|
+
h, mi = 23, 59
|
|
197
|
+
continue
|
|
198
|
+
if h < 0:
|
|
199
|
+
d -= 1
|
|
200
|
+
h, mi = 23, 59
|
|
201
|
+
continue
|
|
202
|
+
hh = -1
|
|
203
|
+
for cand in range(h, -1, -1):
|
|
204
|
+
if (s.hours >> cand) & 1:
|
|
205
|
+
hh = cand
|
|
206
|
+
break
|
|
207
|
+
if hh < 0:
|
|
208
|
+
d -= 1
|
|
209
|
+
h, mi = 23, 59
|
|
210
|
+
continue
|
|
211
|
+
if hh < h:
|
|
212
|
+
h = hh
|
|
213
|
+
mi = 59
|
|
214
|
+
mm = -1
|
|
215
|
+
for cand in range(mi, -1, -1):
|
|
216
|
+
if (s.minutes >> cand) & 1:
|
|
217
|
+
mm = cand
|
|
218
|
+
break
|
|
219
|
+
if mm < 0:
|
|
220
|
+
h -= 1
|
|
221
|
+
mi = 59
|
|
222
|
+
continue
|
|
223
|
+
return (y, mo, d, h, mm)
|
|
224
|
+
return None
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
# --- timezone classification -------------------------------------------------
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _classify(naive: datetime, tz) -> int:
|
|
231
|
+
"""Wall-time kind: _REAL (one instant), _AMBIGUOUS (two), _IMAGINARY (none)."""
|
|
232
|
+
off0 = naive.replace(tzinfo=tz, fold=0).utcoffset()
|
|
233
|
+
off1 = naive.replace(tzinfo=tz, fold=1).utcoffset()
|
|
234
|
+
if off0 == off1:
|
|
235
|
+
return _REAL
|
|
236
|
+
return _AMBIGUOUS if off0 > off1 else _IMAGINARY
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _region(naive: datetime, tz, kind: int) -> tuple[datetime, datetime]:
|
|
240
|
+
"""Maximal contiguous run of same-kind wall minutes containing `naive`."""
|
|
241
|
+
a = naive
|
|
242
|
+
while True:
|
|
243
|
+
try:
|
|
244
|
+
prev = a - _ONE_MINUTE
|
|
245
|
+
except OverflowError:
|
|
246
|
+
break
|
|
247
|
+
if _classify(prev, tz) != kind:
|
|
248
|
+
break
|
|
249
|
+
a = prev
|
|
250
|
+
b = naive
|
|
251
|
+
while True:
|
|
252
|
+
try:
|
|
253
|
+
nxt = b + _ONE_MINUTE
|
|
254
|
+
except OverflowError:
|
|
255
|
+
break
|
|
256
|
+
if _classify(nxt, tz) != kind:
|
|
257
|
+
break
|
|
258
|
+
b = nxt
|
|
259
|
+
return a, b # inclusive
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _matches_in_region(engine, direction: int, a: datetime, b: datetime, year_bound: int) -> list[datetime]:
|
|
263
|
+
"""All schedule wall matches inside [a, b], ascending for next, descending for prev."""
|
|
264
|
+
out: list[datetime] = []
|
|
265
|
+
if direction > 0:
|
|
266
|
+
cursor = a
|
|
267
|
+
while cursor <= b:
|
|
268
|
+
comps = engine.seek_next(cursor.year, cursor.month, cursor.day, cursor.hour, cursor.minute, year_bound)
|
|
269
|
+
if comps is None:
|
|
270
|
+
break
|
|
271
|
+
w = datetime(*comps)
|
|
272
|
+
if w > b:
|
|
273
|
+
break
|
|
274
|
+
out.append(w)
|
|
275
|
+
cursor = w + _ONE_MINUTE
|
|
276
|
+
else:
|
|
277
|
+
cursor = b
|
|
278
|
+
while cursor >= a:
|
|
279
|
+
comps = engine.seek_prev(cursor.year, cursor.month, cursor.day, cursor.hour, cursor.minute, year_bound)
|
|
280
|
+
if comps is None:
|
|
281
|
+
break
|
|
282
|
+
w = datetime(*comps)
|
|
283
|
+
if w < a:
|
|
284
|
+
break
|
|
285
|
+
out.append(w)
|
|
286
|
+
cursor = w - _ONE_MINUTE
|
|
287
|
+
return out
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def _handle_region_next(engine, schedule: Schedule, a: datetime, b: datetime, kind: int, tz, anchor: datetime, start_fold: int, start_utc: datetime, year_bound: int):
|
|
291
|
+
"""First candidate produced by non-real region [a, b] after `anchor`, or None.
|
|
292
|
+
|
|
293
|
+
Mapped candidates (the gap end) must be strictly after the start
|
|
294
|
+
instant; an instant exactly equal to the start's qualifies only when
|
|
295
|
+
its wall time is past the anchor (oracle-verified: this is how a real
|
|
296
|
+
match right at a gap end is included for an imaginary start).
|
|
297
|
+
"""
|
|
298
|
+
|
|
299
|
+
def next_ok(wall: datetime, cand: datetime) -> bool:
|
|
300
|
+
inst = _instant(cand)
|
|
301
|
+
return inst > start_utc or (inst == start_utc and wall > anchor)
|
|
302
|
+
|
|
303
|
+
if kind == _IMAGINARY:
|
|
304
|
+
end_wall = b + _ONE_MINUTE # first real wall after the gap (gap end)
|
|
305
|
+
ge_cand = end_wall.replace(tzinfo=tz, fold=0)
|
|
306
|
+
if schedule.hour_restricted:
|
|
307
|
+
# Any schedule match in [a, end_wall] maps to the gap end.
|
|
308
|
+
comps = engine.seek_next(a.year, a.month, a.day, a.hour, a.minute, year_bound)
|
|
309
|
+
if comps is not None and datetime(*comps) <= end_wall and next_ok(end_wall, ge_cand):
|
|
310
|
+
return ge_cand
|
|
311
|
+
return None
|
|
312
|
+
# Full hour field: an imaginary match W maps to the gap end when the
|
|
313
|
+
# anchor wall precedes W-gap (oracle-verified boundary); otherwise W
|
|
314
|
+
# is skipped and the scan continues to the next real match.
|
|
315
|
+
gap = end_wall - a
|
|
316
|
+
cursor = a
|
|
317
|
+
while cursor <= b:
|
|
318
|
+
comps = engine.seek_next(cursor.year, cursor.month, cursor.day, cursor.hour, cursor.minute, year_bound)
|
|
319
|
+
if comps is None:
|
|
320
|
+
break
|
|
321
|
+
w = datetime(*comps)
|
|
322
|
+
if w > b:
|
|
323
|
+
break
|
|
324
|
+
if anchor < w - gap and next_ok(end_wall, ge_cand):
|
|
325
|
+
return ge_cand
|
|
326
|
+
cursor = w + _ONE_MINUTE
|
|
327
|
+
return None
|
|
328
|
+
# Ambiguous region: fold passes in absolute order (f0 then f1).
|
|
329
|
+
# With an f0 anchor the cross-fold (f1) pass covers ALL region walls
|
|
330
|
+
# when the fold is a full hour (or the hour field is restricted);
|
|
331
|
+
# sub-hour folds (Lord Howe) only yield the f1 walls before the anchor.
|
|
332
|
+
matches = _matches_in_region(engine, +1, a, b, year_bound)
|
|
333
|
+
gap = (b + _ONE_MINUTE) - a
|
|
334
|
+
cross_all = schedule.hour_restricted or gap >= _ONE_HOUR
|
|
335
|
+
if a <= anchor <= b:
|
|
336
|
+
if start_fold == 0:
|
|
337
|
+
passes = [(0, [m for m in matches if m > anchor])]
|
|
338
|
+
if cross_all:
|
|
339
|
+
passes.append((1, matches))
|
|
340
|
+
else:
|
|
341
|
+
# Sub-hour fold: f1 walls strictly before the anchor, plus
|
|
342
|
+
# the region-start wall when the anchor sits exactly on it.
|
|
343
|
+
passes.append((1, [m for m in matches if m < anchor or (anchor == a and m == a)]))
|
|
344
|
+
else:
|
|
345
|
+
passes = [(1, [m for m in matches if m > anchor])]
|
|
346
|
+
else:
|
|
347
|
+
passes = [(0, matches)]
|
|
348
|
+
if cross_all:
|
|
349
|
+
passes.append((1, matches))
|
|
350
|
+
for fold, ms in passes:
|
|
351
|
+
for m in ms:
|
|
352
|
+
return m.replace(tzinfo=tz, fold=fold)
|
|
353
|
+
return None
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _handle_region_prev(engine, schedule: Schedule, a: datetime, b: datetime, kind: int, tz, anchor: datetime, start_fold: int, allow_equal: bool, start_utc: datetime, year_bound: int):
|
|
357
|
+
"""Last candidate produced by non-real region [a, b] before `anchor`, or None."""
|
|
358
|
+
|
|
359
|
+
def wall_ok(w: datetime) -> bool:
|
|
360
|
+
return w < anchor or (allow_equal and w == anchor)
|
|
361
|
+
|
|
362
|
+
def prev_ok(cand: datetime) -> bool:
|
|
363
|
+
return _instant(cand) < start_utc
|
|
364
|
+
|
|
365
|
+
if kind == _IMAGINARY:
|
|
366
|
+
end_wall = b + _ONE_MINUTE
|
|
367
|
+
ge_cand = end_wall.replace(tzinfo=tz, fold=0)
|
|
368
|
+
if schedule.hour_restricted:
|
|
369
|
+
comps = engine.seek_prev(end_wall.year, end_wall.month, end_wall.day, end_wall.hour, end_wall.minute, year_bound)
|
|
370
|
+
if comps is not None and datetime(*comps) >= a and wall_ok(end_wall) and prev_ok(ge_cand):
|
|
371
|
+
return ge_cand
|
|
372
|
+
return None
|
|
373
|
+
# Full hour field: an imaginary match W (descending) maps to the gap
|
|
374
|
+
# end when the anchor wall follows W+gap (oracle-verified boundary);
|
|
375
|
+
# otherwise W is skipped and the scan continues below.
|
|
376
|
+
gap = end_wall - a
|
|
377
|
+
cursor = b
|
|
378
|
+
while cursor >= a:
|
|
379
|
+
comps = engine.seek_prev(cursor.year, cursor.month, cursor.day, cursor.hour, cursor.minute, year_bound)
|
|
380
|
+
if comps is None:
|
|
381
|
+
break
|
|
382
|
+
w = datetime(*comps)
|
|
383
|
+
if w < a:
|
|
384
|
+
break
|
|
385
|
+
if anchor > w + gap and wall_ok(end_wall) and prev_ok(ge_cand):
|
|
386
|
+
return ge_cand
|
|
387
|
+
cursor = w - _ONE_MINUTE
|
|
388
|
+
return None
|
|
389
|
+
# Ambiguous region: fold passes in absolute order (f1 then f0 for prev).
|
|
390
|
+
# With an f1 anchor the cross-fold (f0) pass covers ALL region walls
|
|
391
|
+
# when the fold is a full hour (or the hour field is restricted);
|
|
392
|
+
# sub-hour folds only yield the f0 walls after the anchor.
|
|
393
|
+
matches = _matches_in_region(engine, -1, a, b, year_bound)
|
|
394
|
+
gap = (b + _ONE_MINUTE) - a
|
|
395
|
+
cross_all = schedule.hour_restricted or gap >= _ONE_HOUR
|
|
396
|
+
if a <= anchor <= b:
|
|
397
|
+
if start_fold == 1:
|
|
398
|
+
passes = [(1, [m for m in matches if wall_ok(m)])]
|
|
399
|
+
if cross_all:
|
|
400
|
+
passes.append((0, matches))
|
|
401
|
+
elif anchor == a:
|
|
402
|
+
# Sub-hour fold: the f0 pass fires only when the anchor is
|
|
403
|
+
# exactly the region start; then it covers all region walls.
|
|
404
|
+
passes.append((0, matches))
|
|
405
|
+
else:
|
|
406
|
+
passes = [(0, [m for m in matches if wall_ok(m)])]
|
|
407
|
+
else:
|
|
408
|
+
passes = [(1, matches)]
|
|
409
|
+
if cross_all:
|
|
410
|
+
passes.append((0, matches))
|
|
411
|
+
for fold, ms in passes:
|
|
412
|
+
for m in ms:
|
|
413
|
+
return m.replace(tzinfo=tz, fold=fold)
|
|
414
|
+
return None
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
# --- shared drivers ------------------------------------------------------------
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
def find_next(engine, schedule: Schedule, start: datetime) -> datetime:
|
|
421
|
+
"""First schedule instant strictly after `start` (minute precision)."""
|
|
422
|
+
try:
|
|
423
|
+
return _find_next(engine, schedule, start)
|
|
424
|
+
except OverflowError:
|
|
425
|
+
raise CroniterBadDateError("failed to find next date") from None
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
def _find_next(engine, schedule: Schedule, start: datetime) -> datetime:
|
|
429
|
+
tz = start.tzinfo
|
|
430
|
+
year_bound = min(start.year + _YEAR_SPAN, 9999)
|
|
431
|
+
tw = start.replace(second=0, microsecond=0)
|
|
432
|
+
if tz is None:
|
|
433
|
+
cur = tw + _ONE_MINUTE
|
|
434
|
+
comps = engine.seek_next(cur.year, cur.month, cur.day, cur.hour, cur.minute, year_bound)
|
|
435
|
+
if comps is None:
|
|
436
|
+
raise CroniterBadDateError("failed to find next date")
|
|
437
|
+
return datetime(*comps)
|
|
438
|
+
|
|
439
|
+
# Anchor the wall-clock scan at the wall time OF THE START INSTANT.
|
|
440
|
+
# For real/ambiguous starts this equals the truncated start wall; for an
|
|
441
|
+
# imaginary start it is the real wall clock of the same instant, across
|
|
442
|
+
# the gap — which is where the oracle begins iterating.
|
|
443
|
+
anchor = _wall_of_instant(start, tz)
|
|
444
|
+
start_fold = start.fold
|
|
445
|
+
start_utc = _instant(start)
|
|
446
|
+
if _classify(anchor, tz) != _REAL:
|
|
447
|
+
# The anchor sits in a fold: qualifying candidates may hide at wall
|
|
448
|
+
# minutes at or below the anchor, so begin the scan at the anchor
|
|
449
|
+
# itself and let region handling decide.
|
|
450
|
+
cur = anchor
|
|
451
|
+
else:
|
|
452
|
+
cur = anchor + _ONE_MINUTE
|
|
453
|
+
while True:
|
|
454
|
+
kind_cur = _classify(cur, tz)
|
|
455
|
+
if kind_cur != _REAL:
|
|
456
|
+
a, b = _region(cur, tz, kind_cur)
|
|
457
|
+
result = _handle_region_next(engine, schedule, a, b, kind_cur, tz, anchor, start_fold, start_utc, year_bound)
|
|
458
|
+
if result is not None:
|
|
459
|
+
return result
|
|
460
|
+
cur = b + _ONE_MINUTE
|
|
461
|
+
continue
|
|
462
|
+
comps = engine.seek_next(cur.year, cur.month, cur.day, cur.hour, cur.minute, year_bound)
|
|
463
|
+
if comps is None:
|
|
464
|
+
raise CroniterBadDateError("failed to find next date")
|
|
465
|
+
w = datetime(*comps)
|
|
466
|
+
kind = _classify(w, tz)
|
|
467
|
+
if kind == _REAL:
|
|
468
|
+
return w.replace(tzinfo=tz, fold=0)
|
|
469
|
+
a, b = _region(w, tz, kind)
|
|
470
|
+
result = _handle_region_next(engine, schedule, a, b, kind, tz, anchor, start_fold, start_utc, year_bound)
|
|
471
|
+
if result is not None:
|
|
472
|
+
return result
|
|
473
|
+
cur = b + _ONE_MINUTE
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
def find_prev(engine, schedule: Schedule, start: datetime) -> datetime:
|
|
477
|
+
"""Last schedule instant strictly before `start` (minute precision)."""
|
|
478
|
+
try:
|
|
479
|
+
return _find_prev(engine, schedule, start)
|
|
480
|
+
except OverflowError:
|
|
481
|
+
raise CroniterBadDateError("failed to find next date") from None
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
def _find_prev(engine, schedule: Schedule, start: datetime) -> datetime:
|
|
485
|
+
tz = start.tzinfo
|
|
486
|
+
year_bound = max(start.year - _YEAR_SPAN, 1)
|
|
487
|
+
tw = start.replace(second=0, microsecond=0)
|
|
488
|
+
if tz is None:
|
|
489
|
+
cur = tw if (start.second or start.microsecond) else tw - _ONE_MINUTE
|
|
490
|
+
comps = engine.seek_prev(cur.year, cur.month, cur.day, cur.hour, cur.minute, year_bound)
|
|
491
|
+
if comps is None:
|
|
492
|
+
raise CroniterBadDateError("failed to find next date")
|
|
493
|
+
return datetime(*comps)
|
|
494
|
+
|
|
495
|
+
anchor = _wall_of_instant(start, tz)
|
|
496
|
+
start_fold = start.fold
|
|
497
|
+
start_utc = _instant(start)
|
|
498
|
+
# With sub-minute seconds the truncated start wall minute still
|
|
499
|
+
# qualifies for prev (its instant precedes the start's).
|
|
500
|
+
allow_equal = bool(start.second or start.microsecond)
|
|
501
|
+
if _classify(anchor, tz) != _REAL:
|
|
502
|
+
# The anchor sits in a fold: qualifying candidates may hide at wall
|
|
503
|
+
# minutes at or above the anchor (the other fold pass, or the gap
|
|
504
|
+
# end), so begin the scan at the anchor itself.
|
|
505
|
+
cur = anchor
|
|
506
|
+
elif allow_equal:
|
|
507
|
+
cur = anchor
|
|
508
|
+
else:
|
|
509
|
+
cur = anchor - _ONE_MINUTE
|
|
510
|
+
while True:
|
|
511
|
+
kind_cur = _classify(cur, tz)
|
|
512
|
+
if kind_cur != _REAL:
|
|
513
|
+
a, b = _region(cur, tz, kind_cur)
|
|
514
|
+
result = _handle_region_prev(engine, schedule, a, b, kind_cur, tz, anchor, start_fold, allow_equal, start_utc, year_bound)
|
|
515
|
+
if result is not None:
|
|
516
|
+
return result
|
|
517
|
+
cur = a - _ONE_MINUTE
|
|
518
|
+
continue
|
|
519
|
+
comps = engine.seek_prev(cur.year, cur.month, cur.day, cur.hour, cur.minute, year_bound)
|
|
520
|
+
if comps is None:
|
|
521
|
+
raise CroniterBadDateError("failed to find next date")
|
|
522
|
+
w = datetime(*comps)
|
|
523
|
+
kind = _classify(w, tz)
|
|
524
|
+
if kind == _REAL:
|
|
525
|
+
return w.replace(tzinfo=tz, fold=1) # cosmetic fold, like the oracle's prev
|
|
526
|
+
a, b = _region(w, tz, kind)
|
|
527
|
+
result = _handle_region_prev(engine, schedule, a, b, kind, tz, anchor, start_fold, allow_equal, start_utc, year_bound)
|
|
528
|
+
if result is not None:
|
|
529
|
+
return result
|
|
530
|
+
cur = a - _ONE_MINUTE
|
croniter_mojo/_errors.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""croniter_mojo exception hierarchy.
|
|
2
|
+
|
|
3
|
+
Names mirror the public exception classes of the `croniter` PyPI package
|
|
4
|
+
(the black-box oracle for this project) so that calling code can catch the
|
|
5
|
+
same error categories; they are independent classes, not subclasses of the
|
|
6
|
+
oracle's. All inherit from :class:`CroniterMojoError`.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CroniterMojoError(Exception):
|
|
13
|
+
"""Base class for all croniter_mojo errors."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class CroniterBadCronError(CroniterMojoError):
|
|
17
|
+
"""The cron expression is malformed or has out-of-range values."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class CroniterNotAlphaError(CroniterBadCronError):
|
|
21
|
+
"""A field contains a token that is neither numeric nor a known name."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class CroniterUnsupportedSyntaxError(CroniterBadCronError):
|
|
25
|
+
"""The expression uses syntax outside the supported 5-field scope."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class CroniterBadDateError(CroniterMojoError):
|
|
29
|
+
"""No matching datetime exists within the search horizon."""
|