calendar method
Calendar string
Implementation
String calendar(
Moment moment, {
DateTime? reference,
String? customFormat,
bool omitHours = false,
bool omitHoursIfDistant = true,
}) {
// After this check, we can use `_calendarData` for non-null data
if (calendarData == null) {
throw MomentException(
"Moment.calendar() for this localization $locale ($endonym) must be overriden since [calendarData] is null");
}
reference ??= Moment.nowWithTimezone(moment.isUtc);
late final String dateString;
final int deltaDays = moment.differenceInDays(reference);
final String? deltaDaysName = _calendarData.relativeDayNames[deltaDays];
if (deltaDaysName != null) {
dateString = deltaDaysName;
} else {
/// If it occured before [reference]
if (moment < reference) {
final DateTime weekBefore = reference.subtract(const Duration(days: 7));
if (moment.isBefore(weekBefore)) {
if (omitHoursIfDistant) {
omitHours = true;
}
dateString =
moment.format(customFormat ?? localizationDefaultDateFormat);
} else {
dateString =
_calendarData.keywords.lastWeekday(weekdayName[moment.weekday]!);
}
}
/// If it occured after [reference]
else {
final DateTime weekAfter = reference.add(const Duration(days: 7));
/// If it's this or next week (relative to the reference)
if (moment.isBefore(weekAfter)) {
dateString = weekdayName[moment.weekday]!;
} else {
if (omitHoursIfDistant) {
omitHours = true;
}
dateString =
moment.format(customFormat ?? localizationDefaultDateFormat);
}
}
}
if (customFormat != null) return dateString;
if (omitHours) {
return dateString;
}
return _calendarData.keywords.at(
dateString,
calendarTime(moment),
);
}