fix: sync sadhna calendar with local timezone

This commit is contained in:
gosangam
2026-06-09 00:39:12 +05:30
parent cf733062f8
commit ef6e280524
+11 -6
View File
@@ -11,8 +11,8 @@
* *
* Streak counters come from the dashboard payload; the grid itself is fed by * Streak counters come from the dashboard payload; the grid itself is fed by
* the /sadhna/history range endpoint, fetched per visible period and cached * the /sadhna/history range endpoint, fetched per visible period and cached
* so paging back to a period you've seen is instant. Everything is bucketed * so paging back to a period you've seen is instant. "Today" is the browser's
* by UTC calendar day to match the backend. * local date, matching the backend's local-timezone day boundary.
*/ */
import { useEffect, useMemo, useRef, useState } from "react"; import { useEffect, useMemo, useRef, useState } from "react";
@@ -26,19 +26,24 @@ const WEEKDAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
type View = "week" | "month"; type View = "week" | "month";
// ── UTC date helpers (operate on epoch-ms at UTC midnight) ── // Each calendar day is represented as the UTC-midnight epoch-ms of that date
// (so the date string and all arithmetic stay tz-free and internally
// consistent). "Today", however, is anchored to the browser's LOCAL date so
// the day boundary matches the backend's local-timezone day reset.
function ymd(ms: number): string { function ymd(ms: number): string {
return new Date(ms).toISOString().slice(0, 10); return new Date(ms).toISOString().slice(0, 10);
} }
function startOfDayUTC(d: Date): number { // UTC-midnight ms whose calendar date is the browser's local today.
return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()); function localTodayMs(): number {
const d = new Date();
return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
} }
function startOfWeek(ms: number): number { function startOfWeek(ms: number): number {
return ms - new Date(ms).getUTCDay() * DAY; return ms - new Date(ms).getUTCDay() * DAY;
} }
export function StreakCalendar({ dash }: { dash: SadhnaDashboard | null }) { export function StreakCalendar({ dash }: { dash: SadhnaDashboard | null }) {
const todayMs = startOfDayUTC(new Date()); const todayMs = localTodayMs();
const todayStr = ymd(todayMs); const todayStr = ymd(todayMs);
const [view, setView] = useState<View>("week"); const [view, setView] = useState<View>("week");