normalizeIntervalTime function

DateTime normalizeIntervalTime({
  1. required DateTime time,
  2. required DateInterval interval,
  3. ReturnedInterval returnedInterval = ReturnedInterval.before,
  4. bool atSameMoment = false,
})

Normalizes time stamp

Implementation

DateTime normalizeIntervalTime({
  required DateTime time,
  required DateInterval interval,
  ReturnedInterval returnedInterval = ReturnedInterval.before,
  bool atSameMoment = false,
}) {
  DateTime returnTime = time.copyWith(millisecond: 0, microsecond: 0);
  if (interval.months != 0) {
    returnTime =
        returnTime.copyWith(month: 1, day: 1, hour: 0, minute: 0, second: 0);
  } else if (interval.days != 0) {
    returnTime = returnTime.copyWith(day: 1, hour: 0, minute: 0, second: 0);
  } else if (interval.hours != 0) {
    returnTime = returnTime.copyWith(hour: 0, minute: 0, second: 0);
  } else if (interval.minutes != 0) {
    returnTime = returnTime.copyWith(minute: 0, second: 0);
  } else if (interval.seconds != 0) {
    returnTime = returnTime.copyWith(second: 0);
  }
  while (true) {
    final newTime = returnTime.copyWith(
      month: returnTime.month + interval.months,
      day: returnTime.day + interval.days,
      hour: returnTime.hour + interval.hours,
      minute: returnTime.minute + interval.minutes,
      second: returnTime.second + interval.seconds,
    );
    if (((atSameMoment && newTime.isAtSameMomentAs(time)) ||
            (!atSameMoment && !newTime.isAtSameMomentAs(time))) &&
        newTime.isAfter(time)) {
      if (returnedInterval == ReturnedInterval.before) {
        return returnTime;
      } else {
        return newTime;
      }
    }
    returnTime = newTime.copyWith();
  }
}