Refactors DateCalculator to be a static class
This change refactors the DateCalculator class to be a static class. This removes the need to instantiate DateCalculator in multiple managers and renderers, simplifying dependency management and ensuring consistent date calculations across the application. The configuration is now initialized once at the application start.
This commit is contained in:
parent
0da875a224
commit
b8b44ddae8
11 changed files with 107 additions and 83 deletions
|
|
@ -6,10 +6,14 @@
|
|||
import { CalendarConfig } from '../core/CalendarConfig';
|
||||
|
||||
export class DateCalculator {
|
||||
private config: CalendarConfig;
|
||||
private static config: CalendarConfig;
|
||||
|
||||
constructor(config: CalendarConfig) {
|
||||
this.config = config;
|
||||
/**
|
||||
* Initialize DateCalculator with configuration
|
||||
* @param config - Calendar configuration
|
||||
*/
|
||||
static initialize(config: CalendarConfig): void {
|
||||
DateCalculator.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -18,7 +22,7 @@ export class DateCalculator {
|
|||
* @param methodName - Name of calling method for error messages
|
||||
* @throws Error if date is invalid
|
||||
*/
|
||||
private validateDate(date: Date, methodName: string): void {
|
||||
private static validateDate(date: Date, methodName: string): void {
|
||||
if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
|
||||
throw new Error(`${methodName}: Invalid date provided - ${date}`);
|
||||
}
|
||||
|
|
@ -29,14 +33,14 @@ export class DateCalculator {
|
|||
* @param weekStart - Any date in the week
|
||||
* @returns Array of dates for the configured work days
|
||||
*/
|
||||
getWorkWeekDates(weekStart: Date): Date[] {
|
||||
this.validateDate(weekStart, 'getWorkWeekDates');
|
||||
static getWorkWeekDates(weekStart: Date): Date[] {
|
||||
DateCalculator.validateDate(weekStart, 'getWorkWeekDates');
|
||||
|
||||
const dates: Date[] = [];
|
||||
const workWeekSettings = this.config.getWorkWeekSettings();
|
||||
const workWeekSettings = DateCalculator.config.getWorkWeekSettings();
|
||||
|
||||
// Always use ISO week start (Monday)
|
||||
const mondayOfWeek = this.getISOWeekStart(weekStart);
|
||||
const mondayOfWeek = DateCalculator.getISOWeekStart(weekStart);
|
||||
|
||||
// Calculate dates for each work day using ISO numbering
|
||||
workWeekSettings.workDays.forEach(isoDay => {
|
||||
|
|
@ -55,8 +59,8 @@ export class DateCalculator {
|
|||
* @param date - Any date in the week
|
||||
* @returns The Monday of the ISO week
|
||||
*/
|
||||
getISOWeekStart(date: Date): Date {
|
||||
this.validateDate(date, 'getISOWeekStart');
|
||||
static getISOWeekStart(date: Date): Date {
|
||||
DateCalculator.validateDate(date, 'getISOWeekStart');
|
||||
|
||||
const monday = new Date(date);
|
||||
const currentDay = monday.getDay();
|
||||
|
|
@ -72,10 +76,10 @@ export class DateCalculator {
|
|||
* @param date - Any date in the week
|
||||
* @returns The end date of the ISO week (Sunday)
|
||||
*/
|
||||
getWeekEnd(date: Date): Date {
|
||||
this.validateDate(date, 'getWeekEnd');
|
||||
static getWeekEnd(date: Date): Date {
|
||||
DateCalculator.validateDate(date, 'getWeekEnd');
|
||||
|
||||
const weekStart = this.getISOWeekStart(date);
|
||||
const weekStart = DateCalculator.getISOWeekStart(date);
|
||||
const weekEnd = new Date(weekStart);
|
||||
weekEnd.setDate(weekStart.getDate() + 6);
|
||||
weekEnd.setHours(23, 59, 59, 999);
|
||||
|
|
@ -87,7 +91,7 @@ export class DateCalculator {
|
|||
* @param date - The date to get week number for
|
||||
* @returns Week number (1-53)
|
||||
*/
|
||||
getWeekNumber(date: Date): number {
|
||||
static getWeekNumber(date: Date): number {
|
||||
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
||||
const dayNum = d.getUTCDay() || 7;
|
||||
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
|
||||
|
|
@ -102,7 +106,7 @@ export class DateCalculator {
|
|||
* @param options - Formatting options
|
||||
* @returns Formatted date range string
|
||||
*/
|
||||
formatDateRange(
|
||||
static formatDateRange(
|
||||
start: Date,
|
||||
end: Date,
|
||||
options: {
|
||||
|
|
@ -137,7 +141,7 @@ export class DateCalculator {
|
|||
* @param date - Date to format
|
||||
* @returns ISO date string
|
||||
*/
|
||||
formatISODate(date: Date): string {
|
||||
static formatISODate(date: Date): string {
|
||||
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
|
|
@ -146,7 +150,7 @@ export class DateCalculator {
|
|||
* @param date - Date to check
|
||||
* @returns True if the date is today
|
||||
*/
|
||||
isToday(date: Date): boolean {
|
||||
static isToday(date: Date): boolean {
|
||||
const today = new Date();
|
||||
return date.toDateString() === today.toDateString();
|
||||
}
|
||||
|
|
@ -157,7 +161,7 @@ export class DateCalculator {
|
|||
* @param days - Number of days to add (can be negative)
|
||||
* @returns New date
|
||||
*/
|
||||
addDays(date: Date, days: number): Date {
|
||||
static addDays(date: Date, days: number): Date {
|
||||
const result = new Date(date);
|
||||
result.setDate(result.getDate() + days);
|
||||
return result;
|
||||
|
|
@ -169,8 +173,8 @@ export class DateCalculator {
|
|||
* @param weeks - Number of weeks to add (can be negative)
|
||||
* @returns New date
|
||||
*/
|
||||
addWeeks(date: Date, weeks: number): Date {
|
||||
return this.addDays(date, weeks * 7);
|
||||
static addWeeks(date: Date, weeks: number): Date {
|
||||
return DateCalculator.addDays(date, weeks * 7);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -178,10 +182,10 @@ export class DateCalculator {
|
|||
* @param weekStart - Start of the week
|
||||
* @returns Array of 7 dates for the full week
|
||||
*/
|
||||
getFullWeekDates(weekStart: Date): Date[] {
|
||||
static getFullWeekDates(weekStart: Date): Date[] {
|
||||
const dates: Date[] = [];
|
||||
for (let i = 0; i < 7; i++) {
|
||||
dates.push(this.addDays(weekStart, i));
|
||||
dates.push(DateCalculator.addDays(weekStart, i));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
|
@ -192,7 +196,7 @@ export class DateCalculator {
|
|||
* @param format - 'short' or 'long'
|
||||
* @returns Day name
|
||||
*/
|
||||
getDayName(date: Date, format: 'short' | 'long' = 'short'): string {
|
||||
static getDayName(date: Date, format: 'short' | 'long' = 'short'): string {
|
||||
const formatter = new Intl.DateTimeFormat('en-US', {
|
||||
weekday: format
|
||||
});
|
||||
|
|
@ -204,7 +208,7 @@ export class DateCalculator {
|
|||
* @param date - Date to format
|
||||
* @returns Time string
|
||||
*/
|
||||
formatTime(date: Date): string {
|
||||
static formatTime(date: Date): string {
|
||||
return `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
|
|
@ -213,7 +217,7 @@ export class DateCalculator {
|
|||
* @param date - Date to format
|
||||
* @returns 12-hour time string
|
||||
*/
|
||||
formatTime12(date: Date): string {
|
||||
static formatTime12(date: Date): string {
|
||||
const hours = date.getHours();
|
||||
const minutes = date.getMinutes();
|
||||
const period = hours >= 12 ? 'PM' : 'AM';
|
||||
|
|
@ -227,7 +231,7 @@ export class DateCalculator {
|
|||
* @param minutes - Minutes since midnight
|
||||
* @returns Time string
|
||||
*/
|
||||
minutesToTime(minutes: number): string {
|
||||
static minutesToTime(minutes: number): string {
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const mins = minutes % 60;
|
||||
const period = hours >= 12 ? 'PM' : 'AM';
|
||||
|
|
@ -241,7 +245,7 @@ export class DateCalculator {
|
|||
* @param timeStr - Time string
|
||||
* @returns Minutes since midnight
|
||||
*/
|
||||
timeToMinutes(timeStr: string): number {
|
||||
static timeToMinutes(timeStr: string): number {
|
||||
const [time] = timeStr.split('T').pop()!.split('.');
|
||||
const [hours, minutes] = time.split(':').map(Number);
|
||||
return hours * 60 + minutes;
|
||||
|
|
@ -252,7 +256,7 @@ export class DateCalculator {
|
|||
* @param date - Date or ISO string
|
||||
* @returns Minutes since midnight
|
||||
*/
|
||||
getMinutesSinceMidnight(date: Date | string): number {
|
||||
static getMinutesSinceMidnight(date: Date | string): number {
|
||||
const d = typeof date === 'string' ? new Date(date) : date;
|
||||
return d.getHours() * 60 + d.getMinutes();
|
||||
}
|
||||
|
|
@ -263,7 +267,7 @@ export class DateCalculator {
|
|||
* @param end - End date or ISO string
|
||||
* @returns Duration in minutes
|
||||
*/
|
||||
getDurationMinutes(start: Date | string, end: Date | string): number {
|
||||
static getDurationMinutes(start: Date | string, end: Date | string): number {
|
||||
const startDate = typeof start === 'string' ? new Date(start) : start;
|
||||
const endDate = typeof end === 'string' ? new Date(end) : end;
|
||||
return Math.floor((endDate.getTime() - startDate.getTime()) / 60000);
|
||||
|
|
@ -275,7 +279,7 @@ export class DateCalculator {
|
|||
* @param date2 - Second date
|
||||
* @returns True if same day
|
||||
*/
|
||||
isSameDay(date1: Date, date2: Date): boolean {
|
||||
static isSameDay(date1: Date, date2: Date): boolean {
|
||||
return date1.toDateString() === date2.toDateString();
|
||||
}
|
||||
|
||||
|
|
@ -285,15 +289,20 @@ export class DateCalculator {
|
|||
* @param end - End date or ISO string
|
||||
* @returns True if spans multiple days
|
||||
*/
|
||||
isMultiDay(start: Date | string, end: Date | string): boolean {
|
||||
static isMultiDay(start: Date | string, end: Date | string): boolean {
|
||||
const startDate = typeof start === 'string' ? new Date(start) : start;
|
||||
const endDate = typeof end === 'string' ? new Date(end) : end;
|
||||
return !this.isSameDay(startDate, endDate);
|
||||
return !DateCalculator.isSameDay(startDate, endDate);
|
||||
}
|
||||
|
||||
// Legacy constructor for backward compatibility
|
||||
constructor() {
|
||||
// Empty constructor - all methods are now static
|
||||
}
|
||||
}
|
||||
|
||||
// Create singleton instance with config
|
||||
// Legacy factory function - deprecated, use static methods instead
|
||||
export function createDateCalculator(config: CalendarConfig): DateCalculator {
|
||||
return new DateCalculator(config);
|
||||
DateCalculator.initialize(config);
|
||||
return new DateCalculator();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue