Skip to content

Handle transition.previous being None #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pendulum/tz/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _normalize(
# We set the fold attribute for later
if dst_rule == POST_TRANSITION:
fold = 1
else:
elif transition.previous is not None:
transition = transition.previous

if transition.is_ambiguous(sec):
Expand Down Expand Up @@ -149,7 +149,7 @@ def _convert(self, dt): # type: (datetime) -> datetime
transition = dt.tzinfo._lookup_transition(stamp)
offset = transition.ttype.offset

if stamp < transition.local:
if stamp < transition.local and transition.previous is not None:
if (
transition.previous.is_ambiguous(stamp)
and getattr(dt, "fold", 1) == 0
Expand All @@ -161,7 +161,7 @@ def _convert(self, dt): # type: (datetime) -> datetime
stamp -= offset

transition = self._lookup_transition(stamp, is_utc=True)
if stamp < transition.at:
if stamp < transition.at and transition.previous is not None:
transition = transition.previous

offset = transition.ttype.offset
Expand Down Expand Up @@ -254,7 +254,7 @@ def _get_transition(self, dt): # type: (datetime) -> Transition

transition = self._lookup_transition(stamp)

if stamp < transition.local:
if stamp < transition.local and transition.previous is not None:
fold = getattr(dt, "fold", 1)
if transition.is_ambiguous(stamp):
if fold == 0:
Expand All @@ -270,7 +270,7 @@ def fromutc(self, dt): # type: (datetime) -> datetime
stamp = timestamp(dt)

transition = self._lookup_transition(stamp, is_utc=True)
if stamp < transition.at:
if stamp < transition.at and transition.previous is not None:
transition = transition.previous

stamp += transition.ttype.offset
Expand Down
9 changes: 6 additions & 3 deletions pendulum/tz/zoneinfo/transition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta
from typing import Union
from typing import Union, Optional

from .transition_type import TransitionType

Expand Down Expand Up @@ -51,7 +51,7 @@ def ttype(self): # type: () -> TransitionType
return self._ttype

@property
def previous(self): # type: () -> Transition
def previous(self): # type: () -> Optional[Transition]
return self._previous

@property
Expand All @@ -67,7 +67,10 @@ def is_missing(self, stamp): # type: (int) -> bool
def utcoffset(self): # type: () -> timedelta
return self._utcoffset

def __contains__(self, stamp): # type: () -> bool
def __contains__(self, stamp): # type: (int) -> bool
if self.previous is None:
return stamp < self.local

return self.previous.local <= stamp < self.local

def __repr__(self): # type: () -> str
Expand Down
6 changes: 6 additions & 0 deletions tests/tz/test_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ def test_utcoffset():
assert utcoffset == timedelta(0, -18000)


def test_utcoffset_pre_transition():
tz = pendulum.timezone("America/Chicago")
utcoffset = tz.utcoffset(datetime(1883, 11, 18))
assert utcoffset == timedelta(days=-1, seconds=64800)


def test_dst():
tz = pendulum.timezone("Europe/Amsterdam")
dst = tz.dst(datetime(1940, 7, 1))
Expand Down