Skip to content

fix: parse SQLite UTC timestamps with explicit Z suffix#229

Merged
jalehman merged 2 commits into
Martian-Engineering:mainfrom
tingyiy:fix/utc-timestamp-parsing
Apr 3, 2026
Merged

fix: parse SQLite UTC timestamps with explicit Z suffix#229
jalehman merged 2 commits into
Martian-Engineering:mainfrom
tingyiy:fix/utc-timestamp-parsing

Conversation

@tingyiy

@tingyiy tingyiy commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Problem

SQLite's datetime('now') produces bare strings like 2026-03-30 23:11:15 without a timezone indicator. Per ECMA-262 §21.4.3.2, new Date() parses these as local time, silently shifting results when the runtime's TZ is anything other than UTC.

This causes wrong timestamps in lcm_grep, lcm_expand, and summary injection when running in containers or hosts with a non-UTC timezone (e.g. TZ=America/Los_Angeles).

Fix

  • New src/store/parse-utc-timestamp.ts with parseUtcTimestamp() and parseUtcTimestampOrNull() helpers that append Z to bare timestamps before parsing
  • Replaced all new Date(row.*) calls in conversation-store.ts, summary-store.ts, and migration.ts
  • Strings that already have a timezone indicator (Z or ±HH:MM) are parsed as-is

Changes

  • 4 files changed, 50 insertions, 28 deletions
  • src/store/parse-utc-timestamp.ts — new utility (30 lines)
  • src/store/conversation-store.ts — import + replace 7 call sites
  • src/store/summary-store.ts — import + replace 9 call sites
  • src/db/migration.ts — delegate parseTimestamp() to new helper

Fixes #216

SQLite datetime('now') stores UTC timestamps without a Z suffix.
JavaScript's Date constructor parses bare datetime strings as local time
per ECMA-262, causing timestamps to shift by the local timezone offset.

This adds a parseUtcTimestamp() helper that appends 'Z' before parsing,
and applies it to all new Date(row.*) calls in conversation-store,
summary-store, and migration.

Fixes Martian-Engineering#216
@tingyiy tingyiy force-pushed the fix/utc-timestamp-parsing branch from 4f165fc to 788daf8 Compare April 2, 2026 16:34
Keep explicit timezone offsets intact in the shared timestamp parser while still normalizing bare SQLite datetime('now') values to UTC. Add focused parser coverage for bare, Z-suffixed, and offset-bearing timestamps, and include a patch changeset for the behavior fix.

Regeneration-Prompt: |
  Address the PR review finding on the shared SQLite timestamp parser introduced for issue Martian-Engineering#216. Preserve the intended fix for bare datetime('now') strings that lack a timezone suffix, but do not break timestamps that already include Z or an explicit offset like +02:00. Add narrow tests that prove all three cases still parse correctly, and include a patch changeset because this affects user-visible timestamp handling.
@jalehman

jalehman commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

Thank you!

@jalehman jalehman merged commit 1fb8b8f into Martian-Engineering:main Apr 3, 2026
1 check passed
@github-actions github-actions Bot mentioned this pull request Apr 3, 2026
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.

SQLite UTC timestamps parsed as local time — wrong display and filtering in non-UTC environments

2 participants