Refactors date calculations to use ISO week

Adopts ISO week date calculation (Monday-based) for consistency
and accuracy across calendar components. This change ensures
standardized week determination, resolving potential discrepancies
in week-based operations.
This commit is contained in:
Janus Knudsen 2025-08-19 23:04:56 +02:00
parent 18f7953db4
commit efc1742dad
2 changed files with 41 additions and 32 deletions

View file

@ -13,29 +13,27 @@ export class DateCalculator {
}
/**
* Get dates for work week based on week start and work days configuration
* @param weekStart - The start date of the week (could be Sunday or Monday)
* Get dates for work week based on ISO week (starts Monday)
* @param weekStart - Any date in the week
* @returns Array of dates for the configured work days
*/
getWorkWeekDates(weekStart: Date): Date[] {
const dates: Date[] = [];
const workWeekSettings = this.config.getWorkWeekSettings();
const dateSettings = this.config.getDateViewSettings();
const firstDayOfWeek = dateSettings.firstDayOfWeek; // 0=Sunday, 1=Monday
// Get the actual start of the week based on configuration
const actualWeekStart = this.getWeekStart(weekStart, firstDayOfWeek);
// Get Monday of the ISO week
const monday = this.getISOWeekStart(weekStart);
// Calculate dates for each work day
workWeekSettings.workDays.forEach(dayOfWeek => {
const date = new Date(actualWeekStart);
const date = new Date(monday);
if (firstDayOfWeek === 1 && dayOfWeek === 0) {
// For Monday-based weeks, Sunday is at the end (day 7)
date.setDate(actualWeekStart.getDate() + 7);
if (dayOfWeek === 0) {
// Sunday is 6 days after Monday
date.setDate(monday.getDate() + 6);
} else {
// Normal calculation
date.setDate(actualWeekStart.getDate() + dayOfWeek);
// Monday=1 becomes 0 days after, Tuesday=2 becomes 1 day after, etc.
date.setDate(monday.getDate() + dayOfWeek - 1);
}
dates.push(date);
@ -45,26 +43,27 @@ export class DateCalculator {
}
/**
* Get the start of the week for a given date
* Get the start of the ISO week (Monday) for a given date
* @param date - Any date in the week
* @returns The Monday of the ISO week
*/
getISOWeekStart(date: Date): Date {
const monday = new Date(date);
const currentDay = monday.getDay();
const daysToSubtract = currentDay === 0 ? 6 : currentDay - 1;
monday.setDate(monday.getDate() - daysToSubtract);
monday.setHours(0, 0, 0, 0);
return monday;
}
/**
* Get the start of the week for a given date (legacy method)
* @param date - Any date in the week
* @param firstDayOfWeek - 0 for Sunday, 1 for Monday
* @returns The start date of the week
*/
getWeekStart(date: Date, firstDayOfWeek: number = 1): Date {
const weekStart = new Date(date);
const currentDay = weekStart.getDay();
if (firstDayOfWeek === 1) {
// Monday as first day
const daysToSubtract = currentDay === 0 ? 6 : currentDay - 1;
weekStart.setDate(weekStart.getDate() - daysToSubtract);
} else {
// Sunday as first day
weekStart.setDate(weekStart.getDate() - currentDay);
}
weekStart.setHours(0, 0, 0, 0);
return weekStart;
return this.getISOWeekStart(date);
}
/**