getDaysSpanned method
Calculates the number of days the event spans within a given date range.
Implementation
int getDaysSpanned(DateTime rangeStart, DateTime rangeEnd) {
// Helper to normalize a date to midnight UTC.
DateTime normalize(DateTime dt) => DateTime.utc(dt.year, dt.month, dt.day);
final effectiveStart =
startDate.isAfter(rangeStart) ? startDate : rangeStart;
var effectiveEnd = endDate.isBefore(rangeEnd) ? endDate : rangeEnd;
// If an event's end time is exactly midnight, it concludes at the very beginning of that day,
// meaning it doesn't occupy that day's calendar slot. So we calculate the difference directly.
// The exception is a single-day event, which should always have a span of at least 1.
if (!isSameDay(effectiveStart, effectiveEnd) &&
effectiveEnd.hour == 0 &&
effectiveEnd.minute == 0 &&
effectiveEnd.second == 0 &&
effectiveEnd.millisecond == 0 &&
effectiveEnd.microsecond == 0) {
return normalize(effectiveEnd)
.difference(normalize(effectiveStart))
.inDays;
}
return normalize(effectiveEnd)
.difference(normalize(effectiveStart))
.inDays +
1;
}