Skip to content

fix(db): convert set_state_db timestamps to UTC with astimezone - #5698

Open
springfall2008 wants to merge 2 commits into
mainfrom
fix/db-manager-utc-offset-5693
Open

springfall2008 wants to merge 2 commits into
mainfrom
fix/db-manager-utc-offset-5693

Conversation

@springfall2008

Copy link
Copy Markdown
Owner

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 subtracting utcoffset().seconds // 3600 hours. That drops the negative days part of a western-hemisphere offset, so a -05:00 stamp was stored 24 h early. It also truncates half- and quarter-hour offsets to the hour, so +05:30 was stored 30 min late. The conversion now uses astimezone(timezone.utc).replace(tzinfo=None).

A naive timestamp is refused with a ValueError rather than silently treated as host-local time. The old code also failed on a naive stamp (an AttributeError from None.seconds), so only the exception type changes.

This bug is live for internal-DB users outside UK time. With no timestamp argument the method stamps ComponentBase.now_utc_exact, which is datetime.now(local_tz): the configured timezone, not UTC. That means every set_state() write (ha.py:1076) has been mis-stamped for those users.

Testing

  • Extended the existing set_get_state sub-test in apps/predbat/tests/test_db_manager.py with four offsets: -05:00, +05:30, -03:30 (which crosses midnight forward) and +05:45 (which crosses midnight back). Each case checks both the returned last_changed and the stored row. A naive stamp is checked to be rejected.
  • Fails without the fix. With only db_manager.py stashed, tools/triage_test.sh db_manager fails (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.
  • Passes with the fix. tools/triage_test.sh db_manager passes 5/5.
  • ./run_pre_commit passes: all hooks, plus the quick suite (All tests passed).

Notes

  • Blast radius: GitNexus impact() reported no callers because the call goes through self.db_manager. A search found the two real callers, ha.py:872 and ha.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.
  • The local variable now_utc holds 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

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 springfall2008 self-assigned this Oct 22, 2026
@springfall2008 springfall2008 added BOT_REVIEW Trigger an autotriage BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push labels Oct 22, 2026

@springfall2008 springfall2008 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@springfall2008 springfall2008 removed the BOT_REVIEW Trigger an autotriage label Oct 22, 2026
@springfall2008

Copy link
Copy Markdown
Owner Author

Automated cleanup check from the triage bot — nothing for a maintainer to act on.

Checked at head bd97f803: the branch is already up to date with main, there are no inline review threads, the one review on the PR reported no findings, and CI is green (pre-commit and kernel-binaries pass; label skipped). No changes pushed.

@springfall2008 springfall2008 removed the BOT_CLEANUP Trigger: bot should address PR review feedback and CI failures, then commit and push label Oct 22, 2026
@chalfontchubby chalfontchubby added the BOT_PR_OPENED Bot PR flow opened a draft PR for this ticket label Oct 25, 2026
@chalfontchubby chalfontchubby self-assigned this Oct 25, 2026
@chalfontchubby
chalfontchubby marked this pull request as ready for review October 28, 2026 21:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BOT_PR_OPENED Bot PR flow opened a draft PR for this ticket

Projects

None yet

Development

Successfully merging this pull request may close these issues.

db_manager.set_state_db converts to UTC wrongly for negative and half-hour offsets

2 participants