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

datetime equivalent

to datetime

from datetime

Instant

datetime with UTC

to_stdlib()

Instant(py_datetime)

ZonedDateTime

datetime with ZoneInfo

to_stdlib() [1]

ZonedDateTime(py_datetime, disambiguation=, offset_mismatch=)

OffsetDateTime

datetime with timezone

to_stdlib()

OffsetDateTime(py_datetime)

PlainDateTime

datetime (naive)

to_stdlib()

PlainDateTime(py_datetime)

Date

date

to_stdlib()

Date(py_date)

Time

time

to_stdlib()

Time(py_time)

YearMonth

N/A

N/A

N/A

MonthDay

N/A

N/A

N/A

IsoWeekDate

the isocalendar() result, a named tuple, not a type

via date()

via Date(py_date)

TimeDelta

timedelta

to_stdlib()

TimeDelta(py_timedelta)

ItemizedDelta

N/A

N/A

N/A

ItemizedDateDelta

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:

  • ItemizedDelta and ItemizedDateDelta cannot be converted to timedelta because they may contain calendar units, and because they are itemized: every component is kept as given, unlike timedelta.

  • YearMonth and MonthDay cannot be converted because there is no direct equivalent in the standard library. IsoWeekDate has a named tuple, not a type, in the standard library: go through .date().