Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ def date(self) -> Date:
return Date(self.year, self.month, self.day)

def time(self) -> Time:
return Time(self.hour, self.minute, self.second, self.microsecond)
return Time(
self.hour, self.minute, self.second, self.microsecond, fold=self.fold
)

def naive(self) -> Self:
"""
Expand Down
38 changes: 38 additions & 0 deletions tests/datetime/test_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ def test_time(p, dt):
assert p.time() == dt.time()


@pytest.mark.parametrize("fold", [0, 1])
@pytest.mark.parametrize(
"tzinfo",
[
None,
datetime_timezone.utc,
zoneinfo.ZoneInfo("America/New_York"),
zoneinfo.ZoneInfo("Australia/Lord_Howe"),
],
)
def test_time_preserves_fold(fold, tzinfo):
native = datetime(2024, 11, 3, 1, 30, 12, 345678, tzinfo=tzinfo, fold=fold)
dt = pendulum.DateTime(2024, 11, 3, 1, 30, 12, 345678, tzinfo=tzinfo, fold=fold)

result = dt.time()

assert isinstance(result, pendulum.Time)
assert result == native.time()
assert result.tzinfo is None
assert result.fold == native.time().fold


@pytest.mark.parametrize("fold", [0, 1])
@pytest.mark.parametrize(
("tz", "year", "month", "day", "hour", "minute"),
[
("America/New_York", 2024, 11, 3, 1, 30),
("Australia/Lord_Howe", 2024, 4, 7, 1, 45),
],
)
def test_time_round_trip_during_fold(fold, tz, year, month, day, hour, minute):
dt = pendulum.datetime(year, month, day, hour, minute, tz=tz, fold=fold)

rebuilt = datetime.combine(dt.date(), dt.time(), tzinfo=dt.tzinfo)

assert rebuilt.timestamp() == dt.timestamp()


def test_timetz(p, dt):
assert p.timetz() == dt.timetz()

Expand Down