Skip to content

fix: use UTC-aware datetime for bucket.created default#136

Open
fanxing11 wants to merge 1 commit intoActivityWatch:masterfrom
fanxing11:fix/bucket-created-utc-timezone
Open

fix: use UTC-aware datetime for bucket.created default#136
fanxing11 wants to merge 1 commit intoActivityWatch:masterfrom
fanxing11:fix/bucket-created-utc-timezone

Conversation

@fanxing11
Copy link
Copy Markdown

`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

…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-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 10, 2026

Greptile Summary

This PR fixes a timezone bug where BucketModel.created (and EventModel.timestamp) defaulted to datetime.now(), a naive local datetime. Because iso8601.parse_date assumes UTC when there is no offset, timestamps from non-UTC hosts were silently shifted by the local TZ offset. Switching the defaults to lambda: datetime.now(timezone.utc) ensures the stored value is already in UTC, so round-tripping through SQLite and iso8601.parse_date produces the correct wall-clock time regardless of host timezone.

  • BucketModel.created: now uses lambda: datetime.now(timezone.utc) so newly-created buckets always record an accurate UTC creation time.
  • EventModel.timestamp: same fix applied for consistency, though this default is rarely exercised since watchers supply their own timestamps via EventModel.from_event.

Confidence Score: 5/5

Safe 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

Filename Overview
aw_datastore/storages/peewee.py Changes BucketModel.created and EventModel.timestamp defaults from datetime.now (naive local time) to lambda: datetime.now(timezone.utc) (UTC-aware); timezone is already imported on line 4 and the rest of the file is unchanged.

Sequence Diagram

sequenceDiagram
    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
Loading

Reviews (1): Last reviewed commit: "fix: use UTC-aware datetime for bucket.c..." | Re-trigger Greptile

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.

bucket.created returned as local time tagged with +00:00 (peewee storage)

2 participants