alignDateTime function Null safety

DateTime alignDateTime(
  1. DateTime dt,
  2. Duration alignment,
  3. [bool roundUp = false]
)

Rounds down or up a DateTime object using a Duration object. If roundUp is true, the result is rounded up, otherwise it's rounded down. If the duration is a multiple of days, the result will be aligned at the day mark in the timezone of the source datetime.

Implementation

DateTime alignDateTime(
  DateTime dt,
  Duration alignment, [
  bool roundUp = false,
]) {
  assert(alignment >= Duration.zero);
  if (alignment == Duration.zero) return dt;
  final correction = Duration(
      days: 0,
      hours: alignment.inDays > 0
          ? dt.hour
          : alignment.inHours > 0
              ? dt.hour % alignment.inHours
              : 0,
      minutes: alignment.inHours > 0
          ? dt.minute
          : alignment.inMinutes > 0
              ? dt.minute % alignment.inMinutes
              : 0,
      seconds: alignment.inMinutes > 0
          ? dt.second
          : alignment.inSeconds > 0
              ? dt.second % alignment.inSeconds
              : 0,
      milliseconds: alignment.inSeconds > 0
          ? dt.millisecond
          : alignment.inMilliseconds > 0
              ? dt.millisecond % alignment.inMilliseconds
              : 0,
      microseconds: alignment.inMilliseconds > 0 ? dt.microsecond : 0);
  if (correction == Duration.zero) return dt;
  final corrected = dt.subtract(correction);
  final result = roundUp ? corrected.add(alignment) : corrected;
  return result;
}