Skip to content

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

Open

Description

@bardrop
  • I am on the latest Pendulum version.

  • I have searched the issues of this repo and believe that this is not a duplicate.

  • OS version and name: macOS 27 (also reproduces with the pure-Python build)

  • Pendulum version: 3.3.0 (master e06dace)

Issue

start_of() and end_of() (day, week, month, year, decade, century) keep the fold of the input. When a DST change happens at midnight, the result can be on the wrong day or one hour off.

import pendulum

# Cairo skips 00:00-01:00 on 2026-04-24
now = pendulum.datetime(2026, 4, 24, 12, tz="UTC").in_timezone("Africa/Cairo")  # fold=0, like pendulum.now()
now.start_of("day")
# 2026-04-23 23:00:00+02:00   <- the day before
# expected 2026-04-24 01:00:00+03:00

# so pendulum.today("Africa/Cairo") returns yesterday on that date.

# Cairo repeats 23:00-24:00 on 2026-10-29
now = pendulum.datetime(2026, 10, 29, 12, tz="UTC").in_timezone("Africa/Cairo")
now.end_of("day").in_tz("UTC")
# 2026-10-29 20:59:59.999999+00:00   <- one hour early
# expected 2026-10-29 21:59:59.999999+00:00 (23:59:59.999999+02:00)

# Havana repeated 00:00-01:00 on 2012-11-04; default fold=1 picks the second midnight
pendulum.datetime(2012, 11, 4, 12, tz="America/Havana").start_of("day")
# 2012-11-04 00:00:00-05:00, expected 2012-11-04 00:00:00-04:00

Same for Beirut, Gaza, Tehran (old rules), Santiago, Asuncion, Havana, Azores and others.

Cause: since #713, DateTime.set() passes fold=self.fold to create(). _start_of_day / _end_of_day / _start_of_month / ... go through set()/at(), so the input's fold decides how 00:00 or 23:59:59.999999 is resolved. With fold=0, a skipped midnight is moved back into the previous day; with fold=1 a repeated midnight picks the later one. The start of a day should be the earliest instant on that date, and the end the latest one.

I checked every zone in tzdata, 1995-2035, a few hours around each transition, with both folds: 1,817 of 148,232 start_of("day")/end_of("day") results are on the wrong date or not the first/last instant of the date.

I have a fix with tests and can open a PR.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions