alignedStart method

DateTime alignedStart(
  1. DateTime now
)

Calculates the aligned start of the current period based on now.

This method returns the start of the period that the given now falls into. For example, if the period is daily, it returns the start of the current day.

Implementation

DateTime alignedStart(DateTime now) {
  switch (this) {
    case TrackerPeriod.daily:
      return DateTime(now.year, now.month, now.day);
    case TrackerPeriod.weekly:
      final monday = now.subtract(Duration(days: now.weekday - 1));
      return DateTime(monday.year, monday.month, monday.day);
    case TrackerPeriod.monthly:
      return DateTime(now.year, now.month);
    default:
      final seconds = duration.inSeconds;
      final epochSeconds = now.toUtc().millisecondsSinceEpoch ~/ 1000;
      final alignedEpoch = (epochSeconds ~/ seconds) * seconds;
      return DateTime.fromMillisecondsSinceEpoch(alignedEpoch * 1000,
              isUtc: true)
          .toLocal();
  }
}