-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix leap year issue * Show same cake day date independent of timezone (#2455) * Show same cake day date independent of timezone * Remove commented out assertions --------- Co-authored-by: matc-pub <[email protected]>
- Loading branch information
1 parent
0b35d4a
commit d705f36
Showing
3 changed files
with
16 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { parseISO, getYear, getDayOfYear } from "date-fns"; | ||
import { getYear, isSameDay, isSameYear, parse, setYear } from "date-fns"; | ||
|
||
// Returns a date in local time with the same year, month and day. Ignores the | ||
// source timezone. The goal is to show the same date in all timezones. | ||
export function cakeDate(published: string): Date { | ||
return parse(published.substring(0, 10), "yyyy-MM-dd", new Date(0)); | ||
} | ||
|
||
export default function isCakeDay(published: string): boolean { | ||
const createDate = parseISO(published); | ||
const createDate = cakeDate(published); | ||
const currentDate = new Date(); | ||
|
||
// The day-overflow of Date makes leap days become 03-01 in non leap years. | ||
return ( | ||
getDayOfYear(createDate) === getDayOfYear(currentDate) && | ||
getYear(createDate) !== getYear(currentDate) | ||
isSameDay(currentDate, setYear(createDate, getYear(currentDate))) && | ||
!isSameYear(currentDate, createDate) | ||
); | ||
} |