Skip to content

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

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

bardrop wants to merge 1 commit 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

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

1 participant