Skip to content

fix: bucket the badge streak by local day instead of UTC#905

Open
Anexus5919 wants to merge 1 commit into
Somil450:mainfrom
Anexus5919:fix/badge-streak-local-day
Open

fix: bucket the badge streak by local day instead of UTC#905
Anexus5919 wants to merge 1 commit into
Somil450:mainfrom
Anexus5919:fix/badge-streak-local-day

Conversation

@Anexus5919

Copy link
Copy Markdown
Contributor

📌 Related Issue

Fixes #868


📝 Description

calculateStreak in useBadges.ts bucketed each workout by its UTC date (toISOString().slice(0, 10)) and computed today/yesterday in UTC. For users not at UTC, workouts on the same local day can land in different UTC buckets and consecutive local days can collapse into one, miscounting the badge streak near the local/UTC day boundary.

🔹 What has been changed?

  • src/hooks/useBadges.ts: bucket each workout, and today/yesterday, by its local calendar date (${getFullYear()}-${getMonth()+1}-${getDate()}) instead of the UTC date. calculateStreak is now exported so the pure logic can be unit tested.
  • src/hooks/__tests__/useBadges.test.ts: adds tests, including the regression case of two workouts on consecutive local days that fall on the same UTC date.

🔹 Why are these changes needed?

  • A per-user streak should be measured in the user's local days. UTC bucketing miscounts near midnight for any non-UTC timezone.

🛠️ Type of Change

  • 🐛 Bug Fix

🧪 Testing

✅ Tests Performed

  • Tested locally
  • npx vitest run src/hooks/__tests__/useBadges.test.ts -> 3 tests pass.
  • Confirmed the new boundary test is a real guard: with the previous UTC bucketing it fails (two consecutive local days are counted as a streak of 1 instead of 2), and it passes with the local-day fix.
  • npx tsc --noEmit: useBadges.ts clean; npx eslint: 0 problems.

🌐 Browsers Tested

Not applicable (streak-count logic; verified via unit tests).


📷 Screenshots / Demo (if applicable)

Not applicable.


📋 Checklist

  • I have read the project's CONTRIBUTING guidelines
  • My code follows the project style guidelines
  • I have performed a self-review of my code
  • I have tested my changes locally
  • I have added/updated documentation where necessary (not applicable)
  • My changes do not introduce new warnings or errors
  • This PR is linked to an existing issue

💬 Additional Notes

Branched from latest upstream/main (5464425). This is the badge streak in useBadges.ts, separate from the workout streak in streakUtils.ts (#867).

@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

@Anexus5919 is attempting to deploy a commit to the somiljain2024-4175's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Anexus5919

Copy link
Copy Markdown
Contributor Author

@Somil450 @diksha78dev Kindly have a review on this pr. Thanks!

calculateStreak in useBadges bucketed each workout by its UTC date
(toISOString().slice(0, 10)) and computed today/yesterday in UTC. For users
not at UTC, workouts on the same local day could land in different UTC
buckets and consecutive local days could collapse into one, miscounting the
badge streak near the local/UTC day boundary.

Bucket each workout by its local calendar date instead. calculateStreak is
exported so the behavior can be unit tested.
@Anexus5919 Anexus5919 force-pushed the fix/badge-streak-local-day branch from 761e838 to 0885c6b Compare June 23, 2026 17:34
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]: Badge streak (useBadges.calculateStreak) buckets workouts by UTC date, miscounting streaks near the local/UTC day boundary

1 participant