Skip to content

Pure-Python ZoneInfo.fromutc() misses folds in zones with a single transition #159706

Open

Description

@pganssle

Bug report

import datetime
import zoneinfo
from zoneinfo import _zoneinfo

# Requires tzdata==2026.5; use its TZif files rather than system data.
zoneinfo.reset_tzpath([])
utc = datetime.datetime(1976, 12, 1, 1, tzinfo=datetime.UTC)
for implementation in (zoneinfo, _zoneinfo):
    tz = implementation.ZoneInfo.no_cache("Antarctica/Rothera")
    local = utc.astimezone(tz)
    print(implementation.__name__)
    print(local, "fold=", local.fold)
    print(local.astimezone(datetime.UTC))

Output:

zoneinfo
1976-11-30 22:00:00-03:00 fold= 1
1976-12-01 01:00:00+00:00

zoneinfo._zoneinfo
1976-11-30 22:00:00+00:00 fold= 0
1976-11-30 22:00:00+00:00

The pure-Python implementation should give the same result as the C implementation. Rothera's offset moved from UTC to UTC−03 at 1976-12-01 00:00 UTC, so this instant is in the second occurrence of 22:00 local time and should have fold=1. Instead, the result has fold=0 and converting it back to UTC loses three hours.

In the single-transition branch of fromutc(), tti_prev is set to self._ttinfos[-1], which is the offset after the transition. It matches self._tz_after, so the calculated shift is zero. I think this branch needs to use self._tti_before as the previous offset, as the C implementation already does.

Linked PRs

Activity

  1. lazerg commented on Oct 19, 2026

    @lazerg
    Contributor

    I can reproduce this and have the one-line fix with a test ready. Are you planning to fix it yourself, or would you like me to open a PR?

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 19, 2026
  3. pganssle commented on Oct 19, 2026

    @pganssle
    MemberAuthor

    @lazerg Feel free to send a fix.

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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions