Skip to content

Resolve start_of/end_of to the first/last instant of the date - #1094

Open
bardrop wants to merge 2 commits into
python-pendulum:masterfrom
bardrop-oss:fix/start-end-of-day-fold
Open

bardrop wants to merge 2 commits into
python-pendulum:masterfrom
bardrop-oss:fix/start-end-of-day-fold

Conversation

@bardrop

@bardrop bardrop commented Nov 17, 2026

Copy link
Copy Markdown

Fixes #1093

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code. (no doc change needed: behavior now matches the docs, "time to 00:00:00" / "23:59:59.999999" of that date)

What

start_of() / end_of() for day, week, month, year, decade and century went through set(), which keeps self.fold (since #713). When a DST change happens at midnight:

  • fold=0 (what now(), from_timestamp() and in_timezone() give for normal times) moved a skipped midnight back into the previous day: pendulum.today("Africa/Cairo") on 2026-04-24 returned 2026-04-23 23:00+02:00.
  • fold=1 (the default of pendulum.datetime()) picked the later of two midnights (Havana 2012-11-04 gave 00:00-05:00, not 00:00-04:00).
  • end_of("day") with fold=0 on a day whose last hour repeats (Cairo 2026-10-29, Santiago 2023-04-01) ended one hour early.

How

New _first_instant() / _last_instant() helpers build the wall time with fold=0 and fold=1, drop a result that left the requested date (skipped time), and return the earliest (start) or latest (end) instant. The instant is compared in UTC terms because same-tzinfo comparisons ignore the offset. Hour/minute/second units are unchanged, so they still stay in the same pass of a repeated hour (the existing test_*_on_date_before/after_transition tests still pass).

Tests

  • 4 new tests in tests/datetime/test_start_end_of.py (skipped midnight with fold=0 for day and month, repeated midnight with fold=1, repeated last hour with fold=0). All 4 fail on master and pass here.
  • Brute-force check over every tzdata zone, 1995-2035, a few hours around each transition, both folds (148,232 cases): master gives 1,817 start_of("day")/end_of("day") results that are on the wrong date or not the first/last instant of it; this branch gives 0.
  • Full suite: 1873 passed, 3 skipped. The one failure, tests/test_main.py::test_instance_with_aware_datetime_any_tzinfo, fails the same way on master (local timezone dependent, see Use an explicit generic timezone in instance() tests #1072).
  • ruff format --check and mypy clean for the changed files.

start_of() and end_of() for day and larger units went through set(),
which keeps self.fold. When a DST change happens at midnight, fold=0
moved a skipped midnight back into the previous day and fold=1 picked
the later of two midnights. Try both folds and keep the earliest (or
latest) instant that is still on the requested date.

Fixes python-pendulum#1093
@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Author

Friendly ping: the workflow run still needs a maintainer's approval to start. The PR has no conflicts; happy to adjust it.

@manav4u

manav4u commented Nov 25, 2026

Copy link
Copy Markdown

One boundary regression in the new comparison key: a valid local result can overflow when instant() subtracts its UTC offset.

pendulum.datetime(1, 1, 1, 12, tz=1).start_of("day")
pendulum.datetime(9999, 12, 31, 12, tz=-1).end_of("day")

Both return valid local datetimes on master, but raise OverflowError: date value out of range at this head (1d2386c). The same happens for month/year boundaries. I checked six focused cases on Linux/CPython 3.14 using the pure-Python implementation: six pass on master and six fail here. The existing start/end test module passes here.

Could the ordering key use an ordinal-based timedelta minus the offset, rather than constructing a UTC datetime? That would keep the offset-aware ordering without requiring the UTC instant to fit years 1..9999.

AI assistance used for this review and reproduction.

@bardrop

bardrop commented Nov 25, 2026

Copy link
Copy Markdown
Author

Thanks, you're right. The key is now a timedelta (the local date as days and time, minus the offset), so it can't leave the datetime range. start_of / end_of for day, month and year at year 1 with tz=1 and at 9999 with tz=-1 are in a new test, which fails on the previous head and passes now. Full suite: 1874 passed (the one failure, test_instance_with_aware_datetime_any_tzinfo, fails on master too, #1072).

@manav4u

manav4u commented Nov 26, 2026

Copy link
Copy Markdown

Thanks, confirmed on 360599d. The six boundary cases I reported now pass, along with the revised start/end test module (59 passed total). The 24 fixed-offset/named-zone probes also complete without errors. This was Linux/CPython 3.14 with the pure-Python implementation, not a full-suite or hosted-CI check. AI assistance used for the verification.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

start_of()/end_of() return the previous day or are an hour off when DST changes at midnight

2 participants