Skip to content

fix: use local date methods for Pomodoro session bucketing in getStatsForDate#1758

Open
ITblacksheep wants to merge 2 commits intocallumalpass:mainfrom
ITblacksheep:bugfix/1658-fixing-misbucketed-sessions
Open

fix: use local date methods for Pomodoro session bucketing in getStatsForDate#1758
ITblacksheep wants to merge 2 commits intocallumalpass:mainfrom
ITblacksheep:bugfix/1658-fixing-misbucketed-sessions

Conversation

@ITblacksheep
Copy link
Copy Markdown

Summary

Fixes #1658 — Pomodoro stats view incorrectly counts evening sessions as belonging to the next day.

Root Cause

Sessions are stored by getCurrentTimestamp() with the local timezone offset (e.g. "2026-04-02T21:09:25.755-04:00"). When getStatsForDate calls formatDateForStorage(new Date(session.startTime)), it uses getUTCDate() internally which converts the Eastern session to UTC next day:
"2026-04-02T21:09:25.755-04:00"
→ new Date() → 2026-04-03T01:09:25.755Z internally
→ getUTCDate() → 3
→ formatDateForStorage returns "2026-04-03"
A session at 9pm Eastern on April 2nd gets filed under April 3rd, inflating today's count.
Fix
In getStatsForDate, replace formatDateForStorage with local date methods which correctly resolve the stored timezone offset:
typescript// Before
const sessionDate = formatDateForStorage(new Date(session.startTime));

// After
const d = new Date(session.startTime);
const sessionDate = ${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')};
Also updated getTodayStats to pass getTodayLocal() directly without converting to a UTC anchor first.
This approach is also travel-safe — JavaScript resolves local date methods using the stored timezone offset (-04:00), not the current system timezone. So a session created in Eastern time always reads as its Eastern date regardless of where you are when you read it.
Verification
Confirmed with real pomodoroHistory data — before fix:
Buggy count (UTC): 5 completed sessions today
Fixed count (local): 3 completed sessions today
2 sessions from 2026-04-02T21:xx-04:00 were incorrectly counted as April 3rd.
Verified in Obsidian console:
javascriptconst stats = await plugin.pomodoroService.getTodayStats();
// { pomodorosCompleted: 6, totalMinutes: 150, completionRate: 100 }

Tests

Added regression tests in tests/timezone-bugs/pomodoro-session-bucketing.test.ts following the existing timezone bug documentation pattern. Tests cover:

Evening sessions correctly bucketed to local date
Morning sessions unaffected
Real-world data reproducing the exact bug
Travel-safe behavior across multiple timezones

Pre-existing Failing Test

tests/unit/modals/TaskCreationModal.test.ts — should apply task creation defaults — was already failing before this PR. It exhibits the same UTC/local date mismatch pattern (new Date('2025-01-15') creates UTC midnight which shifts in non-UTC timezones). Flagging for a follow-up issue but leaving out of scope for this PR to keep the fix focused.

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.

[Bug]: Pomodoro stats mis-bucket sessions into "Today" around local midnight (timezone)

1 participant