format static method

String format(
  1. int ms, {
  2. int? locTimeMs,
  3. String? locale,
  4. DayFormat? dayFormat,
})

format time by millis. dateTime : millis. locDateTime: current time or schedule time. millis. locale: output key.

Implementation

static String format(
  int ms, {
  int? locTimeMs,
  String? locale,
  DayFormat? dayFormat,
}) {
  int locTimeMs0 = locTimeMs ?? DateTime.now().millisecondsSinceEpoch;
  String locale0 = locale ?? 'en';
  TimelineInfo info = _timelineInfoMap[locale0] ?? EnInfo();
  DayFormat dayFormat0 = dayFormat ?? DayFormat.common;

  int elapsed = locTimeMs0 - ms;
  String suffix;
  if (elapsed < 0) {
    suffix = info.suffixAfter();
    // suffix after is empty. user just now.
    if (suffix.isNotEmpty) {
      elapsed = elapsed.abs();
      dayFormat0 = DayFormat.simple;
    } else {
      return info.lessThanOneMinute();
    }
  } else {
    suffix = info.suffixAgo();
  }

  String timeline;
  if (info.customYesterday().isNotEmpty &&
      DateUtil.isYesterdayByMs(ms, locTimeMs0)) {
    return _getYesterday(ms, info, dayFormat0);
  }

  if (!DateUtil.yearIsEqualByMs(ms, locTimeMs0)) {
    timeline = _getYear(ms, dayFormat0);
    if (timeline.isNotEmpty) return timeline;
  }

  final num seconds = elapsed / 1000;
  final num minutes = seconds / 60;
  final num hours = minutes / 60;
  final num days = hours / 24;

  if (seconds < 90) {
    timeline = info.oneMinute(1);
    if (suffix != info.suffixAfter() &&
        info.lessThanOneMinute().isNotEmpty &&
        seconds < info.maxJustNowSecond()) {
      timeline = info.lessThanOneMinute();
      suffix = '';
    }
  } else if (minutes < 60) {
    timeline = info.minutes(minutes.round());
  } else if (minutes < 90) {
    timeline = info.anHour(1);
  } else if (hours < 24) {
    timeline = info.hours(hours.round());
  } else {
    if ((days.round() == 1 && info.keepOneDay() == true) ||
        (days.round() == 2 && info.keepTwoDays() == true)) {
      dayFormat0 = DayFormat.simple;
    }
    timeline = _formatDays(ms, days.round(), info, dayFormat0);
    suffix = (dayFormat0 == DayFormat.simple ? suffix : '');
  }
  return timeline + suffix;
}