fix: use UTC-aware datetime for bucket.created default#136
fix: use UTC-aware datetime for bucket.created default#136fanxing11 wants to merge 1 commit intoActivityWatch:masterfrom
Conversation
…faults datetime.now() without arguments returns a naive local datetime, which gets serialized with a +00:00 offset by iso8601.parse_date, causing bucket.created to appear shifted by the host's TZ offset. Use datetime.now(timezone.utc) to produce a proper UTC-aware datetime. Same fix applied to EventModel.timestamp for consistency, though event timestamps are typically provided by watchers. Fixes ActivityWatch#134
Greptile SummaryThis PR fixes a timezone bug where
Confidence Score: 5/5Safe to merge — a two-line targeted fix that corrects an observable datetime offset bug with no side effects on the rest of the storage layer. The change is minimal and self-contained: timezone is already imported, the lambda wrapper is the idiomatic peewee pattern for callable defaults, and the UTC time value is correct regardless of whether peewee strips the +00:00 suffix when writing to SQLite. No existing logic is restructured and no interfaces change. No files require special attention. Important Files Changed
Sequence DiagramsequenceDiagram
participant Watcher
participant PeeweeStorage
participant BucketModel
participant SQLite
participant iso8601
Watcher->>PeeweeStorage: "create_bucket(bucket_id, ..., created=...)"
PeeweeStorage->>BucketModel: "BucketModel.create(created=created, ...)"
Note over BucketModel: If created not provided, default fires
Note over BucketModel: datetime.now(timezone.utc) → UTC-aware datetime
BucketModel->>SQLite: store datetime string (UTC value)
Watcher->>PeeweeStorage: get_metadata(bucket_id)
PeeweeStorage->>BucketModel: BucketModel.get(...).json()
BucketModel->>SQLite: SELECT created FROM bucketmodel
SQLite-->>BucketModel: 2024-01-01 00:00:00 (UTC value)
BucketModel->>iso8601: parse_date(created string)
Note over iso8601: iso8601 defaults bare datetime to UTC
Note over iso8601: Result correct because value was already UTC
iso8601-->>BucketModel: datetime(2024,1,1,0,0,0,UTC)
BucketModel-->>PeeweeStorage: created 2024-01-01T00:00:00+00:00
PeeweeStorage-->>Watcher: bucket metadata
Reviews (1): Last reviewed commit: "fix: use UTC-aware datetime for bucket.c..." | Re-trigger Greptile |
`datetime.now()` without arguments returns a naive local datetime. When peewee stores it and `iso8601.parse_date` later serializes it, the parser tags the bare datetime as UTC by default — so `bucket.created` ends up shifted by the host's TZ offset.
Switching to `datetime.now(timezone.utc)` produces a proper UTC-aware datetime, matching the convention that watchers already use for event timestamps.
Applied the same fix to `EventModel.timestamp` for consistency, though that default is rarely hit since watchers provide their own timestamps.
Fixes #134