getTimeStringForChat method

String? getTimeStringForChat(
  1. int timeStamp
)

Implementation

String? getTimeStringForChat(int timeStamp) {
  final DateTime date = DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000);
  final DateTime epochLimit = DateTime.utc(1971);

  if (date.isBefore(epochLimit)) {
    return null;
  }

  final Duration duration = DateTime.now().difference(date);
  final int diffDays = duration.inDays + (duration.inMinutes > DateTime.now().difference(DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day)).inMinutes ? 1 : 0);
  final int diffMinutes = duration.inMinutes;

  var res;

  // 一个礼拜之内
  if (diffDays > 0 && diffDays < 7) {
    if (diffDays <= 2) {
      res = dayMap()[diffDays - 1];
    } else {
      res = weekdayMap()[date.weekday];
    }
  } else if (diffDays >= 7) {
    //当年内
    if (date.year == DateTime.now().year) {
      res = getMonthDate(date);
    } else {
      res = getYearMonthDate(date);
    }
  } else {
    if (diffMinutes > 1) {
      if (diffMinutes < 60) {
        final String option2 = diffMinutes.toString();
        res = TIM_t_para("{{option2}} 分钟前", "$option2 分钟前")(option2: option2);
      } else {
        res = "${date.hour}:${date.minute < 10 ? "0" + date.minute.toString() : date.minute}";
        // res = "$prefix $timeStr";
      }
    } else {
      res = TIM_t("现在");
    }
  }

  return res;
}