build method

String Function(DateTime) build()

Builds the formatter function

Implementation

String Function(DateTime) build() {
  final locale = _locale ?? 'en_US';

  return (DateTime date) {
    final parts = <String>[];

    // Format date
    if (_includeDate) {
      if (_dateFormatStyle == DateFormatStyle.short) {
        parts.add(formatters.DateFormatters.formatDateShort(date, locale));
      } else if (_dateFormatStyle == DateFormatStyle.medium) {
        parts.add(formatters.DateFormatters.formatDateMedium(date, locale));
      } else if (_dateFormatStyle == DateFormatStyle.long) {
        parts.add(formatters.DateFormatters.formatDateLong(date, locale));
      } else if (_dateFormatStyle == DateFormatStyle.full) {
        parts.add(formatters.DateFormatters.formatDateFull(date, locale));
      } else if (_dateFormatStyle == DateFormatStyle.custom &&
          _customDatePattern != null) {
        parts.add(
          formatters.DateFormatters.formatCustom(
            date,
            _customDatePattern!,
            locale,
          ),
        );
      } else {
        // Default to medium
        parts.add(formatters.DateFormatters.formatDateMedium(date, locale));
      }
    }

    // Format time
    if (_includeTime && _timeFormatStyle != null) {
      String timeStr;
      switch (_timeFormatStyle!) {
        case TimeFormatStyle.twelveHour:
          timeStr =
              time_formatters.TimeFormatters.formatTime12Hour(date, locale);
          break;
        case TimeFormatStyle.twentyFourHour:
          timeStr =
              time_formatters.TimeFormatters.formatTime24Hour(date, locale);
          break;
        case TimeFormatStyle.twelveHourWithSeconds:
          timeStr =
              time_formatters.TimeFormatters.formatTime12HourWithSeconds(
            date,
            locale,
          );
          break;
        case TimeFormatStyle.twentyFourHourWithSeconds:
          timeStr =
              time_formatters.TimeFormatters.formatTime24HourWithSeconds(
            date,
            locale,
          );
          break;
      }

      if (_includeDate) {
        // Combine date and time
        if (locale.startsWith('es')) {
          parts.add('a las $timeStr');
        } else if (locale.startsWith('fr')) {
          parts.add('à $timeStr');
        } else if (locale.startsWith('de')) {
          parts.add('um $timeStr');
        } else {
          parts.add('at $timeStr');
        }
      } else {
        parts.add(timeStr);
      }
    }

    return parts.join(' ');
  };
}