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 _locTimeMs = locTimeMs ?? DateTime.now().millisecondsSinceEpoch;
  String _locale = locale ?? 'en';
  TimelineInfo _info = _timelineInfoMap[_locale] ?? EnInfo();
  DayFormat _dayFormat = dayFormat ?? DayFormat.Common;

  int elapsed = _locTimeMs - ms;
  String suffix;
  if (elapsed < 0) {
    suffix = _info.suffixAfter();
    // suffix after is empty. user just now.
    if (suffix.isNotEmpty) {
      elapsed = elapsed.abs();
      _dayFormat = DayFormat.Simple;
    } else {
      return _info.lessThanOneMinute();
    }
  } else {
    suffix = _info.suffixAgo();
  }

  String timeline;
  if (_info.customYesterday().isNotEmpty &&
      DateUtil.isYesterdayByMs(ms, _locTimeMs)) {
    return _getYesterday(ms, _info, _dayFormat);
  }

  if (!DateUtil.yearIsEqualByMs(ms, _locTimeMs)) {
    timeline = _getYear(ms, _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 < 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)) {
      _dayFormat = DayFormat.Simple;
    }
    timeline = _formatDays(ms, days.round(), _info, _dayFormat);
    suffix = (_dayFormat == DayFormat.Simple ? suffix : '');
  }
  return timeline + suffix;
}