Skip to content

Commit

Permalink
Fix case where transition can be None
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Mar 3, 2019
1 parent 23f290f commit b545b77
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
39 changes: 20 additions & 19 deletions pendulum/tz/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,26 @@ def _normalize(
else:
transition = transition.previous

if transition.is_ambiguous(sec):
# Ambiguous time
if dst_rule == TRANSITION_ERROR:
raise AmbiguousTime(dt)

# We set the fold attribute for later
if dst_rule == POST_TRANSITION:
fold = 1
elif transition.is_missing(sec):
# Skipped time
if dst_rule == TRANSITION_ERROR:
raise NonExistingTime(dt)

# We adjust accordingly
if dst_rule == POST_TRANSITION:
sec += transition.fix
fold = 1
else:
sec -= transition.fix
if transition:
if transition.is_ambiguous(sec):
# Ambiguous time
if dst_rule == TRANSITION_ERROR:
raise AmbiguousTime(dt)

# We set the fold attribute for later
if dst_rule == POST_TRANSITION:
fold = 1
elif transition.is_missing(sec):
# Skipped time
if dst_rule == TRANSITION_ERROR:
raise NonExistingTime(dt)

# We adjust accordingly
if dst_rule == POST_TRANSITION:
sec += transition.fix
fold = 1
else:
sec -= transition.fix

kwargs = {"tzinfo": self}
if _HAS_FOLD or isinstance(dt, pendulum.DateTime):
Expand Down
7 changes: 6 additions & 1 deletion tests/date/test_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pendulum

from datetime import timedelta
from datetime import datetime, timedelta

from ..conftest import assert_date

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


def test_subtract_days_max():
delta = pendulum.now() - pendulum.instance(datetime.min)
assert pendulum.now().subtract(days=delta.days - 1).year == 1


def test_subtract_weeks_positive():
assert pendulum.Date(1975, 5, 28).subtract(weeks=1).day == 21

Expand Down

0 comments on commit b545b77

Please sign in to comment.