Skip to content
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
20 changes: 14 additions & 6 deletions src/pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import calendar
import datetime
import traceback

from typing import TYPE_CHECKING
from typing import Any
Expand Down Expand Up @@ -1238,10 +1237,6 @@ def __add__(self, other: datetime.timedelta) -> Self:
if not isinstance(other, datetime.timedelta):
return NotImplemented

caller = traceback.extract_stack(limit=2)[0].name
if caller == "astimezone":
return super().__add__(other)

return self._add_timedelta_(other)

def __radd__(self, other: datetime.timedelta) -> Self:
Expand Down Expand Up @@ -1278,7 +1273,20 @@ def combine(
return cls.instance(datetime.datetime.combine(date, time), tz=tzinfo)

def astimezone(self, tz: datetime.tzinfo | None = None) -> Self:
dt = super().astimezone(tz)
# Convert a plain datetime: tzinfo.fromutc() implementations add
# timedeltas to the value they're given and expect the standard
# library's arithmetic, not the overridden __add__.
dt = datetime.datetime(
self.year,
self.month,
self.day,
self.hour,
self.minute,
self.second,
self.microsecond,
tzinfo=self.tzinfo,
fold=self.fold,
).astimezone(tz)

return self.__class__(
dt.year,
Expand Down
9 changes: 9 additions & 0 deletions tests/datetime/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ def test_add_timedelta():
assert d.microsecond == 777777


def test_add_timedelta_does_not_depend_on_caller_name():
def astimezone(d):
return d + timedelta(hours=24)

d = pendulum.datetime(2013, 3, 30, 12, tz="Europe/Paris")

assert_datetime(astimezone(d), 2013, 3, 31, 13)


def test_add_duration():
duration = pendulum.duration(
years=2, months=3, days=6, seconds=45, microseconds=123456
Expand Down
25 changes: 25 additions & 0 deletions tests/datetime/test_timezone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from __future__ import annotations

from datetime import datetime

import pytest

from dateutil import tz

import pendulum

from tests.conftest import assert_datetime
Expand Down Expand Up @@ -36,3 +42,22 @@ def test_astimezone():
d = d.astimezone(pendulum.timezone("Europe/Paris"))
assert d.timezone_name == "Europe/Paris"
assert_datetime(d, now.year, now.month, now.day, now.hour + 1, now.minute)


def test_astimezone_with_tzinfo_doing_arithmetic_in_fromutc():
# dateutil's tzoffset.fromutc() adds its offset to the datetime it's given
cest = tz.tzoffset("CEST", 7200)
d = pendulum.datetime(2024, 7, 1, 12)

d = d.astimezone(cest)
assert isinstance(d, pendulum.DateTime)
assert d.tzinfo is cest
assert_datetime(d, 2024, 7, 1, 14)


@pytest.mark.parametrize("fold, hour", [(0, 0), (1, 1)])
def test_astimezone_respects_fold(fold, hour):
d = pendulum.datetime(2024, 10, 27, 2, 30, tz="Europe/Paris", fold=fold)

d = d.astimezone(pendulum.UTC)
assert d == datetime(2024, 10, 27, hour, 30, tzinfo=pendulum.UTC)
Loading