toHumanReadable method

String toHumanReadable()

Implementation

String toHumanReadable() {
  var microseconds = inMicroseconds;
  var sign = (microseconds < 0) ? "-" : "";

  var hours = microseconds ~/ microsecondsPerHour;
  hours = hours.abs();
  microseconds = microseconds.remainder(microsecondsPerHour);

  if (microseconds < 0) microseconds = -microseconds;

  var minutes = microseconds ~/ microsecondsPerMinute;
  microseconds = microseconds.remainder(microsecondsPerMinute);

  var seconds = microseconds ~/ microsecondsPerSecond;
  microseconds = microseconds.remainder(microsecondsPerSecond);

  var milliseconds = microseconds ~/ microsecondsPerMillisecond;
  microseconds = microseconds.remainder(microsecondsPerMillisecond);

  var hasHour = hours > 0;
  var hasMin = minutes > 0;
  var hasSec = seconds > 0;
  var hasMs = milliseconds > 0;

  var l = [
    if (hasHour) "$sign$hours h",
    if (hasMin || (hasHour && (hasSec || hasMs))) "$minutes min",
    if (hasSec || ((hasHour || hasMin) && (hasSec || hasMs))) "$seconds sec",
    if (hasMs) "$milliseconds ms"
  ];

  return l.isEmpty ? '0' : l.join(' ');
}