From 4a096553c1d44b235ec65257c81ee0c039b371a7 Mon Sep 17 00:00:00 2001 From: Wito Chandra Date: Wed, 12 Aug 2020 00:29:02 +0700 Subject: [PATCH] add datetime.dart --- lib/domain/rss_item.dart | 14 ++++---------- lib/util/datetime.dart | 12 ++++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 lib/util/datetime.dart diff --git a/lib/domain/rss_item.dart b/lib/domain/rss_item.dart index 4af2b96..edecd9b 100644 --- a/lib/domain/rss_item.dart +++ b/lib/domain/rss_item.dart @@ -1,4 +1,3 @@ -import 'package:intl/intl.dart'; import 'package:webfeed/domain/dublin_core/dublin_core.dart'; import 'package:webfeed/domain/media/media.dart'; import 'package:webfeed/domain/rss_category.dart'; @@ -6,6 +5,7 @@ import 'package:webfeed/domain/rss_content.dart'; import 'package:webfeed/domain/rss_enclosure.dart'; import 'package:webfeed/domain/rss_item_itunes.dart'; import 'package:webfeed/domain/rss_source.dart'; +import 'package:webfeed/util/datetime.dart'; import 'package:webfeed/util/xml.dart'; import 'package:xml/xml.dart'; @@ -32,7 +32,7 @@ class RssItem { this.link, this.categories, this.guid, - String pubDate, + this.pubDate, this.author, this.comments, this.source, @@ -41,7 +41,7 @@ class RssItem { this.enclosure, this.dc, this.itunes, - }) : this.pubDate = _parsePubDate(pubDate); + }); factory RssItem.parse(XmlElement element) { return RssItem( @@ -52,7 +52,7 @@ class RssItem { return RssCategory.parse(element); }).toList(), guid: findElementOrNull(element, "guid")?.text, - pubDate: findElementOrNull(element, "pubDate")?.text, + pubDate: parseDateTime(findElementOrNull(element, "pubDate")?.text), author: findElementOrNull(element, "author")?.text, comments: findElementOrNull(element, "comments")?.text, source: RssSource.parse(findElementOrNull(element, "source")), @@ -63,10 +63,4 @@ class RssItem { itunes: RssItemItunes.parse(element), ); } - - static _parsePubDate(pubDate) { - if (pubDate == null) return null; - //Locale for pubDate is always en_US, regardless of device locale - return DateFormat('EEE, dd MMM yyyy HH:mm:ss Z', 'en_US').parse(pubDate); - } } diff --git a/lib/util/datetime.dart b/lib/util/datetime.dart new file mode 100644 index 0000000..ef9b289 --- /dev/null +++ b/lib/util/datetime.dart @@ -0,0 +1,12 @@ +import 'package:intl/intl.dart'; + +var rfc822DateFormat = DateFormat('EEE, dd MMM yyyy HH:mm:ss Z', 'en_US'); + +DateTime parseDateTime(dateString) { + if (dateString == null) return null; + try { + return rfc822DateFormat.parse(dateString); + } on FormatException { + return null; + } +}