Skip to content
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

Fix case where transition can be None #350

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
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