feat(label): modified label of pay-periods

This commit is contained in:
Matthieu Haineault 2025-08-21 10:07:04 -04:00
parent 1155a58eac
commit bdf6662374
2 changed files with 11 additions and 28 deletions

View File

@ -13,7 +13,7 @@ export function mapPayPeriodToDto(row: PayPeriods): PayPeriodDto {
period_end: toDateString(row.period_end),
payday:pay,
pay_year: new Date(pay).getFullYear(),
label: `${start} => ${end}`,
label: `${start}.${end}`,
};
}

View File

@ -1,5 +1,4 @@
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;
@ -10,35 +9,19 @@ const toUTCDate = (iso: string | Date) => {
};
export const toDateString = (d: Date) => d.toISOString().slice(0, 10);
export function payYearOfDate(date: string | Date, anchor_ISO = ANCHOR_ISO): number {
const ANCHOR = toUTCDate(anchor_ISO);
export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number {
const ANCHOR = toUTCDate(anchorISO);
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;
}
//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);
//compute labels for periods
export function computePeriod(pay_year: number, period_no: number, anchorISO = ANCHOR_ISO) {
const ANCHOR = toUTCDate(anchorISO);
const cycles = pay_year - (ANCHOR.getUTCFullYear() + 1);
const offset_periods = cycles * PERIODS_PER_YEAR + (period_no - 1);
const start = new Date(+ANCHOR + offset_periods * PERIOD_DAYS * MS_PER_DAY);
const offsetPeriods = cycles * PERIODS_PER_YEAR + (period_no - 1);
const start = new Date(+ANCHOR + offsetPeriods * 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 {
@ -47,12 +30,12 @@ export function computePeriod(pay_year: number, period_no: number, anchor_ISO =
payday: toDateString(pay),
period_start: toDateString(start),
period_end: toDateString(end),
label: buildLabel(start, end),
label: `${toDateString(start)}.${toDateString(end)}`,
start, end,
};
}
//list of all 26 periods for a full year
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));
export function listPayYear(pay_year: number, anchorISO = ANCHOR_ISO) {
return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO));
}