Merge branch 'main' of git.targo.ca:Targo/targo_backend
This commit is contained in:
commit
3741bf3b26
|
|
@ -3,32 +3,32 @@ import { ANCHOR_ISO, MS_PER_DAY, PERIODS_PER_YEAR, PERIOD_DAYS } from "src/time-
|
|||
|
||||
//ensures the week starts from sunday
|
||||
export function weekStartSunday(date_local: Date): Date {
|
||||
const start = new Date(Date.UTC(date_local.getFullYear(), date_local.getMonth(), date_local.getDate()));
|
||||
const dow = start.getDay(); // 0 = dimanche
|
||||
start.setDate(start.getDate() - dow);
|
||||
start.setHours(0, 0, 0, 0);
|
||||
return start;
|
||||
const start = new Date(Date.UTC(date_local.getFullYear(), date_local.getMonth(), date_local.getDate()));
|
||||
const dow = start.getDay(); // 0 = dimanche
|
||||
start.setDate(start.getDate() - dow);
|
||||
start.setHours(0, 0, 0, 0);
|
||||
return start;
|
||||
}
|
||||
|
||||
//converts string to HHmm format
|
||||
export const toStringFromHHmm = (date: Date): string => {
|
||||
const hh = date.getUTCHours().toString().padStart(2, '0');
|
||||
const mm = date.getUTCMinutes().toString().padStart(2, '0');
|
||||
return `${hh}:${mm}`;
|
||||
const hh = date.getUTCHours().toString().padStart(2, '0');
|
||||
const mm = date.getUTCMinutes().toString().padStart(2, '0');
|
||||
return `${hh}:${mm}`;
|
||||
}
|
||||
|
||||
//converts string to Date format
|
||||
export const toStringFromDate = (date: Date) =>
|
||||
date.toISOString().slice(0,10);
|
||||
export const toStringFromDate = (date: Date) =>
|
||||
date.toISOString().slice(0, 10);
|
||||
|
||||
|
||||
|
||||
//converts HHmm format to string
|
||||
export const toHHmmFromString = (hhmm: string): Date => {
|
||||
const [hh, mm] = hhmm.split(':').map(Number);
|
||||
const date = new Date('1970-01-01T00:00:00.000Z');
|
||||
date.setUTCHours(hh, mm, 0, 0);
|
||||
return new Date(date);
|
||||
const [hh, mm] = hhmm.split(':').map(Number);
|
||||
const date = new Date('1970-01-01T00:00:00.000Z');
|
||||
date.setUTCHours(hh, mm, 0, 0);
|
||||
return new Date(date);
|
||||
}
|
||||
|
||||
//converts string to HHmm format
|
||||
|
|
@ -40,52 +40,52 @@ export const toHHmmFromDate = (input: Date | string): string => {
|
|||
}
|
||||
|
||||
//converts Date format to string
|
||||
export const toDateFromString = (ymd: string | Date): Date => {
|
||||
return new Date(`${ymd}`);
|
||||
export const toDateFromString = (ymd: string | Date): Date => {
|
||||
return new Date(ymd);
|
||||
}
|
||||
|
||||
export const toUTCDateFromString = (iso: string | Date) => {
|
||||
const d = typeof iso === 'string' ? new Date(iso + 'T00:00:00.000Z') : iso;
|
||||
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
|
||||
const d = typeof iso === 'string' ? new Date(iso + 'T00:00:00.000Z') : iso;
|
||||
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
|
||||
};
|
||||
|
||||
export const sevenDaysFrom = (date: Date | string): Date[] => {
|
||||
return Array.from({length: 7 }, (_,i) => {
|
||||
return Array.from({ length: 7 }, (_, i) => {
|
||||
const d = new Date(date);
|
||||
d.setUTCDate(d.getUTCDate() + i );
|
||||
d.setUTCDate(d.getUTCDate() + i);
|
||||
return d;
|
||||
});
|
||||
}
|
||||
|
||||
export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number {
|
||||
const ANCHOR = toUTCDateFromString(anchorISO);
|
||||
const d = toUTCDateFromString(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;
|
||||
const ANCHOR = toUTCDateFromString(anchorISO);
|
||||
const d = toUTCDateFromString(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 = toUTCDateFromString(anchorISO);
|
||||
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 end = new Date(+start + (PERIOD_DAYS - 1) * MS_PER_DAY);
|
||||
const pay = new Date(end.getTime() + 6 * MS_PER_DAY);
|
||||
return {
|
||||
period_no: period_no,
|
||||
pay_year: pay_year,
|
||||
payday: toStringFromDate(pay),
|
||||
period_start: toStringFromDate(start),
|
||||
period_end: toStringFromDate(end),
|
||||
label: `${toStringFromDate(start)}.${toStringFromDate(end)}`,
|
||||
start, end,
|
||||
};
|
||||
const ANCHOR = toUTCDateFromString(anchorISO);
|
||||
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 end = new Date(+start + (PERIOD_DAYS - 1) * MS_PER_DAY);
|
||||
const pay = new Date(end.getTime() + 6 * MS_PER_DAY);
|
||||
return {
|
||||
period_no: period_no,
|
||||
pay_year: pay_year,
|
||||
payday: toStringFromDate(pay),
|
||||
period_start: toStringFromDate(start),
|
||||
period_end: toStringFromDate(end),
|
||||
label: `${toStringFromDate(start)}.${toStringFromDate(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));
|
||||
return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO));
|
||||
}
|
||||
|
||||
export const overlaps = (a: { start: Date; end: Date }, b: { start: Date; end: Date }) =>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user