From b545b77c412c7e8eb0db54a2b8cb8de321fd6e0c Mon Sep 17 00:00:00 2001 From: Jace Browning Date: Sat, 2 Mar 2019 21:28:10 -0500 Subject: [PATCH] Fix case where transition can be None --- pendulum/tz/timezone.py | 39 ++++++++++++++++++++------------------- tests/date/test_sub.py | 7 ++++++- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/pendulum/tz/timezone.py b/pendulum/tz/timezone.py index a79242ed..60e38554 100644 --- a/pendulum/tz/timezone.py +++ b/pendulum/tz/timezone.py @@ -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): diff --git a/tests/date/test_sub.py b/tests/date/test_sub.py index b7f781af..1edbd463 100644 --- a/tests/date/test_sub.py +++ b/tests/date/test_sub.py @@ -2,7 +2,7 @@ import pendulum -from datetime import timedelta +from datetime import datetime, timedelta from ..conftest import assert_date @@ -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