toDuration method

Duration toDuration([
  1. DateTime? reference
])

Implementation

Duration toDuration([DateTime? reference]) {
  int days = (7 * this.weeks) + this.days;
  reference ??= DateTime.now();

  if (this.months > 0) {
    final int numMonths = this.months.toInt() + reference.month.toInt();
    final DateTime monthsAdjusted = cloneDate(reference, month: numMonths);
    days += monthsAdjusted.difference(reference).inDays;
  }

  if (this.years > 0) {
    final int numYears = reference.year + this.years;
    final DateTime adjusted = cloneDate(reference, year: numYears);
    days += adjusted.difference(reference).inDays;
  }

  return Duration(
    days: days,
    minutes: minutes,
    hours: hours,
    seconds: seconds,
    milliseconds: millis,
    microseconds: micros,
  );
}