Period.range#

Period.range(start, end)[source]#

Yield the start of every interval overlapping the half-open window from start to end.

The first value is the start of the interval containing start, which may fall before it; the last is the start of the last interval that begins before end. An interval is included when any part of it falls in the window, so the first and last may both extend beyond it. The window is empty if end is not after start.

Parameters:
  • start (datetime) – The start datetime of the range window.

  • end (datetime) – The end datetime of the range window.

Yields:

The first instant of each overlapping interval, in order.

Return type:

Iterator[datetime]

Examples

>>> from datetime import datetime
>>> pt6h = Period.of_hours(6)
>>> for start in pt6h.range(datetime(2024, 3, 1), datetime(2024, 3, 2)):
...     print(start)
2024-03-01 00:00:00
2024-03-01 06:00:00
2024-03-01 12:00:00
2024-03-01 18:00:00

A window that starts mid-interval still yields that whole interval …

>>> for start in pt6h.range(datetime(2024, 3, 1, 3), datetime(2024, 3, 1, 13)):
...     print(start)
2024-03-01 00:00:00
2024-03-01 06:00:00
2024-03-01 12:00:00

… and calendar periods need no special handling.

>>> for start in Period.of_months(1).range(datetime(2024, 1, 15), datetime(2024, 4, 1)):
...     print(start)
2024-01-01 00:00:00
2024-02-01 00:00:00
2024-03-01 00:00:00