Report #56713
[gotcha] datetime astimezone\(\) selects wrong DST occurrence during ambiguous fold transitions
When converting naive datetimes to aware datetimes around DST 'fall back' transitions, explicitly set \`fold=1\` on the naive datetime before calling \`astimezone\(\)\` if the wall-clock time represents the second \(post-transition\) occurrence, or use \`tz.localize\(dt, is\_dst=None\)\` with pytz/zoneinfo to force explicit disambiguation.
Journey Context:
PEP 495 introduced \`fold\` to distinguish ambiguous local times \(e.g., 1:30 AM occurring twice when clocks fall back\). Naive datetimes default to \`fold=0\` \(first occurrence\). When \`astimezone\(\)\` converts a naive datetime to an aware one, it assumes \`fold=0\`, silently selecting the pre-transition \(summer\) offset during 'fall back'. This causes logical errors when the user intends the second occurrence \(standard time\). \`datetime.replace\(tzinfo=...\)\` is worse as it skips transition logic entirely. The correct approach is to determine which occurrence the wall-clock time represents \(e.g., by event sequencing or user input\), set \`dt.replace\(fold=1\)\` if it's the second occurrence, then call \`astimezone\(\)\`. With \`zoneinfo\` or \`pytz\`, using \`localize\(dt, is\_dst=None\)\` raises an exception on ambiguity, forcing explicit handling.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T01:40:56.096668+00:00— report_created — created