Check for non-existent datetime

Working on improving zoneinfo support in pandas, I want to check for non-existent datetimes. e.g. ``` from datetime import datetime, timezone import zoneinfo tz = zoneinfo.ZoneInfo("US/Pacific") dt = datetime(2014, 3, 9, 2, tzinfo=tz) # <- want to tell if this exists roundtrip = dt.astimezone(timezone.utc).astimezone(tz) if roundtrip != dt: raise ValueError("Nonexistent!") ``` Is checking for round-tripping a) correct and b) the suggested way of doing this?

Please see Strict Invalid Time Checking section in PEP 495: https://peps.python.org/pep-0495/#strict-invalid-time-checking It contains example code for detecting both non-existing and ambiguous times. On Mon, Nov 14, 2022 at 10:41 PM <jbrockmendel@gmail.com> wrote:
Working on improving zoneinfo support in pandas, I want to check for non-existent datetimes. e.g.
``` from datetime import datetime, timezone import zoneinfo
tz = zoneinfo.ZoneInfo("US/Pacific")
dt = datetime(2014, 3, 9, 2, tzinfo=tz) # <- want to tell if this exists
roundtrip = dt.astimezone(timezone.utc).astimezone(tz)
if roundtrip != dt: raise ValueError("Nonexistent!") ```
Is checking for round-tripping a) correct and b) the suggested way of doing this? _______________________________________________ Datetime-SIG mailing list -- datetime-sig@python.org To unsubscribe send an email to datetime-sig-leave@python.org https://mail.python.org/mailman3/lists/datetime-sig.python.org/ The PSF Code of Conduct applies to this mailing list: https://www.python.org/psf/codeofconduct/
participants (2)
-
Alexander Belopolsky
-
jbrockmendel@gmail.com