Skip to content

Commit

Permalink
Merge pull request #22 from alexandergottlieb/master
Browse files Browse the repository at this point in the history
parse RssItem.pubDate to DateTime
  • Loading branch information
witochandra authored Aug 11, 2020
2 parents cf3bb97 + d142e44 commit 3ed572a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/domain/rss_item.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,7 +17,7 @@ class RssItem {

final List<RssCategory> categories;
final String guid;
final String pubDate;
final DateTime pubDate;
final String author;
final String comments;
final RssSource source;
Expand All @@ -32,7 +33,7 @@ class RssItem {
this.link,
this.categories,
this.guid,
this.pubDate,
String pubDate,
this.author,
this.comments,
this.source,
Expand All @@ -41,7 +42,8 @@ class RssItem {
this.enclosure,
this.dc,
this.itunes,
});
})
: this.pubDate = _parsePubDate(pubDate);

factory RssItem.parse(XmlElement element) {
return RssItem(
Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions test/rss_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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, "[email protected]");
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3ed572a

Please sign in to comment.