alignTimestamp method

DateTime alignTimestamp(
  1. DateTime dateTime
)

Aligns a given dateTime down to the nearest start boundary for this timeframe.

Implementation

DateTime alignTimestamp(DateTime dateTime) {
  if (this == oneDay) {
    return DateTime(dateTime.year, dateTime.month, dateTime.day);
  } else if (this == oneWeek) {
    final daysToSubtract = (dateTime.weekday - DateTime.monday) % 7;
    final monday = dateTime.subtract(Duration(days: daysToSubtract));
    return DateTime(monday.year, monday.month, monday.day);
  } else if (this == fourHours) {
    final roundedHour = (dateTime.hour ~/ 4) * 4;
    return DateTime(dateTime.year, dateTime.month, dateTime.day, roundedHour);
  } else if (this == oneHour) {
    return DateTime(
      dateTime.year,
      dateTime.month,
      dateTime.day,
      dateTime.hour,
    );
  } else {
    final totalMinutes = dateTime.minute;
    final roundedMinutes =
        (totalMinutes ~/ duration.inMinutes) * duration.inMinutes;
    return DateTime(
      dateTime.year,
      dateTime.month,
      dateTime.day,
      dateTime.hour,
      roundedMinutes,
    );
  }
}