From ec421764c6d7416fc400f0301d87f02ccd1c75ee Mon Sep 17 00:00:00 2001 From: FFspiky Date: Tue, 24 Nov 2026 12:53:09 +0800 Subject: [PATCH] Preserve fold when removing timezone information --- CHANGELOG.md | 5 +++++ docs/docs/instantiation.md | 5 +++-- src/pendulum/datetime.py | 3 ++- tests/datetime/test_naive.py | 23 +++++++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51f84145c..44cb1c2b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## Unreleased + +### Fixed +- Preserve the DST fold when removing timezone information with `DateTime.naive()`. + ## [3.3.0] - 2026-10-12 ### Added diff --git a/docs/docs/instantiation.md b/docs/docs/instantiation.md index a49c6a26c..f2ee57792 100644 --- a/docs/docs/instantiation.md +++ b/docs/docs/instantiation.md @@ -88,8 +88,9 @@ and each has their time value set to `00:00:00`. ``` Pendulum enforces timezone aware datetimes, and using them is the preferred and recommended way -of using the library. However, if you really need a **naive** `DateTime` object, the `naive()` helper -is there for you. +of using the library. If you need a **naive** `DateTime` object, `naive()` removes the timezone +and preserves the local time and its `fold` value. During a clock rollback, this distinguishes the +first occurrence of a local time from the second. ```python >>> import pendulum diff --git a/src/pendulum/datetime.py b/src/pendulum/datetime.py index 96aff25b2..3b74755b6 100644 --- a/src/pendulum/datetime.py +++ b/src/pendulum/datetime.py @@ -317,7 +317,7 @@ def time(self) -> Time: def naive(self) -> Self: """ - Return the DateTime without timezone information. + Return the DateTime without timezone information, preserving the fold. """ return self.__class__( self.year, @@ -327,6 +327,7 @@ def naive(self) -> Self: self.minute, self.second, self.microsecond, + fold=self.fold, ) def on(self, year: int, month: int, day: int) -> Self: diff --git a/tests/datetime/test_naive.py b/tests/datetime/test_naive.py index 95bd2d52b..578c6a921 100644 --- a/tests/datetime/test_naive.py +++ b/tests/datetime/test_naive.py @@ -1,5 +1,7 @@ from __future__ import annotations +import pytest + import pendulum from tests.conftest import assert_datetime @@ -76,3 +78,24 @@ def test_naive_method(): assert dt.tzinfo is None assert dt.timezone is None assert dt.timezone_name is None + + +@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_naive_method_preserves_fold(tz, year, month, day, hour, minute, fold): + dt = pendulum.datetime(year, month, day, hour, minute, 12, 345678, tz=tz, fold=fold) + + naive = dt.naive() + + assert isinstance(naive, pendulum.DateTime) + assert_datetime(naive, year, month, day, hour, minute, 12, 345678) + assert naive.tzinfo is None + assert naive.fold == fold + assert naive.replace(tzinfo=dt.tzinfo).timestamp() == dt.timestamp() + assert naive.naive().fold == fold