@@ -13,37 +13,48 @@ For example, you might want to reuse a particular duration,
1313or perform arithmetic on it.
1414For this, **whenever ** provides an API
1515designed to help you avoid common pitfalls.
16- The type annotations and descriptive errors should guide you
17- to the correct usage.
1816
19- Durations are created using the duration units provided.
20- Here is a quick demo:
17+ There are two main kind of deltas:
18+
19+ - :class: `~whenever.TimeDelta ` for precise time durations
20+ (hours, minutes, seconds, microseconds). This type supports mathematical
21+ operations like addition, subtraction, multiplication, and division.
22+ Arithmetic on time units is straightforward.
23+ It behaves similarly to the :class: `~datetime.timedelta `
24+ of the standard library.
25+ The drawback is that `TimeDelta ` doesn't support calender units
26+ like months or years, as their duration varies depending on context.
27+ That's where the second type comes in:
28+ - :class: `~whenever.ItemizedDelta ` for calendar-based durations.
29+ They don't have a precise duration, as this depends on the context.
30+ For example, the number of days in a month varies, and a day may be
31+ longer or shorter than 24 hours due to Daylight Saving Time.
32+ This makes arithmetic on calendar units less intuitive.
33+ In general, arithmetic on these delta's on their own is not supported,
34+ a local date/time is needed to provide context.
2135
22- >>> from whenever import years, months, days, hours, minutes
23- >>> # Precise units create a TimeDelta, supporting broad arithmetic
24- >>> movie_runtime = hours(2 ) + minutes(9 )
25- TimeDelta(02:09:00)
36+ .. note ::
37+
38+ There also is a third type, :class: `~whenever.ItemizedDateDelta `.
39+ It behaves similarly to `ItemizedDelta `, but contains *only * calendar units.
40+
41+ >>> movie_runtime = TimeDelta(hours = 2 , minutes = 9 )
42+ TimeDelta("PT2h9m")
2643>>> movie_runtime.in_minutes()
2744129.0
2845>>> movie_runtime / 1.2 # what if we watch it at 1.2x speed?
29- TimeDelta(01:47:30)
30- ...
31- >>> # Calendar units create a DateDelta, with more limited arithmetic
32- >>> project_estimate = months(1 ) + days(10 )
33- DateDelta("P1M10D")
34- >>> Date(2023 , 1 , 29 ) + project_estimate
35- Date("2023-03-10")
36- >>> project_estimate * 2 # make it pessimistic
37- DateDelta("P2M20D")
38- ...
39- >>> # Mixing date and time units creates a generic DateTimeDelta
40- >>> project_estimate + movie_runtime
41- DateTimeDelta("P1M10DT2H9M")
42- ...
46+ TimeDelta("Pt1h47m30s")
47+ >>> Instant.now() + movie_runtime # when does it end?
48+
49+
50+ >>> # If needed, composite, unnormalized durations can be created:
51+ >>> room_occupied = ItemizedDelta(months = 1 , days = 10 , minutes = 90 )
52+ ItemizedDelta("P1m10dT90m")
53+
4354>>> # API ensures common mistakes are caught early:
44- >>> project_estimate * 1.3 # Impossible arithmetic on calendar units
45- >>> project_estimate .in_hours() # Resolving calendar units without context
46- >>> Date(2023 , 1 , 29 ) + movie_runtime # Adding time to a date
55+ >>> room_occupied * 1.3 # Impossible arithmetic on calendar units
56+ >>> room_occupied .in_hours() # Resolving calendar units without context
57+ >>> Date(2023 , 1 , 29 ) + room_occupied # Adding time to a date
4758
4859Types of deltas
4960---------------
@@ -53,18 +64,10 @@ There are three duration types in **whenever**:
5364- :class: `~whenever.TimeDelta `, created by precise units
5465 :func: `~whenever.hours `, :func: `~whenever.minutes `, :func: `~whenever.seconds `,
5566 and :func: `~whenever.microseconds `.
56- Their duration is always the same and independent of the calendar.
57- Arithmetic on time units is straightforward.
58- It behaves similarly to the :class: `~datetime.timedelta `
59- of the standard library.
6067
6168- :class: `~whenever.DateDelta `, created by the calendar units
6269 :func: `~whenever.years `, :func: `~whenever.months `, :func: `~whenever.weeks `,
6370 and :func: `~whenever.days `.
64- They don't have a precise duration, as this depends on the context.
65- For example, the number of days in a month varies, and a day may be
66- longer or shorter than 24 hours due to Daylight Saving Time.
67- This makes arithmetic on calendar units less intuitive.
6871
6972- :class: `~whenever.DateTimeDelta `, created when you mix
7073 time and calendar units.
0 commit comments