conversationFormatTime static method

String conversationFormatTime(
  1. int? timestamp, {
  2. DateTime? now,
})

Implementation

static String conversationFormatTime(int? timestamp, {DateTime? now}) {
  if (!hasValidTimestamp(timestamp)) return '';
  final date = DateTime.fromMillisecondsSinceEpoch(timestamp!);
  now = (now ?? DateTime.now()).toLocal();

  // 先判断年份
  if (date.year != now.year) {
    return '${date.year}年${date.month}月${date.day}日';
  }

  final differenceInDays = _calendarDayDifference(now, date);
  if (differenceInDays == 0) {
    return '${date.hour}:${date.minute.toString().padLeft(2, '0')}';
  } else if (differenceInDays == 1) {
    return '昨天';
  } else if (differenceInDays > 1 && differenceInDays < 7) {
    const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    return weekdays[date.weekday % 7];
  } else {
    return '${date.month}月${date.day}日';
  }
}