format static method

String format(
  1. int hour, [
  2. int minute = 0,
  3. int second = 0,
  4. int millisecond = 0,
  5. int microsecond = 0,
])

Formats the time as a ISO-8601 time.

Times.format(20, 30, 40, 5, 6); // 20:30:40.005006

Implementation

static String format(int hour, [int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0]) {
  final hours = hour.toString().padLeft(2, '0');
  final minutes = minute.toString().padLeft(2, '0');

  final seconds = second == 0 && millisecond == 0 && microsecond == 0 ? '' : ':${second.toString().padLeft(2, '0')}';
  final milliseconds = millisecond == 0 && microsecond == 0 ? '' : '.${millisecond.toString().padLeft(3, '0')}';
  final microseconds = microsecond == 0 ? '' :  microsecond.toString().padLeft(3, '0');

  return '$hours:$minutes$seconds$milliseconds$microseconds';
}