From 81ee00eec7a5da941c06ebd1dc988ef2bbcef03e Mon Sep 17 00:00:00 2001 From: charanteja25 Date: Thu, 15 Oct 2026 17:33:32 +0530 Subject: [PATCH 1/2] fix: align naive datetimes independent of the local UTC offset align_datetime used datetime.timestamp() for naive datetimes. That call applies the local UTC offset of the machine, so alignment to an interval was wrong when the offset is not a multiple of the interval (for example UTC+05:30 and a one-hour interval). Naive datetimes are now aligned with timedelta arithmetic relative to a naive epoch. Aware datetimes are unchanged. Add a regression test that runs with the process timezone set to UTC+05:30. Signed-off-by: charanteja25 --- .../src/openstef_core/utils/datetime.py | 19 ++++-- .../tests/unit/utils/test_datetime.py | 66 +++++++++++++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/packages/openstef-core/src/openstef_core/utils/datetime.py b/packages/openstef-core/src/openstef_core/utils/datetime.py index 8258798d7..5d618bf5d 100644 --- a/packages/openstef-core/src/openstef_core/utils/datetime.py +++ b/packages/openstef-core/src/openstef_core/utils/datetime.py @@ -28,19 +28,26 @@ def align_datetime(timestamp: datetime, interval: timedelta, mode: Literal["ceil Raises: ValueError: If mode is not "ceil" or "floor". """ + if mode not in {"ceil", "floor"}: + msg = f"Unknown alignment mode: {mode}" + raise ValueError(msg) + + if timestamp.tzinfo is None: + # Naive datetimes carry no offset. Calling `.timestamp()` would apply the local UTC offset of the machine, + # which breaks alignment when that offset is not a multiple of the interval (e.g. UTC+05:30). + epoch = datetime(1970, 1, 1) # noqa: DTZ001 + elapsed = timestamp - epoch + steps = elapsed // interval if mode == "floor" else -(-elapsed // interval) + return epoch + steps * interval + timestamp_epoch_seconds = timestamp.timestamp() interval_seconds = interval.total_seconds() if mode == "floor": aligned_secs = math.floor(timestamp_epoch_seconds / interval_seconds) * interval_seconds - elif mode == "ceil": - aligned_secs = math.ceil(timestamp_epoch_seconds / interval_seconds) * interval_seconds else: - msg = f"Unknown alignment mode: {mode}" - raise ValueError(msg) + aligned_secs = math.ceil(timestamp_epoch_seconds / interval_seconds) * interval_seconds - if timestamp.tzinfo is None: - return datetime.fromtimestamp(aligned_secs, tz=None) # noqa: DTZ006 return datetime.fromtimestamp(aligned_secs, tz=timestamp.tzinfo) diff --git a/packages/openstef-core/tests/unit/utils/test_datetime.py b/packages/openstef-core/tests/unit/utils/test_datetime.py index 2d5c7cf7c..b6b267ccf 100644 --- a/packages/openstef-core/tests/unit/utils/test_datetime.py +++ b/packages/openstef-core/tests/unit/utils/test_datetime.py @@ -4,6 +4,10 @@ """Tests for datetime utilities.""" +import os +import sys +import time as time_module +from collections.abc import Iterator from datetime import UTC, datetime, time, timedelta, timezone from typing import Literal @@ -70,6 +74,68 @@ def test_align_datetime_naive( assert result.tzinfo is None +@pytest.fixture +def local_timezone_ist() -> Iterator[None]: + """Run a test with the process timezone set to UTC+05:30 (no DST).""" + if sys.platform == "win32": + pytest.skip("time.tzset is not available on Windows") + + previous = os.environ.get("TZ") + os.environ["TZ"] = "IST-5:30" + time_module.tzset() + yield + if previous is None: + os.environ.pop("TZ", None) + else: + os.environ["TZ"] = previous + time_module.tzset() + + +@pytest.mark.parametrize( + ("timestamp", "interval", "mode", "expected"), + [ + pytest.param( + datetime.fromisoformat("2020-01-01T10:15:00"), + timedelta(hours=1), + "ceil", + datetime.fromisoformat("2020-01-01T11:00:00"), + id="ceil_hour", + ), + pytest.param( + datetime.fromisoformat("2020-01-01T10:00:00"), + timedelta(hours=1), + "ceil", + datetime.fromisoformat("2020-01-01T10:00:00"), + id="ceil_already_aligned", + ), + pytest.param( + datetime.fromisoformat("2020-01-01T10:45:00"), + timedelta(hours=1), + "floor", + datetime.fromisoformat("2020-01-01T10:00:00"), + id="floor_hour", + ), + pytest.param( + datetime.fromisoformat("2020-01-01T10:07:30"), + timedelta(minutes=15), + "ceil", + datetime.fromisoformat("2020-01-01T10:15:00"), + id="ceil_15_minutes", + ), + ], +) +@pytest.mark.usefixtures("local_timezone_ist") +def test_align_datetime_naive_ignores_local_utc_offset( + timestamp: datetime, interval: timedelta, mode: Literal["ceil", "floor"], expected: datetime +): + # Arrange / Act + result = align_datetime(timestamp, interval, mode) + + # Assert + assert result == expected + assert result.tzinfo is None + + def test_align_datetime_with_timezone(): # Arrange tz = UTC From c0c42469ce5eb5df91cdf3d3990ea782c7967e14 Mon Sep 17 00:00:00 2001 From: charanteja25 Date: Fri, 13 Nov 2026 16:47:47 +0530 Subject: [PATCH 2/2] refactor: use pandas floor/ceil in align_datetime Signed-off-by: charanteja25 --- .../src/openstef_core/utils/datetime.py | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/openstef-core/src/openstef_core/utils/datetime.py b/packages/openstef-core/src/openstef_core/utils/datetime.py index 5d618bf5d..e44a71fc8 100644 --- a/packages/openstef-core/src/openstef_core/utils/datetime.py +++ b/packages/openstef-core/src/openstef_core/utils/datetime.py @@ -9,13 +9,17 @@ to regular time grids or specific analysis windows. """ -import math from datetime import datetime, time, timedelta from typing import Literal +import pandas as pd + def align_datetime(timestamp: datetime, interval: timedelta, mode: Literal["ceil", "floor"] = "ceil") -> datetime: - """Align timestamp using modulo approach. + """Align timestamp to an interval boundary. + + Aware datetimes align on UTC epoch boundaries. Naive datetimes align on boundaries + relative to a naive epoch, so the result does not depend on the local time zone of the machine. Args: timestamp: The datetime to align. @@ -32,23 +36,16 @@ def align_datetime(timestamp: datetime, interval: timedelta, mode: Literal["ceil msg = f"Unknown alignment mode: {mode}" raise ValueError(msg) - if timestamp.tzinfo is None: - # Naive datetimes carry no offset. Calling `.timestamp()` would apply the local UTC offset of the machine, - # which breaks alignment when that offset is not a multiple of the interval (e.g. UTC+05:30). - epoch = datetime(1970, 1, 1) # noqa: DTZ001 - elapsed = timestamp - epoch - steps = elapsed // interval if mode == "floor" else -(-elapsed // interval) - return epoch + steps * interval + aligned = pd.Timestamp(timestamp) + if timestamp.tzinfo is not None: + aligned = aligned.tz_convert("UTC") - timestamp_epoch_seconds = timestamp.timestamp() - interval_seconds = interval.total_seconds() + aligned = aligned.floor(interval) if mode == "floor" else aligned.ceil(interval) - if mode == "floor": - aligned_secs = math.floor(timestamp_epoch_seconds / interval_seconds) * interval_seconds - else: - aligned_secs = math.ceil(timestamp_epoch_seconds / interval_seconds) * interval_seconds + if timestamp.tzinfo is not None: + aligned = aligned.tz_convert(timestamp.tzinfo) - return datetime.fromtimestamp(aligned_secs, tz=timestamp.tzinfo) + return aligned.to_pydatetime() def align_datetime_to_time(timestamp: datetime, align_time: time, mode: Literal["ceil", "floor"] = "ceil") -> datetime: