From 415c8a62fa48cd575558add437f993d2f2730381 Mon Sep 17 00:00:00 2001 From: Alexey Novikov Date: Fri, 8 Mar 2024 14:14:18 -0500 Subject: [PATCH] Optimized broadcast week calculation --- src/week.ts | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/src/week.ts b/src/week.ts index ac60319..ade7408 100644 --- a/src/week.ts +++ b/src/week.ts @@ -2,17 +2,6 @@ import { DateTime } from "luxon"; import { IfValid, isValid } from "./helpers"; -const DAY = 24 * 60 * 60 * 1000; - -function isLastWeekOverflown(date: DateTime): boolean { - const yearEnd = date.endOf("year"); - const yearEndWeekDay = yearEnd.weekday; - if (yearEndWeekDay === 7) { - return false; - } - return yearEnd.toMillis() - date.toMillis() <= yearEndWeekDay * DAY; -} - /** * returns broadcast week number (1-54) for a given date **/ @@ -23,19 +12,5 @@ export function getBroadcastWeek( return null as IfValid; } - const day = date.startOf("day"); - - if (isLastWeekOverflown(day)) { - return 1 as IfValid; - } - - const yearStart = day.startOf("year"); - const yearStartWeekDay = yearStart.weekday - 1; - const weekNo = Math.ceil( - ((day.valueOf() - yearStart.toMillis() + yearStartWeekDay * DAY) / DAY + - 1) / - 7, - ); - - return weekNo as IfValid; + return Math.ceil(date.endOf("week").ordinal / 7) as IfValid; }