From f9f2f172344eac85dd834d5316ea7d26d22e0076 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com> Date: Fri, 10 May 2024 15:12:37 -0400 Subject: [PATCH] Fix leap year issue --- lemmy-translations | 2 +- src/shared/utils/helpers/is-cake-day.ts | 27 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lemmy-translations b/lemmy-translations index b2de37b3..f0ab81de 160000 --- a/lemmy-translations +++ b/lemmy-translations @@ -1 +1 @@ -Subproject commit b2de37b31ea6294fdcd7f41492ea241b62ee9099 +Subproject commit f0ab81deea347c433277a90ae752b10f68473719 diff --git a/src/shared/utils/helpers/is-cake-day.ts b/src/shared/utils/helpers/is-cake-day.ts index 16aa031e..dfa4cde8 100644 --- a/src/shared/utils/helpers/is-cake-day.ts +++ b/src/shared/utils/helpers/is-cake-day.ts @@ -1,11 +1,34 @@ -import { parseISO, getYear, getDayOfYear } from "date-fns"; +import { parseISO, getYear, getDayOfYear, isLeapYear } from "date-fns"; + +const leapDay = getDayOfYear(new Date(2024, 1, 29)); export default function isCakeDay(published: string): boolean { const createDate = parseISO(published); + const createDateDayOfYear = getDayOfYear(createDate); + const isCreateDateLeapYear = isLeapYear(createDate); + const currentDate = new Date(); + let currentDateDayOfYear = getDayOfYear(currentDate); + const isCurrentDateLeapYear = isLeapYear(currentDate); + + if ( + isCreateDateLeapYear && + !isCurrentDateLeapYear && + currentDateDayOfYear >= leapDay + ) { + ++currentDateDayOfYear; + } + + if ( + !isCreateDateLeapYear && + isCurrentDateLeapYear && + createDateDayOfYear >= leapDay + ) { + --currentDateDayOfYear; + } return ( - getDayOfYear(createDate) === getDayOfYear(currentDate) && + createDateDayOfYear === currentDateDayOfYear && getYear(createDate) !== getYear(currentDate) ); }