Period.count#

Period.count(other)[source]#

Return how many intervals of this period fit into each interval of other, or None if there is no constant number.

A count exists when this period’s intervals fit inside other’s exactly: one of them starts where the interval of other starts, another ends where it ends, and no part-intervals fall in between. That whole number is returned, and it is the same for every interval across the timeline - e.g. twenty-four hours in a day, twelve months in a year.

Two situations have no such number, and both return None:

  • Aligned, but not constant. The boundaries line up, but the count differs between intervals. Days divide every month cleanly, yet months may have 28 to 31 days, so there is no single answer.

  • Not aligned. Intervals of this period straddle the boundaries of other, so no interval of other contains a whole number of them. This also covers this period simply being the larger of the two.

Use is_subperiod_of() to tell the two apart: it is True in the first case and False in the second.

Parameters:

other (Period) – The period whose intervals this period’s intervals are counted within

Return type:

int | None

Returns:

The number of intervals of this period per interval of other, the same for every interval on the timeline; or None if no such number exists

Examples

>>> pt1h, p1d, p1m, p1y = (
...     Period.of_hours(1),
...     Period.of_days(1),
...     Period.of_months(1),
...     Period.of_years(1),
... )

The ordinary case: a whole number, the same for every interval on the timeline.

>>> pt1h.count(p1d)
24
>>> p1m.count(p1y)
12

Aligned, but a month is 28-31 days, so no constant count exists.

>>> print(p1d.count(p1m))
None
>>> p1d.is_subperiod_of(p1m)
True

A larger period never fits inside a smaller one.

>>> print(p1d.count(pt1h))
None
>>> p1d.is_subperiod_of(pt1h)
False

An offset on one of them breaks the alignment …

>>> pt1h_t1m = Period.of_hours(1).with_minute_offset(1)
>>> print(pt1h_t1m.count(p1d))
None

… but not if both carry the same offset.

>>> pt1h_t1m.count(Period.of_days(1).with_minute_offset(1))
24

See also

is_subperiod_of(), for whether the intervals nest at all.