normalizeTime function

TimeOfDay normalizeTime(
  1. TimeOfDay time, {
  2. int minuteInterval = 1,
})

Snaps time to the nearest minuteInterval.

A value within half a step of the next midnight rolls to 00:00.

Implementation

TimeOfDay normalizeTime(TimeOfDay time, {int minuteInterval = 1}) {
  if (minuteInterval <= 0 || 60 % minuteInterval != 0) {
    throw ArgumentError.value(
      minuteInterval,
      'minuteInterval',
      'Must be a positive divisor of 60.',
    );
  }
  final total = time.hour * 60 + time.minute;
  final snapped = (total / minuteInterval).round() * minuteInterval;
  final wrapped = snapped % (24 * 60);
  return TimeOfDay(hour: wrapped ~/ 60, minute: wrapped % 60);
}