toHumanizedString method

String toHumanizedString()

Converts the Duration into a human-friendly string in the format:

  • HH:MM:SS if hours are present
  • MM:SS if no hours

Minutes and seconds are zero-padded if necessary.

Implementation

String toHumanizedString() {
  final seconds = '${inSeconds % 60}'.padLeft(2, '0');
  String minutes = '${inMinutes % 60}';
  if (inHours > 0 || inMinutes == 0) {
    minutes = minutes.padLeft(2, '0');
  }
  String value = '$minutes:$seconds';
  if (inHours > 0) {
    value = '$inHours:$minutes:$seconds';
  }
  return value;
}