calculateTimeDifference method

TimeDifference calculateTimeDifference()

Calculates the difference in time and returns a TimeDifference.

Implementation

TimeDifference calculateTimeDifference() {
  // If the closest time unit is second
  if ((differenceInMsAbs < MS_PER_MINUTE)) {
    return TimeDifference(
      value: (differenceInMs / MS_PER_SECOND).floor(),
      unit: LitTimeUnit.second,
    );
  }
  // If the closest time unit is minute
  if ((differenceInMsAbs < MS_PER_HOUR)) {
    return TimeDifference(
      value: (differenceInMs / MS_PER_MINUTE).floor(),
      unit: LitTimeUnit.minute,
    );
  }
  // If the closest time unit is hour
  if ((differenceInMsAbs < MS_PER_DAY)) {
    return TimeDifference(
      value: (differenceInMs / MS_PER_HOUR).floor(),
      unit: LitTimeUnit.hour,
    );
  }
  // If the closest time unit is day
  if ((differenceInMsAbs < MS_PER_YEAR)) {
    return TimeDifference(
      value: (differenceInMs / MS_PER_DAY).floor(),
      unit: LitTimeUnit.day,
    );
  }
  // Else the closest time unit is year
  return TimeDifference(
    value: (differenceInMs / MS_PER_YEAR).floor(),
    unit: LitTimeUnit.year,
  );
}