feat(label): modified label of pay-periods

This commit is contained in:
Matthieu Haineault 2025-08-21 09:13:09 -04:00
parent 5282406b7a
commit 1155a58eac

View File

@ -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));
}