mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
## Problem Job dates on the dashboard displayed the raw UTC wall-clock instead of the viewer's local time. e.g. a job submitted at ~4:10 PM local (viewer at UTC-2) showed as `8/14/2026, 6:10:25 PM`. ## Root cause `created_at` (and timeline/log timestamps) were built from naive `datetime.utcnow()` and serialized via `model_dump(mode='json')` / `.isoformat()`, producing ISO strings with **no timezone offset** (`"2026-08-14T18:10:25.123456"`). Firestore stored them as plain strings and returned them verbatim. The frontend's `new Date(str)` parses an offset-less ISO date-time as **local** time, so the UTC value was shown unconverted — off by the viewer's UTC offset. ## Fix **Frontend (fixes all existing + new data, no migration):** - New `parseServerDate()` util in `frontend/lib/utils.ts` — treats offset-less ISO date-times as UTC (appends `Z`); passes through strings that already carry `Z`/`±HH:MM`, plus `Date`/epoch inputs. - Applied at every customer-facing server-timestamp display: `JobCard` created-at, `JobLogs` timestamps, referral payout dates, and referral-discount expiry (`BuyCreditsDialog` + dashboard). - 8 unit tests covering naive/`Z`/`+00:00`/non-UTC-offset/date-only/epoch/`Date` cases. **Backend (makes stored timestamps unambiguous going forward):** - `create_job` and timeline events now use `datetime.now(timezone.utc)`, so serialized ISO strings carry a `+00:00` offset. Arithmetic consumers (`job_health_service`) already guard for both naive and aware datetimes. ## Testing - Backend: 210 targeted tests pass (job_manager / firestore / timeline / job_health / countdown). - Frontend: full suite 1108 tests pass (85 suites), including the new util tests. ## Notes - `/admin/*` pages (English-only, internal) still use `new Date()` directly; they benefit from the backend change for new jobs but were left otherwise untouched as out of scope. - CodeRabbit local review was rate-limited (free OSS quota), so this PR intentionally omits `@coderabbitai ignore` to let the bot review. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
32 lines
1.3 KiB
TypeScript
32 lines
1.3 KiB
TypeScript
import { clsx, type ClassValue } from 'clsx'
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs))
|
|
}
|
|
|
|
/**
|
|
* Parse a timestamp returned by the backend into a Date.
|
|
*
|
|
* Backend timestamps (job.created_at, log timestamps, etc.) are UTC but are
|
|
* frequently serialized from naive `datetime.utcnow()` values, producing ISO
|
|
* strings with NO timezone offset (e.g. "2026-08-14T18:10:25.123456"). The
|
|
* JS `Date` constructor parses such offset-less date-time strings as *local*
|
|
* time, which displays the raw UTC wall-clock unconverted (off by the viewer's
|
|
* UTC offset). To fix this we treat offset-less timestamps as UTC by appending
|
|
* "Z". Strings that already carry an offset ("Z" or "+HH:MM"/"-HH:MM") — or that
|
|
* aren't the expected ISO date-time shape — are passed through unchanged.
|
|
*/
|
|
export function parseServerDate(value: string | number | Date): Date {
|
|
if (value instanceof Date) return value
|
|
if (typeof value === 'number') return new Date(value)
|
|
|
|
// ISO date-time with no timezone designator → assume UTC.
|
|
// Matches "YYYY-MM-DDTHH:MM:SS" with optional fractional seconds and no
|
|
// trailing Z / ±HH:MM offset.
|
|
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?$/.test(value)) {
|
|
return new Date(`${value}Z`)
|
|
}
|
|
return new Date(value)
|
|
}
|