Skip to content

Commit aad6885

Browse files
committed
refactor: Update date utilities to use UTC methods consistently
Update remaining date helper functions to use UTC methods for consistency with the timezone bug fixes. This ensures all date comparisons and calculations work consistently across timezones. Changes: - Convert isSameDay() to use UTC date methods - Update legacy recurrence date comparisons to use UTC - Fix date arithmetic in default date generation - Update date iteration loops to use UTC methods These changes complement the timezone fix in Issue #314 by ensuring all date utilities work consistently with UTC dates.
1 parent f795db4 commit aad6885

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/utils/helpers.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ export function calculateDefaultDate(defaultOption: 'none' | 'today' | 'tomorrow
184184
break;
185185
case 'tomorrow':
186186
targetDate = new Date(today);
187-
targetDate.setDate(today.getDate() + 1);
187+
targetDate.setUTCDate(today.getUTCDate() + 1);
188188
break;
189189
case 'next-week':
190190
targetDate = new Date(today);
191-
targetDate.setDate(today.getDate() + 7);
191+
targetDate.setUTCDate(today.getUTCDate() + 7);
192192
break;
193193
default:
194194
return '';
@@ -198,12 +198,12 @@ export function calculateDefaultDate(defaultOption: 'none' | 'today' | 'tomorrow
198198
}
199199

200200
/**
201-
* Checks if two dates are the same day
201+
* Checks if two dates are the same day using UTC methods for consistency
202202
*/
203203
export function isSameDay(date1: Date, date2: Date): boolean {
204-
return date1.getFullYear() === date2.getFullYear() &&
205-
date1.getMonth() === date2.getMonth() &&
206-
date1.getDate() === date2.getDate();
204+
return date1.getUTCFullYear() === date2.getUTCFullYear() &&
205+
date1.getUTCMonth() === date2.getUTCMonth() &&
206+
date1.getUTCDate() === date2.getUTCDate();
207207
}
208208

209209
/**
@@ -354,9 +354,9 @@ export function isDueByRRule(task: TaskInfo, date: Date): boolean {
354354
// Legacy recurrence object handling
355355
const frequency = task.recurrence.frequency;
356356
const targetDate = parseDate(formatUTCDateForCalendar(date));
357-
const dayOfWeek = targetDate.getDay();
358-
const dayOfMonth = targetDate.getDate();
359-
const monthOfYear = targetDate.getMonth() + 1; // JavaScript months are 0-indexed
357+
const dayOfWeek = targetDate.getUTCDay();
358+
const dayOfMonth = targetDate.getUTCDate();
359+
const monthOfYear = targetDate.getUTCMonth() + 1; // JavaScript months are 0-indexed
360360
// Map JavaScript's day of week (0-6, where 0 is Sunday) to our day abbreviations
361361
const weekdayMap = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
362362

@@ -382,8 +382,8 @@ export function isDueByRRule(task: TaskInfo, date: Date): boolean {
382382
else if (task.due) {
383383
try {
384384
const originalDueDate = parseDate(task.due); // Safe parsing
385-
return originalDueDate.getDate() === dayOfMonth &&
386-
originalDueDate.getMonth() === targetDate.getMonth();
385+
return originalDueDate.getUTCDate() === dayOfMonth &&
386+
originalDueDate.getUTCMonth() === targetDate.getUTCMonth();
387387
} catch (error) {
388388
console.error(`Error parsing due date ${task.due}:`, error);
389389
return false;
@@ -408,9 +408,9 @@ export function isRecurringTaskDueOn(task: any, date: Date): boolean {
408408

409409
const frequency = task.recurrence.frequency;
410410
const targetDate = parseDate(formatUTCDateForCalendar(date));
411-
const dayOfWeek = targetDate.getDay();
412-
const dayOfMonth = targetDate.getDate();
413-
const monthOfYear = targetDate.getMonth() + 1; // JavaScript months are 0-indexed
411+
const dayOfWeek = targetDate.getUTCDay();
412+
const dayOfMonth = targetDate.getUTCDate();
413+
const monthOfYear = targetDate.getUTCMonth() + 1; // JavaScript months are 0-indexed
414414
// Map JavaScript's day of week (0-6, where 0 is Sunday) to our day abbreviations
415415
const weekdayMap = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
416416

@@ -436,8 +436,8 @@ export function isRecurringTaskDueOn(task: any, date: Date): boolean {
436436
else if (task.due) {
437437
try {
438438
const originalDueDate = parseDate(task.due); // Safe parsing
439-
return originalDueDate.getDate() === dayOfMonth &&
440-
originalDueDate.getMonth() === targetDate.getMonth();
439+
return originalDueDate.getUTCDate() === dayOfMonth &&
440+
originalDueDate.getUTCMonth() === targetDate.getUTCMonth();
441441
} catch (error) {
442442
console.error(`Error parsing due date ${task.due}:`, error);
443443
return false;
@@ -539,7 +539,7 @@ export function generateRecurringInstances(task: TaskInfo, startDate: Date, endD
539539
if (isDueByRRule(task, current)) {
540540
instances.push(new Date(current));
541541
}
542-
current.setDate(current.getDate() + 1);
542+
current.setUTCDate(current.getUTCDate() + 1);
543543
}
544544

545545
return instances;

0 commit comments

Comments
 (0)