Working with deltas

This page gives a conceptual overview of the delta types in whenever. For the full API reference, see Delta types.

Three types for three use cases

whenever provides three delta types because deltas have fundamentally different arithmetic rules depending on the units involved:

Type

Units

When to use

TimeDelta

hours, minutes, seconds, …

Measuring exact elapsed time

ItemizedDateDelta

years, months, weeks, days

Calendar arithmetic (e.g. “3 months from now”)

ItemizedDelta

all of the above

Display, ISO 8601 round-tripping, mixed deltas

Keeping these cases separate prevents operations that need context—such as comparing 1 month with 30 days—from looking like ordinary exact-duration arithmetic. It also avoids normalizing away calendar components that a display or interchange format needs to preserve.

Most of the time you won’t create delta objects directly—you’ll use add(), subtract(), since(), and until() on datetime and date objects. But deltas become useful when you need to reuse a delta, pass it around, or inspect its components.

Normalized vs. itemized

TimeDelta normalizes its components: 90 minutes automatically becomes 1 hour 30 minutes. This makes comparison and arithmetic straightforward.

ItemizedDateDelta and ItemizedDelta keep their components itemized: 1 month stays 1 month, not 30 days. This is essential because calendar units have variable lengths—a month can be 28, 29, 30, or 31 days depending on when you start.

>>> TimeDelta(hours=1, minutes=90)
TimeDelta("PT2h30m")          # normalized: 2 hours 30 minutes
>>> ItemizedDelta(hours=1, minutes=90)
ItemizedDelta("PT1h90m")      # itemized: components kept as-is

Subsecond values use one nanoseconds component, bounded to 999,999,999; there are no milliseconds or microseconds components. Use total() for a scalar in any unit:

>>> d = ItemizedDelta(seconds=1, nanoseconds=234_567_890)
>>> reference = PlainDateTime(2024, 1, 1)
>>> d.total("milliseconds", relative_to=reference)
1234.56789
>>> d.total("microseconds", relative_to=reference)
1234567.89
>>> d.total("nanoseconds", relative_to=reference)
1234567890

Nanosecond totals are integers to preserve precision; other totals are floating-point values. See Seconds and nanoseconds for the full rules.

Calendar units need context

Calendar units are not fixed durations. 1 month may be 28, 29, 30, or 31 days, and applying it can clamp at month end. As a result, calendar units need a reference date for operations that convert them to other units. An itemized delta’s total() and in_units() always take relative_to=, even for exact units, as the example above shows: whether a reference is needed would otherwise depend on the delta’s value. TimeDelta has no calendar units, and needs none.

>>> d = ItemizedDateDelta(months=1)
>>> d.total("days", relative_to=Date(2024, 1, 15))   # January → February
31.0
>>> d.total("days", relative_to=Date(2024, 2, 15))   # February → March
29.0   # 2024 is a leap year

The same applies to in_units() when calendar units are involved.

Composing deltas

+, -, add(), and subtract() compose itemized deltas component-wise: like components add up, and the result stays itemized.

>>> ItemizedDateDelta(weeks=1) + ItemizedDateDelta(days=3)
ItemizedDateDelta("P1w3d")

That isn’t the same as applying the deltas in turn. With years or months, the two can land on different days, because a month clamps at the end of the month:

>>> one_month = ItemizedDateDelta(months=1)
>>> start = Date("2023-01-31")

>>> start + one_month + one_month
Date("2023-03-28")
>>> start + one_month.add(one_month, month_composition_ok=True)
Date("2023-03-31")

Both answers are right, for different questions. The second monthly renewal after January 31 falls on March 31; two monthly steps from January 31 land on March 28. So composing years or months emits MonthCompositionWarning, and month_composition_ok=True says you meant the first. Days, weeks, and exact units never clamp, and compose without a warning.

To apply deltas in turn, add each to the date, and measure the result if you need it as a delta:

>>> (start + one_month + one_month).since(start, in_units=["months", "days"])
ItemizedDateDelta("P1m28d")

A composed delta is never balanced: 1 hour plus 90 minutes stays 1 hour 90 minutes. The next section shows how to balance it.

Balancing into different units

“Balancing” means redistributing a delta’s value across a new set of units. Use in_units():

>>> td = TimeDelta(minutes=150)
>>> hours, minutes = td.in_units(["hours", "minutes"]).values()
(2, 30)

For itemized deltas with calendar units, balancing requires a reference date:

>>> d = ItemizedDateDelta(days=400)
>>> years, months, days = d.in_units(["years", "months", "days"], relative_to=Date(2024, 1, 1)).values()
(1, 1, 3)

Sign

All deltas carry a single sign that applies to every component. There are no mixed-sign deltas:

>>> -ItemizedDateDelta(years=1, months=6)
ItemizedDateDelta("-P1y6m")

See Sign for more details.

Next steps

  • Arithmetic — adding and subtracting time from datetimes

  • Delta types — full API reference for all three delta types