Repository navigation
fix(db): convert set_state_db timestamps to UTC with astimezone - #5698
springfall2008 wants to merge 2 commits into
Conversation
The old conversion subtracted utcoffset().seconds // 3600 hours, which drops the negative days component of a western-hemisphere offset (stored 24 h early) and truncates half/quarter-hour offsets to the hour. Use astimezone(utc) and refuse naive stamps explicitly. Fixes #5693. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
springfall2008
left a comment
There was a problem hiding this comment.
Automated comment from the triage bot.
Reviewed at head commit bd97f80 (fix(db): convert set_state_db timestamps to UTC with astimezone) — no issues found: 0 inline, 0 in the body. The conversion now handles negative and fractional-hour offsets exactly and the PR's own test cases are red under the old formula (verified by hand-arithmetic, e.g. -05:00 10:00 stored as prior-day 15:00, +05:30 stored 05:00 instead of 04:30), so they cannot pass with the change reverted.
What was checked against the checkout: the two production callers pass aware stamps in every path (ha.py:872 mirrors HA last_changed parsed by strptime_aware, which raises on a missing offset; ha.py:1076 stamps now_utc_exact, an aware datetime.now(local_tz)), so the new ValueError guard for naive stamps cannot fire in production and only changes the exception type from the old AttributeError; the ValueError raises before send_via_ipc, so there is no partial write; timedelta has no remaining use in db_manager.py after the import removal; _get_state_db's last_updated+Z shape matches the test's rstrip("Z") read-back; no other utcoffset().seconds // 3600 conversion site exists in the repo; test_db_manager is already registered in TEST_REGISTRY. No user-facing surface changes, so no docs or CONFIG_ITEMS update is needed.
|
Automated cleanup check from the triage bot — nothing for a maintainer to act on. Checked at head |
This is an automated draft PR generated from issue #5693 — a maintainer should review it before merging.
Fixes #5693
Summary
DatabaseManager.set_state_db()normalised a timestamp to UTC by subtractingutcoffset().seconds // 3600hours. That drops the negativedayspart of a western-hemisphere offset, so a-05:00stamp was stored 24 h early. It also truncates half- and quarter-hour offsets to the hour, so+05:30was stored 30 min late. The conversion now usesastimezone(timezone.utc).replace(tzinfo=None).A naive timestamp is refused with a
ValueErrorrather than silently treated as host-local time. The old code also failed on a naive stamp (anAttributeErrorfromNone.seconds), so only the exception type changes.This bug is live for internal-DB users outside UK time. With no
timestampargument the method stampsComponentBase.now_utc_exact, which isdatetime.now(local_tz): the configuredtimezone, not UTC. That means everyset_state()write (ha.py:1076) has been mis-stamped for those users.Testing
set_get_statesub-test inapps/predbat/tests/test_db_manager.pywith four offsets:-05:00,+05:30,-03:30(which crosses midnight forward) and+05:45(which crosses midnight back). Each case checks both the returnedlast_changedand the stored row. A naive stamp is checked to be rejected.db_manager.pystashed,tools/triage_test.sh db_managerfails (4/5):2025-12-25T10:00:00-05:00 returned last_changed 2025-12-24T15:00:00.000000, expected 2025-12-25T15:00:00.000000.tools/triage_test.sh db_managerpasses 5/5../run_pre_commitpasses: all hooks, plus the quick suite (All tests passed).Notes
impact()reported no callers because the call goes throughself.db_manager. A search found the two real callers,ha.py:872andha.py:1076. Both pass aware datetimes (strptime_aware,now_utc_exact,datetime.now(timezone.utc)), so the naive-stamp guard never fires on them. For the UTC and Europe/London stamps they already pass, the stored value is unchanged.now_utcholds a local-time stamp until the conversion line, so its name is only true after the conversion. I left it alone to keep the diff to the bug.🤖 Generated with Claude Code