Skip to content

Commit 9af9efe

Browse files
author
Sébastien Eustace
committed
Merge branch 'bryanforbes-fix-early-date-transitions'
2 parents b47dc2a + fb2b950 commit 9af9efe

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

pendulum/tz/timezone.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _normalize(self, dt, dst_rule=None): # type: (_D, Optional[str]) -> _D
104104
# We set the fold attribute for later
105105
if dst_rule == POST_TRANSITION:
106106
fold = 1
107-
else:
107+
elif transition.previous is not None:
108108
transition = transition.previous
109109

110110
if transition:
@@ -149,7 +149,7 @@ def _convert(self, dt): # type: (_D) -> _D
149149
transition = dt.tzinfo._lookup_transition(stamp)
150150
offset = transition.ttype.offset
151151

152-
if stamp < transition.local:
152+
if stamp < transition.local and transition.previous is not None:
153153
if (
154154
transition.previous.is_ambiguous(stamp)
155155
and getattr(dt, "fold", 1) == 0
@@ -161,7 +161,7 @@ def _convert(self, dt): # type: (_D) -> _D
161161
stamp -= offset
162162

163163
transition = self._lookup_transition(stamp, is_utc=True)
164-
if stamp < transition.at:
164+
if stamp < transition.at and transition.previous is not None:
165165
transition = transition.previous
166166

167167
offset = transition.ttype.offset
@@ -260,7 +260,7 @@ def _get_transition(self, dt): # type: (_datetime) -> Transition
260260

261261
transition = self._lookup_transition(stamp)
262262

263-
if stamp < transition.local:
263+
if stamp < transition.local and transition.previous is not None:
264264
fold = getattr(dt, "fold", 1)
265265
if transition.is_ambiguous(stamp):
266266
if fold == 0:
@@ -276,7 +276,7 @@ def fromutc(self, dt): # type: (_D) -> _D
276276
stamp = timestamp(dt)
277277

278278
transition = self._lookup_transition(stamp, is_utc=True)
279-
if stamp < transition.at:
279+
if stamp < transition.at and transition.previous is not None:
280280
transition = transition.previous
281281

282282
stamp += transition.ttype.offset

pendulum/tz/zoneinfo/transition.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def utcoffset(self): # type: () -> timedelta
6868
return self._utcoffset
6969

7070
def __contains__(self, stamp): # type: (int) -> bool
71+
if self.previous is None:
72+
return stamp < self.local
73+
7174
return self.previous.local <= stamp < self.local
7275

7376
def __repr__(self): # type: () -> str

tests/tz/test_timezone.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ def test_utcoffset():
303303
assert utcoffset == timedelta(0, -18000)
304304

305305

306+
def test_utcoffset_pre_transition():
307+
tz = pendulum.timezone("America/Chicago")
308+
utcoffset = tz.utcoffset(datetime(1883, 11, 18))
309+
assert utcoffset == timedelta(days=-1, seconds=64800)
310+
311+
306312
def test_dst():
307313
tz = pendulum.timezone("Europe/Amsterdam")
308314
dst = tz.dst(datetime(1940, 7, 1))

0 commit comments

Comments
 (0)