From 1155a58eace0527c8f2e7ec23ada19809500abc7 Mon Sep 17 00:00:00 2001 From: Matthieu Haineault Date: Thu, 21 Aug 2025 09:13:09 -0400 Subject: [PATCH] feat(label): modified label of pay-periods --- .../pay-periods/utils/pay-year.util.ts | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/modules/pay-periods/utils/pay-year.util.ts b/src/modules/pay-periods/utils/pay-year.util.ts index 1330601..8f2e1fe 100644 --- a/src/modules/pay-periods/utils/pay-year.util.ts +++ b/src/modules/pay-periods/utils/pay-year.util.ts @@ -1,4 +1,5 @@ export const ANCHOR_ISO = '2023-12-17'; // ancre date + const PERIOD_DAYS = 14; const PERIODS_PER_YEAR = 26; const MS_PER_DAY = 86_400_000; @@ -9,19 +10,35 @@ const toUTCDate = (iso: string | Date) => { }; export const toDateString = (d: Date) => d.toISOString().slice(0, 10); -export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number { - const ANCHOR = toUTCDate(anchorISO); +export function payYearOfDate(date: string | Date, anchor_ISO = ANCHOR_ISO): number { + const ANCHOR = toUTCDate(anchor_ISO); const d = toUTCDate(date); const days = Math.floor((+d - +ANCHOR) / MS_PER_DAY); const cycles = Math.floor(days / (PERIODS_PER_YEAR * PERIOD_DAYS)); return ANCHOR.getUTCFullYear() + 1 + cycles; } -//compute labels for periods -export function computePeriod(pay_year: number, period_no: number, anchorISO = ANCHOR_ISO) { - const ANCHOR = toUTCDate(anchorISO); + +//build a string example : 10.au.23.aout.2025 or 24.aout.au.6.septembre.2025 +function buildLabel(start: Date, end: Date): string { + const start_day = String(start.getUTCDate()); + const end_day = String(end.getUTCDate()); + const start_month = String(start.getUTCMonth()); + const end_month = String(end.getUTCMonth()); + const year = String(end.getUTCFullYear()); + + if(start.getUTCMonth() === end.getUTCMonth() && start.getUTCFullYear() === end.getUTCFullYear()){ + return [start_day, 'au', end_day, end_month, year].join('.'); + } + + return [start_day, start_month, 'au', end_day, end_month, year].join('.'); +} + +//compute periods with label +export function computePeriod(pay_year: number, period_no: number, anchor_ISO = ANCHOR_ISO) { + const ANCHOR = toUTCDate(anchor_ISO); const cycles = pay_year - (ANCHOR.getUTCFullYear() + 1); - const offsetPeriods = cycles * PERIODS_PER_YEAR + (period_no - 1); - const start = new Date(+ANCHOR + offsetPeriods * PERIOD_DAYS * MS_PER_DAY); + const offset_periods = cycles * PERIODS_PER_YEAR + (period_no - 1); + const start = new Date(+ANCHOR + offset_periods * PERIOD_DAYS * MS_PER_DAY); const end = new Date(+start + (PERIOD_DAYS - 1) * MS_PER_DAY); const pay = new Date(end.getTime() + 6 * MS_PER_DAY); return { @@ -30,12 +47,12 @@ export function computePeriod(pay_year: number, period_no: number, anchorISO = A payday: toDateString(pay), period_start: toDateString(start), period_end: toDateString(end), - label: `${toDateString(start)} → ${toDateString(end)}`, + label: buildLabel(start, end), start, end, }; } //list of all 26 periods for a full year -export function listPayYear(pay_year: number, anchorISO = ANCHOR_ISO) { - return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO)); +export function listPayYear(pay_year: number, anchor_ISO = ANCHOR_ISO) { + return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchor_ISO)); }