Skip to content

fix(timestamps): emit UTC with Z suffix across backend (#79) - #440

Merged
vybe merged 1 commit into
mainfrom
fix/79-utc-timestamps
Apr 21, 2026
Merged

vybe merged 1 commit into
mainfrom
fix/79-utc-timestamps

Conversation

@dolho

@dolho dolho commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Backend emitted naive ISO timestamps from datetime.utcnow().isoformat() in 41 modules — no Z suffix, no offset. JavaScript new Date(str) then parsed them as local time, so timestamps rendered in the server's timezone instead of the client's (the bug reported against the Payments tab and elsewhere).

  • Mass-replace datetime.utcnow().isoformat() → utc_now_iso() helper (which appends Z) across 38 modules.
  • Replace 3 stray datetime.now().isoformat() callsites (agent create docker label, user role update, health check).
  • docker_service.py reads trinity.created label via parse_iso_timestamp, so legacy naive labels are tagged UTC and emitted with +00:00 on serialization.
  • Strip redundant + "Z" suffixes where the old code appended Z manually — helper already includes it. Without this, logs printed ...ZZ.

utc_now_iso() and parse_iso_timestamp() already existed in utils/helpers.py; this PR completes the mass adoption. Going forward, new backend code should use the helper.

Closes #79

Verification

API now returns UTC-marked timestamps:

/api/agents         -> "2026-04-10T08:30:00.842757+00:00"   (was no tz)
/api/activities/... -> "2026-04-17T11:58:00.091554Z"        (was no tz)
logs                -> "2026-04-21T10:42:24.745640Z"        (was "ZZ" briefly during debug, now single Z)

Both ...Z and ...+00:00 round-trip correctly through new Date(str).toLocaleString() in the browser — frontend now displays every timestamp in the viewer's local zone.

Test plan

  • Backend starts cleanly (verified locally: docker compose restart backend — no errors)
  • GET /api/agents returns created with timezone marker
  • GET /api/activities/timeline returns started_at with Z
  • Log lines carry single Z (not ZZ)
  • Open Payments tab (NeverminedPanel.vue) in a different browser timezone (or via DevTools timezone override) and confirm log entries display in local time
  • Same check on chat timestamps, schedule history, agent created-at, credential last-updated
  • datetime.fromisoformat(label) paths still handle both ...Z (new writes) and naive (legacy docker labels) — parse_iso_timestamp covers both

Follow-ups (out of scope for this PR)

  • DB rows written before this PR remain naive; frontend new Date(str) will still mis-parse those as local. Options: one-off backfill migration OR frontend parseUTC adoption sweep (utility already exists at src/frontend/src/utils/timestamps.js). The latter is mechanical and ~30 files.
  • A couple of (datetime.utcnow() - timedelta(...)).isoformat() + "Z" patterns in db/monitoring.py weren't touched — they emit single Z correctly but could be standardized via to_utc_iso(datetime_obj).

🤖 Generated with Claude Code

Backend previously called datetime.utcnow().isoformat() in 41 modules,
producing naive ISO strings like "2026-04-10T08:30:00.842757" (no
timezone). The frontend then parsed these via new Date(str), which JS
interprets as local time rather than UTC — so timestamps rendered in
the server's timezone regardless of client locale (the bug reported
against the Payments tab and elsewhere).

Changes:
- Replace datetime.utcnow().isoformat() with utc_now_iso() helper
  (returns ISO with trailing "Z") across 38 modules.
- Replace three remaining datetime.now().isoformat() callsites in
  agent_service/crud.py (docker trinity.created label), db/users.py
  (role update), and main.py (health check).
- docker_service.py reads trinity.created via parse_iso_timestamp so
  legacy naive labels are tagged UTC and serialized with offset.
- Strip redundant `+ "Z"` suffixes left behind where the old code
  appended Z manually after isoformat() — helper already includes Z.

Helper utc_now_iso() and parse_iso_timestamp() already existed in
utils/helpers.py but were not mass-adopted. This PR completes that
adoption. Going forward, new backend code should always use the helper.

API now returns timestamps like "2026-04-21T10:42:24.745640Z" (or
"+00:00" when round-tripped through Pydantic datetime), both of which
JS new Date(str) parses as UTC and toLocaleString() renders in the
client's local zone automatically.

Closes #79

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Validated via /validate-pr — mechanical sweep is correct, security clean, no architectural impact. Follow-ups (naive datetime .isoformat() in routers/approvals, audit, executions, email_auth, and frontend parseUTC adoption) are acknowledged in the PR body and out of scope here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timestamps display in server timezone instead of client local time

2 participants