firstDayOffset static method
Computes the offset from the first day of the week that the first day of
the month
falls on.
For example, September 1, 2017 falls on a Friday, which in the calendar localized for United States English appears as:
S M T W T F S
_ _ _ _ _ 1 2
The offset for the first day of the months is the number of leading blanks in the calendar, i.e. 5.
The same date localized for the Russian calendar has a different offset, because the first day of week is Monday rather than Sunday:
M T W T F S S
_ _ _ _ 1 2 3
So the offset is 4, rather than 5.
This code consolidates the following:
- DateTime.weekday provides a 1-based index into days of week, with 1 falling on Monday.
- MaterialLocalizations.firstDayOfWeekIndex provides a 0-based index into the MaterialLocalizations.narrowWeekdays list.
- MaterialLocalizations.narrowWeekdays list provides localized names of days of week, always starting with Sunday and ending with Saturday.
Implementation
static int firstDayOffset(
int year, int month, MaterialLocalizations localizations) {
// 0-based day of week for the month and year, with 0 representing Saturday.
final int weekdayFromSaturday =
(Jalali(year, month, 1).weekDay % 7); // Saturday is 0
// 0-based start of week depending on the locale, with 0 representing Sunday.
int firstDayOfWeekIndex = localizations.firstDayOfWeekIndex;
// Adjust firstDayOfWeekIndex to be Saturday-based for comparison.
firstDayOfWeekIndex =
(firstDayOfWeekIndex - 6) % 7; // Saturday as 0, adjust accordingly
// Compute the offset between the localized first day of the week and the first day of the month.
return (weekdayFromSaturday - firstDayOfWeekIndex) % 7;
}