Period.interval#

Period.interval(datetime_obj)[source]#

Return the bounds of the interval containing the supplied datetime, as a (start, end) pair.

The pair is half-open - start <= d < end for every datetime d in the interval - so end is the first instant of the next interval and belongs to that one. start is what floor() returns.

Parameters:

datetime_obj (datetime) – The datetime whose interval is wanted. Any tzinfo it carries is ignored, as in ordinal().

Return type:

tuple[datetime, datetime]

Returns:

A (start, end) tuple of datetimes, both carrying this Period’s own tzinfo

Examples

>>> from datetime import datetime
>>> Period.of_hours(1).interval(datetime(2024, 3, 15, 9, 47))
(datetime.datetime(2024, 3, 15, 9, 0), datetime.datetime(2024, 3, 15, 10, 0))

Calendar intervals work the same way, despite their varying length.

>>> Period.of_months(1).interval(datetime(2024, 2, 10))
(datetime.datetime(2024, 2, 1, 0, 0), datetime.datetime(2024, 3, 1, 0, 0))

See also

range(), which yields interval starts this can turn into bounded intervals.