isInTimes function
Check a file time value within a specified range min, max.
DateTime now = DateTime.now();
DateTime yesterday = now.subtract(Duration(days: 1));
DateTime tomorrow = now.add(Duration(days: 1));
print(isInTimes(now)); // false (no range defined)
print(isInTimes(now, min: yesterday, max: tomorrow)); // true (within range)
print(isInTimes(yesterday, min: yesterday, max: tomorrow)); // true
print(isInTimes(tomorrow, min: yesterday, max: tomorrow)); // true
print(isInTimes(yesterday, min: now, max: tomorrow)); // false
print(isInTimes(now, min: yesterday)); // true (after or equal to min)
print(isInTimes(yesterday, min: now)); // false
print(isInTimes(now, max: tomorrow)); // true (before or equal to max)
print(isInTimes(tomorrow, max: tomorrow)); // true
print(isInTimes(tomorrow, max: yesterday)); // false
Implementation
bool isInTimes(DateTime value, {DateTime? min, DateTime? max}) {
// Condition 1: No range defined
if (min == null && max == null) return false;
// Condition 2: Within range
if (min != null && max != null) {
return value.isAfter(min) && value.isBefore(max) ||
value == min ||
value == max;
}
// Condition 3: After or equal to min
if (min != null) return value.isAfter(min) || value == min;
// Condition 4: Before or equal to max
return value.isBefore(max!) || value == max; // max != null
}