timeAgoForChat method

  1. @override
String timeAgoForChat(
  1. DateTime? dateTime, {
  2. Locale? locale,
})
override

Implementation

@override
String timeAgoForChat(DateTime? dateTime, {Locale? locale}) {
  if (dateTime == null) return '';

  String languageCode = locale?.languageCode ?? 'zh';
  final nowTime = DateTime.now();
  if (nowTime.year != dateTime.year) {
    return DateUtil.formatDate(dateTime,
        format:
            languageCode == 'zh' ? 'yyyy年MM月dd HH:mm' : 'yyyy-MM-dd HH:mm');
  } else if (nowTime.month != dateTime.month) {
    return DateUtil.formatDate(dateTime,
        format: languageCode == 'zh' ? 'M月dd HH:mm' : 'M-dd HH:mm');
  } else if (nowTime.day != dateTime.day) {
    if (DateUtil.isYesterday(dateTime, nowTime)) {
      return languageCode == 'zh' ? '昨天' : 'Yesterday';
    }
    if (DateUtil.isWeek(dateTime.millisecondsSinceEpoch)) {
      final weekday = weekDay(dateTime, locale: locale);
      return '$weekday ${DateUtil.formatDate(dateTime, format: 'HH:mm')}';
    }
    return DateUtil.formatDate(dateTime,
        format: languageCode == 'zh' ? 'M月dd HH:mm' : 'M-dd HH:mm');
  } else {
    final threeMinutesAgo = nowTime.subtract(const Duration(minutes: 3));
    if (dateTime.isAfter(threeMinutesAgo) && dateTime.isBefore(nowTime)) {
      return languageCode == 'zh' ? '刚刚' : 'just now';
    } else {
      return DateUtil.formatDate(dateTime, format: 'HH:mm');
    }
  }
}