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
+13
View File
@@ -1,6 +1,7 @@
import json
import tempfile
from pathlib import Path
from types import SimpleNamespace
from typing import Any
from unittest.mock import Mock, patch
@@ -1042,3 +1043,15 @@ class TestConfigSetup:
with pytest.raises(RuntimeError, match="before its settings are loaded"):
coreabc.get_config(init=True)
def test_access_after_settings_are_loaded_returns_config_in_setup(
self, uncreated_config, monkeypatch
):
"""During creation the configuration is returned once its settings are loaded."""
from akkudoktoreos.core import coreabc
config_in_setup = SimpleNamespace(_initialized=True)
monkeypatch.setattr(coreabc, "_config_eos_setup_running", True)
monkeypatch.setitem(ConfigEOS._instances, ConfigEOS, config_in_setup)
assert coreabc.get_config(init=True) is config_in_setup