PlainDateTime

class whenever.PlainDateTime(iso_string: str, /)[source]
class whenever.PlainDateTime(py_datetime: datetime, /)
class whenever.PlainDateTime(year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, *, nanosecond: int = 0)

A date and time-of-day without any time zone information.

Represents a local time as people observe it, without saying where. It can’t be mixed with exact-time types (e.g. Instant, ZonedDateTime) without explicitly assuming a time zone or offset.

>>> PlainDateTime(2024, 3, 10, 15, 30)
PlainDateTime("2024-03-10 15:30:00")

Can also be constructed from an ISO 8601 string or a standard library datetime:

>>> PlainDateTime("2024-03-10T15:30:00")
PlainDateTime("2024-03-10 15:30:00")

Convert to an exact time type by supplying a time zone or offset:

>>> dt = PlainDateTime(2024, 3, 10, 15, 30)
>>> dt.assume_tz("Europe/Amsterdam")
ZonedDateTime("2024-03-10 15:30:00+01:00[Europe/Amsterdam]")
>>> dt.assume_fixed_offset(hours(5))
OffsetDateTime("2024-03-10 15:30:00+05:00")

When to use this type:

  • You need to express a date and time as it would appear on a local time, independent of time zone.

  • You receive a datetime without time zone information and need to represent this lack of information in the type system.

  • You’re working in a context where time zones and DST transitions truly don’t apply (e.g. a simulation).

classmethod parse(s: str, /, *, pattern: str = ...) → PlainDateTime[source]

Parse a plain datetime from a custom pattern string.

See Pattern format for details.

>>> PlainDateTime.parse("2024-03-15 14:30", pattern="YYYY-MM-DD HH:mm")
PlainDateTime("2024-03-15 14:30:00")
classmethod parse_iso(s: str, /) → PlainDateTime[source]

Parse an ISO 8601 string without an offset, such as 2020-08-15T23:12:00. An offset or a bracketed time zone ID is rejected. See ISO 8601 for the accepted variants.

Inverse of format_iso().

>>> PlainDateTime.parse_iso("2020-08-15T23:12:00")
PlainDateTime("2020-08-15 23:12:00")
__add__(delta: TimeDelta, /) → PlainDateTime[source]

Add a delta to this datetime.

Warning

Adding exact time units (a TimeDelta) to a PlainDateTime does not account for time zone transitions that may occur in the interval. Use .assume_tz('<tz>') + delta if you know the time zone. Use .add(..., naive_arithmetic_ok=True) or Python’s standard warning filters to suppress.

__eq__(other: object, /) → bool[source]

Compare objects for equality. Only ever equal to other PlainDateTime instances with the same values.

Warning

To comply with the Python data model, this method can’t raise a TypeError when comparing with other types. Although it seems to be the sensible response, it would result in surprising behavior when using values as dictionary keys.

Use mypy’s --strict-equality flag to detect and prevent this.

>>> PlainDateTime(2020, 8, 15, 23) == PlainDateTime(2020, 8, 15, 23)
True
>>> PlainDateTime(2020, 8, 15, 23, 1) == PlainDateTime(2020, 8, 15, 23)
False
>>> PlainDateTime(2020, 8, 15) == Instant.from_utc(2020, 8, 15)
False  # Use mypy's --strict-equality flag to detect this.
__format__(spec: str, /) → str[source]

Default object formatter.

Return str(self) if format_spec is empty. Raise TypeError otherwise.

__ge__(other: PlainDateTime, /) → bool[source]

Return self>=value.

__gt__(other: PlainDateTime, /) → bool[source]

Return self>value.

__le__(other: PlainDateTime, /) → bool[source]

Return self<=value.

__lt__(other: PlainDateTime, /) → bool[source]

Return self<value.

__str__() → str

Return str(self).

__sub__(other: PlainDateTime, /) → TimeDelta[source]
__sub__(other: TimeDelta, /) → PlainDateTime

Subtract a delta or calculate the duration to another plain datetime.

Warning

Subtracting a TimeDelta or measuring the difference between two PlainDateTime values does not account for time zone transitions that may occur in the interval. Use assume_tz() to convert to a ZonedDateTime first for accurate results. When that is intentional, use subtract(..., naive_arithmetic_ok=True) or difference(..., naive_arithmetic_ok=True); the operator takes no keyword.

add(
d: TimeDelta | ItemizedDelta | ItemizedDateDelta,
/,
*,
naive_arithmetic_ok: bool = ...,
) → PlainDateTime[source]
add(
*,
years: int = ...,
months: int = ...,
weeks: int = ...,
days: int = ...,
hours: float = ...,
minutes: float = ...,
seconds: float = ...,
milliseconds: float = ...,
microseconds: float = ...,
nanoseconds: int = ...,
naive_arithmetic_ok: bool = ...,
) → PlainDateTime

Add a time amount to this datetime.

Years and months are applied first (clamped), then weeks and days; then the exact units. subtract() is add() of the negated components.

Warning

Adding exact time units (e.g. hours, seconds) to a PlainDateTime does not account for time zone transitions that may occur in the interval. Use .assume_tz('<tz>') + delta if you know the time zone. Pass naive_arithmetic_ok=True to suppress; Python’s standard warning filters also apply.

assume_fixed_offset(offset: int | TimeDelta, /) → OffsetDateTime[source]

Assume the datetime has the given offset, creating an OffsetDateTime.

>>> PlainDateTime(2020, 8, 15, 23, 12).assume_fixed_offset(hours(2))
OffsetDateTime("2020-08-15 23:12:00+02:00")
assume_system_tz(*, disambiguation: DisambiguationStr = ...) → ZonedDateTime[source]

Assume the datetime is in the system time zone, creating a ZonedDateTime.

Deprecated since version 0.11: Use assume_tz(SYSTEM_TZ) instead.

Note

The local time may be repeated or skipped in the system time zone (e.g. during a DST transition). You can explicitly specify how to handle such a situation using disambiguation. See the documentation for more information.

>>> d = PlainDateTime(2020, 8, 15, 23, 12)
>>> # assuming system time zone is America/New_York
>>> d.assume_tz(SYSTEM_TZ, disambiguation="raise")
ZonedDateTime("2020-08-15 23:12:00-04:00[America/New_York]")
assume_tz(tz: str | SYSTEM_TZ, /, *, disambiguation: DisambiguationStr = ...) → ZonedDateTime[source]

Assume the datetime is in the given time zone, creating a ZonedDateTime. Pass SYSTEM_TZ for the system time zone.

Note

The local time may be repeated or skipped in the given time zone (e.g. during a DST transition). You can explicitly specify how to handle such a situation using the disambiguation argument. See the documentation for more information.

>>> d = PlainDateTime(2020, 8, 15, 23, 12)
>>> d.assume_tz("Europe/Amsterdam", disambiguation="raise")
ZonedDateTime("2020-08-15 23:12:00+02:00[Europe/Amsterdam]")
Raises:

TimeZoneNotFoundError – If the time zone ID is not found in the time zone database.

assume_utc() → Instant[source]

Assume the datetime is in UTC, creating an Instant.

>>> PlainDateTime(2020, 8, 15, 23, 12).assume_utc()
Instant("2020-08-15 23:12:00Z")
date() → Date

The date part of the datetime

>>> d = PlainDateTime("2020-01-02 03:04:05")
>>> date = d.date()
>>> date
Date("2020-01-02")

To perform the inverse, use Date.at() and a method like assume_utc() or assume_tz():

>>> date.at(d.time()).assume_tz("Europe/London")
ZonedDateTime("2020-01-02 03:04:05+00:00[Europe/London]")
day_of_week() → Weekday

The day of the week

>>> PlainDateTime(2021, 1, 2, 12).day_of_week()
Weekday.SATURDAY
day_of_year() → int

Ordinal day in the year (1–366)

>>> PlainDateTime(2021, 1, 2).day_of_year()
2
days_in_month() → int

Number of days in the current month (28–31)

>>> PlainDateTime(2024, 2, 1).days_in_month()
29
days_in_year() → int

Number of days in the current year (365 or 366)

>>> PlainDateTime(2024, 1, 1).days_in_year()
366
difference(other: PlainDateTime, /, *, naive_arithmetic_ok: bool = ...) → TimeDelta[source]

Calculate the exact time difference between two plain datetimes.

This method returns the exact elapsed TimeDelta between two PlainDateTime values. Equivalent to the subtraction operator (-), but allows suppressing the NaiveArithmeticWarning via the naive_arithmetic_ok parameter.

Use since() or until() for more advanced options such as calendar units, unit decomposition, and rounding.

Warning

Calculating the difference between two PlainDateTime values does not account for time zone transitions. Use assume_tz() to convert to a ZonedDateTime first for accurate results.

end_of(
unit: Literal['year', 'month', 'week_mon', 'week_sun', 'day', 'hour', 'minute', 'second'],
/,
) → PlainDateTime[source]

The end of the given unit

>>> PlainDateTime(2024, 8, 15, 14, 30, 45).end_of("day")
PlainDateTime("2024-08-15 23:59:59.999999999")
>>> PlainDateTime(2024, 8, 15, 14, 30, 45).end_of("hour")
PlainDateTime("2024-08-15 14:59:59.999999999")

See also start_of()

format(pattern: str, /) → str[source]

Format as a custom pattern string.

Also available via f"{dt:YYYY-MM-DD HH:mm}" (Python’s __format__ protocol), where an empty spec falls back to __str__().

See Pattern format for details.

>>> PlainDateTime(2024, 3, 15, 14, 30).format("YYYY-MM-DD HH:mm")
'2024-03-15 14:30'
format_iso(
*,
unit: Literal['hour', 'minute', 'second', 'millisecond', 'microsecond', 'nanosecond', 'auto'] = 'auto',
basic: bool = False,
sep: Literal['T', ' '] = 'T',
) → str[source]

Format as an ISO 8601 string, such as 2020-08-15T23:12:00.

Inverse of parse_iso().

>>> PlainDateTime(2020, 8, 15, 23, 12).format_iso()
'2020-08-15T23:12:00'

unit, basic, and sep are as on ZonedDateTime.format_iso().

in_leap_year() → bool

Whether the year of this datetime is a leap year

>>> PlainDateTime(2024, 1, 1).in_leap_year()
True
replace(
*,
year: int = ...,
month: int = ...,
day: int = ...,
hour: int = ...,
minute: int = ...,
second: int = ...,
nanosecond: int = ...,
) → PlainDateTime[source]

Create a new instance with the given fields replaced

A result that is not a valid date or time raises ValueError.

>>> d = PlainDateTime(2021, 1, 31, 12, 30)
>>> d.replace(month=2, day=28)
PlainDateTime("2021-02-28 12:30:00")
replace_date(date: Date, /) → PlainDateTime[source]

Create a new instance with the date replaced

>>> d = PlainDateTime(2021, 1, 2, 12, 30)
>>> d.replace_date(Date(2024, 2, 29))
PlainDateTime("2024-02-29 12:30:00")
replace_time(time: Time, /) → PlainDateTime[source]

Create a new instance with the time replaced

>>> d = PlainDateTime(2021, 1, 2, 12, 30)
>>> d.replace_time(Time(8, 15, nanosecond=1))
PlainDateTime("2021-01-02 08:15:00.000000001")
round(
unit: Literal['day', 'hour', 'minute', 'second', 'millisecond', 'microsecond', 'nanosecond'] | TimeDelta = 'second',
/,
*,
increment: int = ...,
mode: RoundModeStr = 'half_even',
) → PlainDateTime[source]

Round the datetime to the specified unit and increment, or to a multiple of a TimeDelta. Different rounding modes are available.

>>> d = PlainDateTime(2020, 8, 15, 23, 24, 18)
>>> d.round("day")
PlainDateTime("2020-08-16 00:00:00")
>>> d.round("minute", increment=15, mode="floor")
PlainDateTime("2020-08-15 23:15:00")
>>> d.round(TimeDelta(minutes=15))
PlainDateTime("2020-08-15 23:30:00")

Increments are counted from midnight, so "half_even" breaks a tie toward the even multiple counted from there.

since(other: PlainDateTime, /, *, total: DeltaTotalUnitStr, naive_arithmetic_ok: bool = ...) → float[source]
since(
other: PlainDateTime,
/,
*,
in_units: Sequence[DeltaUnitStr],
round_mode: RoundModeStr = ...,
round_increment: int = ...,
naive_arithmetic_ok: bool = ...,
) → ItemizedDelta

Calculate the duration since another PlainDateTime, in terms of the specified units.

>>> d1 = PlainDateTime(2020, 8, 15, 23, 12)
>>> d2 = PlainDateTime(2020, 8, 14, 22)
>>> d1.since(d2, in_units=["hours", "minutes"],
...          round_increment=15,
...          round_mode="ceil")
ItemizedDelta("PT25h15m")

Warning

Exact units in the result (total= of one, or in_units containing any) emit NaiveArithmeticWarning: a difference in hours between two local times ignores the time zone transitions between them. Pass naive_arithmetic_ok=True when that is intentional.

start_of(
unit: Literal['year', 'month', 'week_mon', 'week_sun', 'day', 'hour', 'minute', 'second'],
/,
) → PlainDateTime[source]

The start of the given unit

>>> PlainDateTime(2024, 8, 15, 14, 30, 45).start_of("day")
PlainDateTime("2024-08-15 00:00:00")
>>> PlainDateTime(2024, 8, 15, 14, 30, 45).start_of("hour")
PlainDateTime("2024-08-15 14:00:00")
subtract(
d: TimeDelta | ItemizedDelta | ItemizedDateDelta,
/,
*,
naive_arithmetic_ok: bool = ...,
) → PlainDateTime[source]
subtract(
*,
years: int = ...,
months: int = ...,
weeks: int = ...,
days: int = ...,
hours: float = ...,
minutes: float = ...,
seconds: float = ...,
milliseconds: float = ...,
microseconds: float = ...,
nanoseconds: int = ...,
naive_arithmetic_ok: bool = ...,
) → PlainDateTime

Subtract a time amount from this datetime.

See add() for more information.

time() → Time

The time-of-day part of the datetime

>>> d = ZonedDateTime("2021-01-02T03:04:05+01:00[Europe/Paris]")
>>> time = d.time()
>>> time
Time("03:04:05")

To perform the inverse, use Time.on() and a method like assume_utc() or assume_tz():

>>> time.on(d.date()).assume_tz("Europe/Paris")
ZonedDateTime("2021-01-02 03:04:05+01:00[Europe/Paris]")
to_stdlib() → datetime

Convert to a standard library datetime

Note

Nanoseconds are floored to microseconds. If you need more control over rounding, use round() first.

until(other: PlainDateTime, /, *, total: DeltaTotalUnitStr, naive_arithmetic_ok: bool = ...) → float[source]
until(
other: PlainDateTime,
/,
*,
in_units: Sequence[DeltaUnitStr],
round_mode: RoundModeStr = ...,
round_increment: int = ...,
naive_arithmetic_ok: bool = ...,
) → ItemizedDelta

Inverse of the since() method. See since() for more information.

MAX: ClassVar[PlainDateTime] = PlainDateTime("9999-12-31 23:59:59.999999999")

The maximum possible value of this type.

MIN: ClassVar[PlainDateTime] = PlainDateTime("0001-01-01 00:00:00")

The minimum possible value of this type.

property day: int

The day component of the datetime

property hour: int

The hour component of the datetime

property minute: int

The minute component of the datetime

property month: int

The month component of the datetime

property nanosecond: int

The nanosecond component of the datetime

property second: int

The second component of the datetime

property year: int

The year component of the datetime