Skip to content

Commit

Permalink
Incorrect Year according to Week Display on Leaderboard (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyurajeesh authored Jan 2, 2025
1 parent 24118eb commit 07783d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import {
formatDuration as _formatDuration,
intervalToDuration,
format,
getISOWeek,
startOfISOWeek,
addDays,
getISOWeekYear,
} from "date-fns";
import { env } from "@/env.mjs";

Expand All @@ -17,11 +21,16 @@ export const formatDuration = (duration_in_ms: number) =>
.join(" ");

export const getWeekNumber = (date: Date) => {
const d = new Date(date);
const dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil(((Number(d) - Number(yearStart)) / 86400000 + 1) / 7);
let weekNumber = getISOWeek(date);

if (weekNumber === 1 && date.getMonth() === 11) {
const startOfWeek = startOfISOWeek(date);
if (startOfWeek.getFullYear() !== getISOWeekYear(date)) {
const lastDayOfLastWeekThisYear = addDays(startOfWeek, -1);
weekNumber = getISOWeek(lastDayOfLastWeekThisYear) + 1;
}
}
return weekNumber;
};

const now = new Date();
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 07783d1

Please sign in to comment.