align_datetime_to_time#
- openstef_core.utils.datetime.align_datetime_to_time(timestamp: datetime, align_time: time, mode: Literal['ceil', 'floor'] = 'ceil') datetime[source]#
Align timestamp to the nearest occurrence of a specific time of day.
Aligns a timestamp to either the next (ceil) or previous (floor) occurrence of the specified time. Properly handles timezone conversions when both timestamp and align_time have timezone information.
- Parameters:
- Returns:
Aligned datetime with the same timezone as the original timestamp.
- Return type:
Example
>>> from datetime import datetime, time >>> dt = datetime.fromisoformat("2023-01-15T14:30:00") >>> target = time.fromisoformat("09:00:00") >>> align_datetime_to_time(dt, target, "ceil") datetime.datetime(2023, 1, 16, 9, 0) >>> align_datetime_to_time(dt, target, "floor") datetime.datetime(2023, 1, 15, 9, 0)