Skip to content

Commit

Permalink
Fix an error when adding an interval to a pendulum instance across D…
Browse files Browse the repository at this point in the history
…ST transition
  • Loading branch information
sdispater committed Mar 22, 2018
1 parent d479c93 commit 1ec1ad0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 8 additions & 3 deletions pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def _add_timedelta(self, delta):
:rtype: DateTime
"""
if isinstance(delta, pendulum.Duration):
if isinstance(delta, pendulum.Period):
return self.add(
years=delta.years,
months=delta.months,
Expand All @@ -589,9 +589,14 @@ def _add_timedelta(self, delta):
seconds=delta.remaining_seconds,
microseconds=delta.microseconds
)
elif isinstance(delta, pendulum.Duration):
return self.add(
years=delta.years,
months=delta.months,
seconds=delta.total_seconds()
)

return self.add(days=delta.days, seconds=delta.seconds,
microseconds=delta.microseconds)
return self.add(seconds=delta.total_seconds())

def _subtract_timedelta(self, delta):
"""
Expand Down
8 changes: 4 additions & 4 deletions pendulum/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ def __new__(cls, days=0, seconds=0, microseconds=0,
self._microseconds = round(total % m * 1e6)
self._seconds = abs(int(total)) % SECONDS_PER_DAY * m

days = abs(int(total)) // SECONDS_PER_DAY * m
self._days = days + (years * 365 + months * 30)
self._remaining_days = abs(days) % 7 * m
self._weeks = abs(days) // 7 * m
_days = abs(int(total)) // SECONDS_PER_DAY * m
self._days = _days + (years * 365 + months * 30)
self._remaining_days = abs(_days) % 7 * m
self._weeks = abs(_days) // 7 * m
self._months = months
self._years = years

Expand Down
7 changes: 7 additions & 0 deletions tests/datetime/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,10 @@ def test_add_time_to_new_transition_repeated_big():
assert dt.timezone_name == 'Europe/Paris'
assert dt.offset == 3600
assert not dt.is_dst()


def test_add_interval():
dt = pendulum.datetime(2017, 3, 11, 10, 45, tz='America/Los_Angeles')
new = dt + pendulum.duration(hours=24)

assert_datetime(new, 2017, 3, 12, 11, 45)

0 comments on commit 1ec1ad0

Please sign in to comment.