parseHttpDate function

DateTime parseHttpDate(
  1. String date
)

Parses an HTTP-formatted date into a UTC DateTime.

This follows RFC 2616. It will throw a FormatException if date is invalid.

Implementation

DateTime parseHttpDate(String date) =>
    wrapFormatException('HTTP date', date, () {
      final scanner = StringScanner(date);

      if (scanner.scan(_longWeekdayRegExp)) {
        // RFC 850 starts with a long weekday.
        scanner.expect(', ');
        final day = _parseInt(scanner, 2);
        scanner.expect('-');
        final month = _parseMonth(scanner);
        scanner.expect('-');
        final year = 1900 + _parseInt(scanner, 2);
        scanner.expect(' ');
        final time = _parseTime(scanner);
        scanner.expect(' GMT');
        scanner.expectDone();

        return _makeDateTime(year, month, day, time);
      }

      // RFC 1123 and asctime both start with a short weekday.
      scanner.expect(_shortWeekdayRegExp);
      if (scanner.scan(', ')) {
        // RFC 1123 follows the weekday with a comma.
        final day = _parseInt(scanner, 2);
        scanner.expect(' ');
        final month = _parseMonth(scanner);
        scanner.expect(' ');
        final year = _parseInt(scanner, 4);
        scanner.expect(' ');
        final time = _parseTime(scanner);
        scanner.expect(' GMT');
        scanner.expectDone();

        return _makeDateTime(year, month, day, time);
      }

      // asctime follows the weekday with a space.
      scanner.expect(' ');
      final month = _parseMonth(scanner);
      scanner.expect(' ');
      final day =
          scanner.scan(' ') ? _parseInt(scanner, 1) : _parseInt(scanner, 2);
      scanner.expect(' ');
      final time = _parseTime(scanner);
      scanner.expect(' ');
      final year = _parseInt(scanner, 4);
      scanner.expectDone();

      return _makeDateTime(year, month, day, time);
    });