Skip to content

Fix timezone bug in analytics revenue time series date bucketing - #151

Open
michaellasmanis wants to merge 15 commits into
ai-hero-dev:live-run-throughfrom
michaellasmanis:fix_timezone_issue_for_analytics
Open

Fix timezone bug in analytics revenue time series date bucketing#151
michaellasmanis wants to merge 15 commits into
ai-hero-dev:live-run-throughfrom
michaellasmanis:fix_timezone_issue_for_analytics

Conversation

@michaellasmanis

Copy link
Copy Markdown

This should be applied to commit "05.06.02: Instructor analytics Phase 2 — revenue chart + per-course table"
This unit tests there are failing from PDT (UTC-7)

The revenue time series helpers (formatDateKey, formatMonthKey, generateDailyKeys, generateMonthlyKeys) were using local-time Date methods (getFullYear, getMonth, getDate, setHours, setDate, setMonth) to generate the date keys used for bucketing revenue data points. However, purchase timestamps are stored as ISO 8601 strings (UTC), and the SQL query groups revenue by extracting the date prefix from those UTC strings via substr(createdAt, 1, 10).

This mismatch meant that in timezones behind UTC (e.g. US timezones), a purchase made in the late local evening would have a UTC date one day ahead of the locally-generated key. The revenue from the DB query would then fail to map to any key in the generated range, silently dropping it from the results.

Switched all four helper functions to use UTC Date methods (getUTCFullYear, getUTCMonth, getUTCDate, setUTCHours, setUTCDate, setUTCMonth, Date.UTC) so the generated keys are always consistent with the UTC-based grouping in the database queries.

mattpocock and others added 15 commits April 2, 2026 09:12
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…able

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The revenue time series helpers (formatDateKey, formatMonthKey,
generateDailyKeys, generateMonthlyKeys) were using local-time Date
methods (getFullYear, getMonth, getDate, setHours, setDate, setMonth)
to generate the date keys used for bucketing revenue data points.
However, purchase timestamps are stored as ISO 8601 strings (UTC),
and the SQL query groups revenue by extracting the date prefix from
those UTC strings via substr(createdAt, 1, 10).

This mismatch meant that in timezones behind UTC (e.g. US timezones),
a purchase made in the late local evening would have a UTC date one
day ahead of the locally-generated key. The revenue from the DB query
would then fail to map to any key in the generated range, silently
dropping it from the results.

Switched all four helper functions to use UTC Date methods
(getUTCFullYear, getUTCMonth, getUTCDate, setUTCHours, setUTCDate,
setUTCMonth, Date.UTC) so the generated keys are always consistent
with the UTC-based grouping in the database queries.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

3 participants