format static method

String format(
  1. int timeMillis, {
  2. int? locTimeMillis,
  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 timeMillis,
    {int? locTimeMillis, String? locale, DayFormat? dayFormat}) {
  int _locTimeMillis = locTimeMillis ?? DateTime.now().millisecondsSinceEpoch;
  String _locale = locale ?? 'zh';
  TimelineInfo _info = _timelineInfoMap[_locale] ?? ZhInfo();
  DayFormat _dayFormat = dayFormat ?? DayFormat.Common;

  int elapsed = _locTimeMillis - timeMillis;
  String suffix;
  if (elapsed < 0) {
    elapsed = elapsed.abs();
    suffix = _info.suffixAfter();
    _dayFormat = DayFormat.Simple;
  } else {
    suffix = _info.suffixAgo();
  }

  String? timeline;
  if (_info.customYesterday().isNotEmpty &&
      TimeUtils.isYesterday(
        TimeUtils.getDateTime(milliseconds: timeMillis),
        TimeUtils.getDateTime(milliseconds: _locTimeMillis),
      )) {
    return _getYesterday(timeMillis, _info, _dayFormat);
  }

  if (!TimeUtils.yearIsEqual(
    TimeUtils.getDateTime(milliseconds: timeMillis),
    TimeUtils.getDateTime(milliseconds: _locTimeMillis),
  )) {
    timeline = _getYear(timeMillis, _dayFormat);
    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 < 120) {
    timeline = _info.oneMinute(1);
    if (suffix != _info.suffixAfter() &&
        _info.lessThanTenSecond().isNotEmpty &&
        seconds < 10) {
      timeline = _info.lessThanTenSecond();
      suffix = "";
    }
  } else if (minutes < 60) {
    timeline = _info.minutes(minutes.round());
  } else if (hours < 24) {
    timeline = _info.hours(hours.round());
  } else {
    if ((days.round() == 1 && _info.keepOneDay() == true) ||
        (days.round() == 2 && _info.keepTwoDays() == true)) {
      _dayFormat = DayFormat.Simple;
    }
    timeline = _formatDays(timeMillis, days.round(), _info, _dayFormat);
    suffix = (_dayFormat == DayFormat.Simple ? suffix : "");
  }
  return (timeline as String) + suffix;
}