Week.forDate constructor
Week.forDate(
- DateTime date
Implementation
factory Week.forDate(DateTime date) {
assert(date.debugCheckIsValidTimetableDate());
// Algorithm from https://en.wikipedia.org/wiki/ISO_week_date#Calculating_the_week_number_from_a_month_and_day_of_the_month_or_ordinal_date
final year = date.year;
final weekOfYear = (date.dayOfYear + 10 - date.weekday) ~/ 7;
if (weekOfYear == 0) {
// If the week number thus obtained equals 0, it means that the given date
// belongs to the preceding (week-based) year.
final weekOfYear =
DateTimeTimetable.date(year - 1, 12, 31).week.weekOfYear;
return Week(year - 1, weekOfYear);
}
if (weekOfYear == 53 &&
DateTime(year, 12, 31).weekday < DateTime.thursday) {
// If a week number of 53 is obtained, one must check that the date is not
// actually in week 1 of the following year.
return Week(year + 1, 1);
}
return Week(year, weekOfYear);
}