Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime fields in RssFeed #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/domain/rss_feed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:webfeed/domain/rss_cloud.dart';
import 'package:webfeed/domain/rss_image.dart';
import 'package:webfeed/domain/rss_item.dart';
import 'package:webfeed/domain/syndication/syndication.dart';
import 'package:webfeed/util/datetime.dart';
import 'package:webfeed/util/xml.dart';
import 'package:xml/xml.dart';

Expand All @@ -22,7 +23,8 @@ class RssFeed {
final List<RssCategory> categories;
final List<String> skipDays;
final List<int> skipHours;
final String lastBuildDate;
final DateTime pubDate;
final DateTime lastBuildDate;
final String language;
final String generator;
final String copyright;
Expand All @@ -46,6 +48,7 @@ class RssFeed {
this.categories,
this.skipDays,
this.skipHours,
this.pubDate,
this.lastBuildDate,
this.language,
this.generator,
Expand Down Expand Up @@ -97,7 +100,8 @@ class RssFeed {
?.map((e) => int.tryParse(e.text ?? '0'))
?.toList() ??
[],
lastBuildDate: findFirstElement(channelElement, 'lastBuildDate')?.text,
pubDate: parseDateTime(findFirstElement(channelElement, 'pubDate')?.text),
lastBuildDate: parseDateTime(findFirstElement(channelElement, 'lastBuildDate')?.text),
language: findFirstElement(channelElement, 'language')?.text,
generator: findFirstElement(channelElement, 'generator')?.text,
copyright: findFirstElement(channelElement, 'copyright')?.text,
Expand Down
6 changes: 3 additions & 3 deletions lib/util/datetime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:intl/intl.dart';

const rfc822DatePattern = 'EEE, dd MMM yyyy HH:mm:ss Z';

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

DateTime _parseRfc822DateTime(String dateString) {
Expand All @@ -18,7 +18,7 @@ DateTime _parseRfc822DateTime(String dateString) {
}
}

DateTime _parseIso8601DateTime(dateString) {
DateTime _parseIso8601DateTime(String dateString) {
try {
return DateTime.parse(dateString);
} on FormatException {
Expand Down
3 changes: 2 additions & 1 deletion test/rss_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ void main() {
expect(feed.link, 'https://foo.bar.news/');
expect(feed.author, '[email protected]');
expect(feed.language, 'en-US');
expect(feed.lastBuildDate, 'Mon, 26 Mar 2018 14:00:00 PDT');
expect(feed.pubDate, DateTime(2018, 03, 26, 13, 30));
expect(feed.lastBuildDate, DateTime(2018, 03, 26, 14));
expect(feed.generator, 'Custom');
expect(feed.copyright, 'Copyright 2018, Foo bar Inc.');
expect(feed.docs, 'https://foo.bar.news/docs');
Expand Down
1 change: 1 addition & 0 deletions test/xml/RSS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<category>Ipsum</category>
<category domain="news">Lorem Ipsum</category>
<language>en-US</language>
<pubDate>Mon, 26 Mar 2018 13:30:00 PDT</pubDate>
<lastBuildDate>Mon, 26 Mar 2018 14:00:00 PDT</lastBuildDate>
<generator>Custom</generator>
<copyright>Copyright 2018, Foo bar Inc.</copyright>
Expand Down