toString method

String toString ()

Returns a string representation of this Duration.

Returns a string with hours, minutes, seconds, and microseconds, in the following format: HH:MM:SS.mmmmmm. For example,

var d = new Duration(days:1, hours:1, minutes:33, microseconds: 500);
d.toString();  // "25:33:00.000500"

Implementation

String toString() {
  String sixDigits(int n) {
    if (n >= 100000) return "$n";
    if (n >= 10000) return "0$n";
    if (n >= 1000) return "00$n";
    if (n >= 100) return "000$n";
    if (n >= 10) return "0000$n";
    return "00000$n";
  }

  String twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }

  if (inMicroseconds < 0) {
    return "-${-this}";
  }
  String twoDigitMinutes = twoDigits(inMinutes.remainder(minutesPerHour));
  String twoDigitSeconds = twoDigits(inSeconds.remainder(secondsPerMinute));
  String sixDigitUs =
      sixDigits(inMicroseconds.remainder(microsecondsPerSecond));
  return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs";
}