previous method
Returns the previous date that fits the dayInYear.
- If the current
date- DayInYear.dayInYear is greater than the dayInYear, it's returned the same year with the DayInYear.dayInYear being the dayInYear. - If the current
date- DayInYear.dayInYear is less than the dayInYear, it's returned the previous year with the DayInYear.dayInYear being the dayInYear.
Implementation
@override
DateTime previous(DateTime date) {
final thisYearDay = date.firstDayOfYear
.add(Duration(days: dayInYear - 1))
.clamp(max: date.lastDayOfYear);
if (date.dayInYear > dayInYear) return thisYearDay;
return date
.copyWith(year: date.year - 1)
.firstDayOfYear
.add(Duration(days: dayInYear - 1))
.clamp(max: date.copyWith(year: date.year - 1).lastDayOfYear);
}