Delta

when_exactly.Delta dataclass

A Delta represents a duration of time that can be added to or subtracted from a Moment.

Unlike Python's timedelta, Delta supports month and year arithmetic, which is essential for calendar-aware date operations. All attributes default to 0 and must be specified as keyword arguments.

Attributes:

Name Type Description
years int

Number of years in the delta.

months int

Number of months in the delta.

weeks int

Number of weeks in the delta.

days int

Number of days in the delta.

hours int

Number of hours in the delta.

minutes int

Number of minutes in the delta.

seconds int

Number of seconds in the delta.

Example
>>> import when_exactly as wnx
>>> # Create various deltas
>>> delta = wnx.Delta(days=5, hours=3)
>>> delta
Delta(years=0, months=0, weeks=0, days=5, hours=3, minutes=0, seconds=0)

>>> # Add to a moment
>>> moment = wnx.Moment(2025, 1, 15, 10, 0, 0)
>>> moment + wnx.Delta(months=1, days=10)
Moment(year=2025, month=2, day=25, hour=10, minute=0, second=0)

>>> # Handle month-end edge cases
>>> wnx.Moment(2025, 1, 31, 0, 0, 0) + wnx.Delta(months=1)
Moment(year=2025, month=2, day=28, hour=0, minute=0, second=0)