Skip to content

Commit 116ef84

Browse files
committed
Probe backwards for bound windows before the scan range
1 parent ef17651 commit 116ef84

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎hypothesis/src/hypothesis/strategies/_internal/datetime.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,22 @@ def _interesting_instants(tz, lo, hi):
402402
try:
403403
if tz is None:
404404
transitions = ()
405-
elif lo > _SCAN_HI or hi < _SCAN_LO:
406-
# The window is wholly outside the usual scan range: probe a few
405+
elif lo > _SCAN_HI:
406+
# The window is wholly above the usual scan range: probe a few
407407
# years directly, enough to cover any recurring annual rule.
408408
try:
409409
cap = lo + _FALLBACK_SCAN
410410
except OverflowError: # within a few years of datetime.max
411411
cap = hi
412412
transitions = _probe_transitions(tz, lo, min(hi, cap))
413+
elif hi < _SCAN_LO:
414+
# Wholly before the scan range: probe backwards from the window's
415+
# end.
416+
try:
417+
floor = hi - _FALLBACK_SCAN
418+
except OverflowError: # within a few years of datetime.min
419+
floor = lo
420+
transitions = _probe_transitions(tz, max(lo, floor), hi)
413421
else:
414422
transitions = _transitions(tz)
415423
except Exception:

‎hypothesis/tests/cover/test_datetimes.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,31 @@ def test_fixed_offset_timezones_has_no_interesting_instants():
289289
assert nearest == 0
290290

291291

292+
class _EarlyTransitionTimezone(dt.tzinfo):
293+
# A timezone with an offset change in 1699
294+
def utcoffset(self, value):
295+
naive = value.replace(tzinfo=None)
296+
return dt.timedelta(hours=2 * (naive >= dt.datetime(1699, 6, 1)))
297+
298+
def dst(self, value):
299+
return dt.timedelta(0)
300+
301+
def tzname(self, value):
302+
return "Early"
303+
304+
305+
def test_early_transition_timezone_interesting_instants():
306+
lo, hi = dt.datetime.min, dt.datetime(1700, 1, 1)
307+
instants, nearest = _interesting_instants(_EarlyTransitionTimezone(), lo, hi)
308+
assert instants == (dt.datetime(1699, 6, 1),)
309+
assert nearest == 0
310+
311+
312+
def test_interesting_instants_near_datetime_min():
313+
lo, hi = dt.datetime.min, dt.datetime(3, 1, 1)
314+
assert _interesting_instants(_EarlyTransitionTimezone(), lo, hi) == ((), 0)
315+
316+
292317
UTC = dt.timezone.utc
293318
fixed_offsets = st.builds(
294319
dt.timezone, st.timedeltas(dt.timedelta(hours=-23), dt.timedelta(hours=23))

0 commit comments

Comments
 (0)