When implementing algorithms involving date and time calculations, maintain precise control over time components to avoid subtle bugs: 1. **Reset unwanted time components** - Always explicitly set seconds and milliseconds to zero when you only care about hours and minutes
When implementing algorithms involving date and time calculations, maintain precise control over time components to avoid subtle bugs:
// ❌ PROBLEMATIC: Implicit time components cause non-deterministic behavior
const targetTime = setHours(setMinutes(now, 0), 11); // Still has seconds/ms from now
// ✅ CORRECT: Explicitly reset all time components you don't want
const targetTime = setMilliseconds(setSeconds(setMinutes(now, 0), 0), 0) |>
(d => setHours(d, 11));
// ❌ PROBLEMATIC: Rounding can collapse distinct slots
const slotDate = addDays(intervalStart, Math.round(i * slotLength));
// ✅ CORRECT: Preserve fractional precision in intermediate calculations
const wholeDays = Math.floor(i * slotLength);
const slotDate = addDays(intervalStart, wholeDays);
// ❌ PROBLEMATIC: Missing boundary check
if (daysOfWeek & nextDayMask) {
nextDate.setHours(0, 0, 0, 0);
return nextDate; // Could return a time in the past
}
// ✅ CORRECT: Check time boundaries
if (daysOfWeek & nextDayMask) {
nextDate.setHours(0, 0, 0, 0);
if (daysToAdd === 0 && nextDate <= fromDate) {
daysToAdd++;
continue; // Skip to next day
}
return nextDate;
}
Time-related bugs can be subtle and hard to detect. They often manifest only in specific situations (like date boundaries or daylight saving time changes). Implement thorough tests that cover edge cases, and always be explicit about which time components you’re manipulating.
Enter the URL of a public GitHub repository