Standard library conversions¶
Most classes have an equivalent in the Python standard library.
Use the to_stdlib() method to convert to the standard library equivalent,
or pass the standard library object directly to the constructor:
>>> from datetime import *
>>> from whenever import *
>>> py_dt = datetime(2025, 4, 19, 15, 30, tzinfo=timezone.utc)
>>> instant = Instant(py_dt)
>>> instant.to_stdlib()
datetime.datetime(2025, 4, 19, 15, 30, tzinfo=datetime.timezone.utc)
whenever class |
|
to |
from |
|---|---|---|---|
|
|||
|
|||
|
|||
|
|
||
|
|||
|
|||
N/A |
N/A |
N/A |
|
N/A |
N/A |
N/A |
|
the |
via |
via |
|
|
|||
N/A |
N/A |
N/A |
|
N/A |
N/A |
N/A |
Note
There are some exceptions where the conversion is not exact; see the individual method documentation for details.
Converting to the standard library is not always lossless. Nanoseconds are floored to microseconds.
The declared standard library type’s own fields are the contract. A field
the target cannot hold raises ValueError: a time with a tzinfo,
an aware datetime for PlainDateTime, and a naive one for
Instant, OffsetDateTime, and ZonedDateTime.
Warning
Subclasses of the standard library types are accepted and read through the
standard library attributes, so data those attributes cannot represent is
lost. pandas.Timestamp and pandas.Timedelta carry nanoseconds;
pendulum.Duration carries months and years (and folds them into 30-day
days). Passing one of these emits a WheneverWarning.
Convert explicitly instead:
Instant.from_timestamp(ts.value, unit="nanosecond") for a
pandas.Timestamp, TimeDelta(nanoseconds=td.value) for a
pandas.Timedelta, and an ItemizedDelta built from the
pendulum.Duration’s components. A datetime given to Date is the
same case: it is a date subclass whose time is dropped, so it warns; call
.date() first. A subclass that adds no data, such as freezegun’s
FakeDatetime, passes silently.
Converting a datetime with a ZoneInfo
ZonedDateTime reads such a datetime as local fields, the
offset its ZoneInfo computes for them, and a time zone ID.
An ISO string with an offset and a time zone ID carries the same three things,
so both go through the same
resolution flow. The constructor accepts
offset_mismatch= and disambiguation=, and raises
InvalidOffsetError by default when the standard library’s
rules and whenever’s own rules disagree. A local time in a gap is the
exception: under PEP 495 the datetime
still names one instant, which its fold selects, and whenever takes that
instant without applying disambiguation=.
ZoneInfo subclasses are accepted. timezone.utc, pytz,
and dateutil tzinfos are rejected with ValueError, because they carry
no time zone ID: use OffsetDateTime() or Instant() for those.
fold is read through the offset on aware datetimes and set by
to_stdlib(), so both occurrences of a repeated local time round-trip. On
naive datetimes and on time, fold is ignored.
to_stdlib() hands the time zone ID to the standard library, which resolves
it on its own search path. A system time zone without a time zone ID gives a
fixed-offset timezone instead.
There are no Python equivalents for the following classes:
ItemizedDeltaandItemizedDateDeltacannot be converted totimedeltabecause they may contain calendar units, and because they are itemized: every component is kept as given, unliketimedelta.YearMonthandMonthDaycannot be converted because there is no direct equivalent in the standard library.IsoWeekDatehas a named tuple, not a type, in the standard library: go through.date().