formatTimeMillis function

String formatTimeMillis(
  1. int? time
)

Implementation

String formatTimeMillis(int? time) {
  if (time == null || time == 0) return '0';

  var sig = '';

  if (time < 0) {
    sig = '-';
    time = -time;
  }

  if (time < oneSecond) {
    return '$sig$time ms';
  } else if (time < oneMinute) {
    var t = time / oneSecond;
    var f = formatDecimal(t);
    return '$sig$f sec';
  } else if (time < oneHour) {
    var t = time / oneMinute;

    var min = t.toInt();
    var sec = ((t - min) * 60).toInt();

    return sec > 0 ? '$sig$min min $sec s' : ('$sig$min min');
  } else if (time < oneDay) {
    var t = time / oneHour;

    var hour = t.toInt();
    var min = ((t - hour) * 60).toInt();

    return min > 0 ? '$sig$hour h $min min' : '$sig$hour h';
  } else {
    var t = time / oneDay;

    var day = t.toInt();
    var hour = ((t - day) * 24).toInt();

    return hour > 0 ? '$sig$day d $hour h' : '$sig$day d';
  }
}