From d142e44cbde6b2c82b61025a55bb88e27677c36b Mon Sep 17 00:00:00 2001 From: Xander Gottlieb Date: Wed, 6 Nov 2019 11:29:23 +0100 Subject: [PATCH] parse pubDate to DateTime --- lib/domain/rss_item.dart | 14 +++++++++++--- pubspec.yaml | 1 + test/rss_test.dart | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/domain/rss_item.dart b/lib/domain/rss_item.dart index 54c1d5c..783585a 100644 --- a/lib/domain/rss_item.dart +++ b/lib/domain/rss_item.dart @@ -1,3 +1,4 @@ +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'; @@ -16,7 +17,7 @@ class RssItem { final List categories; final String guid; - final String pubDate; + final DateTime pubDate; final String author; final String comments; final RssSource source; @@ -32,7 +33,7 @@ class RssItem { this.link, this.categories, this.guid, - this.pubDate, + String pubDate, this.author, this.comments, this.source, @@ -41,7 +42,8 @@ class RssItem { this.enclosure, this.dc, this.itunes, - }); + }) + : this.pubDate = _parsePubDate(pubDate); factory RssItem.parse(XmlElement element) { return RssItem( @@ -63,4 +65,10 @@ 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/pubspec.yaml b/pubspec.yaml index b7bb403..85aa596 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,6 +7,7 @@ environment: sdk: ">=2.0.0 <3.0.0" dependencies: xml: "^3.0.0" + intl: "^0.16.0" dev_dependencies: test: ^1.3.0 http: "^0.11.3+16" diff --git a/test/rss_test.dart b/test/rss_test.dart index 5b14dfc..60cd46c 100644 --- a/test/rss_test.dart +++ b/test/rss_test.dart @@ -72,7 +72,7 @@ void main() { "Lorem ipsum dolor sit amet, consectetur adipiscing elit"); expect(feed.items.first.link, "https://foo.bar.news/1"); expect(feed.items.first.guid, "https://foo.bar.news/1?guid"); - expect(feed.items.first.pubDate, "Mon, 26 Mar 2018 14:00:00 PDT"); + expect(feed.items.first.pubDate, DateTime(2018, 03, 26, 14)); //Mon, 26 Mar 2018 14:00:00 PDT expect(feed.items.first.categories.first.domain, "news"); expect(feed.items.first.categories.first.value, "Lorem"); expect(feed.items.first.author, "alice@foo.bar.news"); @@ -102,7 +102,7 @@ void main() { var item = feed.items.first; expect(item.title, null); expect(item.link, "http://www.foo.com"); - expect(item.pubDate, "Mon, 27 Aug 2001 16:08:56 PST"); + expect(item.pubDate, DateTime(2001, 08, 27, 16, 08, 56)); //Mon, 27 Aug 2001 16:08:56 PST expect(item.media.group.contents.length, 5); expect(item.media.group.credits.length, 2);