Skip to content

Commit

Permalink
Merge pull request #34 from alexandergottlieb/master
Browse files Browse the repository at this point in the history
Support shortened RFC 822 dates
  • Loading branch information
witochandra authored Aug 17, 2020
2 parents 09cbc7a + 27e40c8 commit e300254
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/util/datetime.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import 'package:intl/intl.dart';

var rfc822DateFormat = DateFormat('EEE, dd MMM yyyy HH:mm:ss Z', 'en_US');
const rfc822DatePattern = 'EEE, dd MMM yyyy HH:mm:ss Z';

DateTime parseDateTime(dateString) {
if (dateString == null) return null;
return _parseRfc822DateTime(dateString) ?? _parseIso8601DateTime(dateString);
}

DateTime _parseRfc822DateTime(dateString) {
DateTime _parseRfc822DateTime(String dateString) {
try {
return rfc822DateFormat.parse(dateString);
final length = dateString?.length?.clamp(0, rfc822DatePattern.length);
final trimmedPattern = rfc822DatePattern.substring(0, length); //Some feeds use a shortened RFC 822 date, e.g. 'Tue, 04 Aug 2020'
final format = DateFormat(trimmedPattern, 'en_US');
return format.parse(dateString);
} on FormatException {
return null;
}
Expand Down

0 comments on commit e300254

Please sign in to comment.