perf(datetimeutil): parse ISO 8601 instants without the format search

to_datetime() tried ten pendulum formats before an ISO 8601 date-time
with "Z" or an explicit offset reached pendulum.parse(). None of the
formats can match such a string (each is combined with a timezone
name), so every call paid ~190 µs for nothing. Database timestamps
("20241027T123456Z") take this path on every record lookup
(db_get_record, _db_timestamp_after, key_from_series), and so do API
and provider date-times.

These strings name one instant and are now parsed directly (~7 µs),
built with pendulum.datetime() like pendulum.parse(), so the result
keeps the same fold. Any other form, offsets of 24 hours or more and
out-of-range fields still go through the generic parser. Compared with
the previous to_datetime() over 162 432 calls (5076 strings, 8 target
zones): no difference in value, timezone, fold or raised exception.

DatabaseTimestamp.from_datetime() formats with an f-string instead of
the format-token parser of pendulum (6 µs -> 2 µs), with the same output
for every 4-digit year. Its docstring example shows the stored form
"20241027T123456Z".

The five DB/EnergyCharts test modules: 298 s -> 112 s.

Additional changes
Test the config returned to a re-entrant get_config(); the full suite
reached that line only as a side effect of slow tests

Refs #1553

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>
Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
SchaafAlexander
2026-11-20 07:15:33 +01:00
committed by Bobby Noelte
co-authored by Claude Opus 5.5
parent cc875f798c
commit 00a39f75bf
5 changed files with 159 additions and 2 deletions
+33
View File
@@ -361,6 +361,39 @@ async def seq_sparse():
return s, now
# ---------------------------------------------------------------------------
# DatabaseTimestamp
# ---------------------------------------------------------------------------
class TestDatabaseTimestamp:
@pytest.mark.parametrize(
("local", "timezone", "expected"),
[
# 02:30:59 occurs twice on the day DST ends.
("2024-10-27T02:30:59+02:00", "Europe/Berlin", "20241027T003059Z"),
("2024-10-27T02:30:59+01:00", "Europe/Berlin", "20241027T013059Z"),
("2026-01-01T00:00:00+14:00", "Pacific/Kiritimati", "20251231T100000Z"),
("2026-01-01T23:59:59-12:00", "Etc/GMT+12", "20260102T115959Z"),
],
)
def test_round_trip(self, local: str, timezone: str, expected: str) -> None:
dt = to_datetime(local, in_timezone=timezone)
db_ts = DatabaseTimestamp.from_datetime(dt.add(microseconds=999))
assert db_ts == expected
assert db_ts == dt.in_timezone("UTC").format("YYYYMMDDTHHmmss[Z]")
parsed = db_ts.to_datetime()
# Compare in UTC: an inter-zone comparison is never equal for the
# second (fold=1) occurrence of an ambiguous local time.
assert parsed == dt.in_timezone("UTC")
assert parsed.timezone_name == "UTC"
def test_naive_datetime_rejected(self) -> None:
with pytest.raises(ValueError):
DatabaseTimestamp.from_datetime(DateTime(2024, 1, 1))
# ---------------------------------------------------------------------------
# Core mixin tests
# ---------------------------------------------------------------------------