Comparison and equality

Every type supports ==. Every type but Weekday and the itemized deltas supports ordering (<, <=, >, >=): a calendar unit has no fixed length, so compare ItemizedDelta and ItemizedDateDelta after in_units() or total() with a relative_to. Values of different types are equal and order only within the exact family (Instant, OffsetDateTime, ZonedDateTime): outside the exact family, == across types is False and < raises TypeError. No whenever value equals a standard-library value, and an itemized delta, though a Mapping, never equals a dict. hash() agrees with == on every type: values that compare equal hash alike, across Instant, OffsetDateTime, and ZonedDateTime too, so equal values collapse in a set or share a dict key.

Exact time

For exact types (Instant, OffsetDateTime, ZonedDateTime), comparison and equality are based on whether they represent the same moment in time. This means that two objects with different values can be equal:

>>> # different ways of representing the same moment in time
>>> inst = Instant.from_utc(2023, 12, 28, 11, 30)
>>> as_5hr_offset = OffsetDateTime(2023, 12, 28, 16, 30, offset=hours(5))
>>> as_8hr_offset = OffsetDateTime(2023, 12, 28, 19, 30, offset=hours(8))
>>> in_nyc = ZonedDateTime(2023, 12, 28, 6, 30, tz="America/New_York")
>>> # all equal
>>> inst == as_5hr_offset == as_8hr_offset == in_nyc
True
>>> # comparison
>>> in_nyc > OffsetDateTime(2023, 12, 28, 11, 30, offset=hours(5))
True

mypy’s --strict-equality flags == between two different exact types; Mixing local and exact types has the workarounds.

To also compare what == leaves out—here the local datetime and the offset—use strict_eq(). See Strict equality below.

Local time

For PlainDateTime, equality is simply based on whether the values are the same, since there is no concept of time zones or UTC offset:

>>> d = PlainDateTime(2023, 12, 28, 11, 30)
>>> same = PlainDateTime(2023, 12, 28, 11, 30)
>>> different = PlainDateTime(2023, 12, 28, 11, 31)
>>> d == same
True
>>> d == different
False

See also

See the documentation of __eq__ (exact) and PlainDateTime.__eq__ for more details.

Strict equality

Some types deliberately leave part of their value out of ==. For those, strict equality—the strict_eq() method—compares that part as well. It refines ==: if a.strict_eq(b) holds, then a == b holds, but never the other way around. An argument of a different type raises a TypeError, because a cross-type call is nearly always a mistake.

The method exists on exactly the types whose == ignores something, and compares exactly that in addition:

  • Instant: the argument’s type.

  • OffsetDateTime: the type, the local datetime, and the offset.

  • ZonedDateTime: the type, the local datetime, the offset, and the time zone—meaning its time zone ID (or the absence of one, for the system time zone) and its definition. Two values with the same time zone ID can carry different rules after a clear_tzcache() or a reset_tzpath(), and they are then not strictly equal.

  • ItemizedDelta and ItemizedDateDelta: the type, and whether each component was given explicitly. An explicit zero is a component; == treats it as a missing one.

>>> d = OffsetDateTime(2023, 12, 28, 11, 30, offset=hours(5))
>>> same = OffsetDateTime(2023, 12, 28, 11, 30, offset=hours(5))
>>> same_moment = OffsetDateTime(2023, 12, 28, 12, 30, offset=hours(6))
>>> d == same_moment
True
>>> d.strict_eq(same_moment)
False
>>> d.strict_eq(same)
True

The types whose == already compares every field—PlainDateTime, Date, Time, TimeDelta, YearMonth, MonthDay, and IsoWeekDate—have no strict_eq().

Nanosecond precision and interoperability

Take care when comparing datetimes after interoperating with databases, the Python standard library, or other systems that may not support nanosecond precision. Since equality is based on full nanosecond precision, two datetimes may no longer be equal after a round-trip that loses precision.

This may not be apparent in development if your system’s clock only supports microsecond precision (such as macOS).

Use .round('microsecond') to explicitly round values to microsecond precision.

Mixing local and exact types

Local and exact types are never equal or comparable to each other. However, to comply with the Python data model, the equality operator won’t prevent you from using == to compare them. To prevent these mix-ups, use mypy’s --strict-equality flag.

>>> # These are never equal, but Python won't stop you from comparing them.
>>> # Mypy will catch this mix-up if you use enable --strict-equality flag.
>>> Instant.from_utc(2023, 12, 28) == PlainDateTime(2023, 12, 28)
False

Why not raise a TypeError?

It may seem like the equality operator should raise a TypeError in these cases, but this would result in surprising behavior when using values as dictionary keys.

Unfortunately, mypy’s --strict-equality is very strict, forcing you to match exact types exactly.


x = Instant.from_utc(2023, 12, 28, 10)

# mypy: ✅
x == Instant.from_utc(2023, 12, 28, 10)

# mypy: ❌ (too strict, this should be allowed)
x == OffsetDateTime(2023, 12, 28, 11, offset=hours(1))

To work around this, you can either convert explicitly:

x == OffsetDateTime(2023, 12, 28, 11, offset=hours(1)).to_instant()

Or annotate the other value with a union:

other: OffsetDateTime | Instant = OffsetDateTime(
    2023, 12, 28, 11, offset=hours(1)
)
x == other