Core Concepts
When-Exactly is basically a wrapper around Python's already awesome datetime
module, and allows developers to work with dates in a more abstract way.
Moment
The Moment
represents, a moment in time. This is analogous to Python's
datetime.datetime
class.
Note
The resolution of a moment is limited to a second. If you need more resolution, then when-exactly is probably not the library you need.
>>> moment = we.Moment(
... year=2025,
... month=3,
... day=14,
... hour=15,
... minute=0,
... second=0,
... )
>>> str(moment)
'2025-03-14T15:00:00'
Delta
The Delta
is analogous to Python's
datetime.timedelta
,
with extra functionality for deltas of years and months.
>>> delta = we.Delta(
... years=1,
... months=1,
... weeks=2,
... )
>>> moment + delta
Moment(year=2026, month=4, day=28, hour=15, minute=0, second=0)
Interval
An Interval
represents a time span.
An interval has an inclusive start and an exclusive stop.
>>> interval = we.Interval(
... start=we.Moment(2025, 2, 14, 12, 0, 0,),
... stop=we.Moment(2025, 2, 14, 12, 30, 0),
... )
>>> str(interval)
'2025-02-14T12:00:00/2025-02-14T12:30:00'
Custom Interval
A CustomInterval
represents a time span with a specific resolution.
This is the building block of all the custom intervals like Year, Month, etc.
Collection
The Collection
represents a collection of Interval
objects.
It provides all of the standard functionality you would expect a container to have
>>> collection = we.Collection([
... we.Day(2023, 1, 5),
... we.Day(2023, 1, 7),
... we.Week(2023, 10),
... ])
>>> collection[0]
Day(2023, 1, 5)
>>> collection[0:2]
Collection([Day(2023, 1, 5), Day(2023, 1, 7)])
>>> we.Week(2023, 10) in collection
True
>>> for interval in collection:
... print(interval)
2023-01-05
2023-01-07
2023-W10