Skip to content

Commit b545b77

Browse files
committed
Fix case where transition can be None
1 parent 23f290f commit b545b77

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

pendulum/tz/timezone.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,26 @@ def _normalize(
108108
else:
109109
transition = transition.previous
110110

111-
if transition.is_ambiguous(sec):
112-
# Ambiguous time
113-
if dst_rule == TRANSITION_ERROR:
114-
raise AmbiguousTime(dt)
115-
116-
# We set the fold attribute for later
117-
if dst_rule == POST_TRANSITION:
118-
fold = 1
119-
elif transition.is_missing(sec):
120-
# Skipped time
121-
if dst_rule == TRANSITION_ERROR:
122-
raise NonExistingTime(dt)
123-
124-
# We adjust accordingly
125-
if dst_rule == POST_TRANSITION:
126-
sec += transition.fix
127-
fold = 1
128-
else:
129-
sec -= transition.fix
111+
if transition:
112+
if transition.is_ambiguous(sec):
113+
# Ambiguous time
114+
if dst_rule == TRANSITION_ERROR:
115+
raise AmbiguousTime(dt)
116+
117+
# We set the fold attribute for later
118+
if dst_rule == POST_TRANSITION:
119+
fold = 1
120+
elif transition.is_missing(sec):
121+
# Skipped time
122+
if dst_rule == TRANSITION_ERROR:
123+
raise NonExistingTime(dt)
124+
125+
# We adjust accordingly
126+
if dst_rule == POST_TRANSITION:
127+
sec += transition.fix
128+
fold = 1
129+
else:
130+
sec -= transition.fix
130131

131132
kwargs = {"tzinfo": self}
132133
if _HAS_FOLD or isinstance(dt, pendulum.DateTime):

tests/date/test_sub.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pendulum
44

5-
from datetime import timedelta
5+
from datetime import datetime, timedelta
66

77
from ..conftest import assert_date
88

@@ -43,6 +43,11 @@ def test_subtract_days_negative():
4343
assert pendulum.Date(1975, 5, 30).subtract(days=-1).day == 31
4444

4545

46+
def test_subtract_days_max():
47+
delta = pendulum.now() - pendulum.instance(datetime.min)
48+
assert pendulum.now().subtract(days=delta.days - 1).year == 1
49+
50+
4651
def test_subtract_weeks_positive():
4752
assert pendulum.Date(1975, 5, 28).subtract(weeks=1).day == 21
4853

0 commit comments

Comments
 (0)