formatTimeOfDay function

String formatTimeOfDay(
  1. TimeOfDay timeOfDay, {
  2. bool alwaysUse24HourFormat = false,
})

Implementation

String formatTimeOfDay(TimeOfDay timeOfDay,
    {bool alwaysUse24HourFormat = false}) {
  // Not using intl.DateFormat for two reasons:
  //
  // - DateFormat supports more formats than our material time picker does,
  //   and we want to be consistent across time picker format and the string
  //   formatting of the time of day.
  // - DateFormat operates on DateTime, which is sensitive to time eras and
  //   time zones, while here we want to format hour and minute within one day
  //   no matter what date the day falls on.
  final StringBuffer buffer = StringBuffer();

  // Add hour:minute.
  buffer
    ..write(formatHour(timeOfDay, alwaysUse24HourFormat: alwaysUse24HourFormat))
    ..write(':')
    ..write(formatMinute(timeOfDay));

  if (alwaysUse24HourFormat) {
    // There's no AM/PM indicator in 24-hour format.
    return '$buffer';
  }

  // Add AM/PM indicator.
  buffer..write(' ')..write(_formatDayPeriod(timeOfDay));
  return '$buffer';
}