align_datetime_to_time#

openstef_core.utils.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:
  • timestamp (datetime) – The datetime to align.

  • align_time (time) – Target time of day to align to. If timezone-aware and timestamp has timezone info, converts align_time to timestamp’s timezone.

  • mode (Literal['ceil', 'floor']) – Alignment direction - “ceil” for next occurrence, “floor” for previous.

Returns:

Aligned datetime with the same timezone as the original timestamp.

Return type:

datetime

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)
Parameters:
  • timestamp (datetime)

  • align_time (time)

  • mode (Literal['ceil', 'floor'])

Return type:

datetime