Debian Codegraph @

t/29-date-parsing.t

raw
1
2 use strict;
3 use warnings;
4 use Test::More;
5 use XML::Feed;
6
7 my ($rss, $atom) = split /\n---\n/, join '', <DATA>;
8 my $now = DateTime->now;
9 my $y = $now->year;
10 my $m = $now->month;
11 my $d = $now->day;
12
13 for my $spec (
14 [ '2010-05-17T06:58:50-08:00', '2010:5:17:6:58:50:-0800' ],
15 [ '2009-05-29T20:17:07+01:00', '2009:5:29:20:17:7:+0100' ],
16 [ '2010-05-20T12:14:57-05:00', '2010:5:20:12:14:57:-0500' ],
17 [ 'May 29, 2004 23:39:25', '2004:5:29:23:39:25:floating' ],
18 [ '4/13/2010 6:58:50 PM', '2010:4:13:18:58:50:floating' ],
19 [ 'May 19, 2010', '2010:5:19:0:0:0:floating' ],
20 [ 'feb 28 5pm', "$y:2:28:17:0:0:floating" ],
21 [ 'may 21st', "$y:5:21:0:0:0:floating" ],
22 [ 'march 2nd 2009', '2009:3:2:0:0:0:floating' ],
23 [ 'October 2006', '2006:10:1:0:0:0:floating' ],
24 [ 'jan 3 2010', '2010:1:3:0:0:0:floating' ],
25 [ '3 jan 2010', '2010:1:3:0:0:0:floating' ],
26 [ '27/5/1979', '1979:5:27:0:0:0:floating' ],
27 [ '4:00', "$y:$m:$d:4:0:0:floating" ],
28 [ '20:00', "$y:$m:$d:20:0:0:floating" ],
29 [ '3:20:00', "$y:$m:$d:3:20:0:floating" ],
30 [ '2009-09-08T00:25:30Z', '2009:9:8:0:25:30:UTC' ],
31 [ 'Fri, 21 May 2010 12:00:37 +0000', '2010:5:21:12:0:37:UTC' ],
32 [ 'Thu, 20 May 2010 13:55:00 GMT', '2010:5:20:13:55:0:GMT' ],
33 [ 'Wed, 19 May 2010 14:56:00 -0400', '2010:5:19:14:56:0:-0400' ],
34 [ 'Fri, 21 May 2010 09:30:25 PDT', '2010:5:21:9:30:25:America/Los_Angeles' ],
35 [ 'Wed, 05 May 2010 17:29:27 +0000', '2010:5:5:17:29:27:+0000' ],
36 [ '2010-05-21', '2010:5:21:0:0:0:floating' ],
37 ) {
38 my $date = $spec->[0];
39 my %params;
40 @params{qw(year month day hour minute second time_zone)} = split /:/ => $spec->[1];
41 my $dt = DateTime->new(%params);
42
43 # Try RSS with PubDate.
44 ok my $feed = XML::Feed->parse(\sprintf($rss,
45 'pubDate', $date, 'pubDate',
46 'pubDate', $date, 'pubDate',
47 'dcterms:modified', $date, 'dcterms:modified'
48 )), "Create RSS with PubDate $date";
49
50 is $feed->modified, $dt, 'Feed modified PubDate should be correct';
51 my ($entry) = $feed->entries;
52 is $entry->issued, $dt, 'Entry issued PubDate should be correct';
53 is $entry->modified, $dt, 'Entry modified date should be correct';
54
55 # Try RSS with dc:date and dcterms:modified.
56 ok $feed = XML::Feed->parse(\sprintf($rss,
57 'dc:date', $date, 'dc:date',
58 'dc:date', $date, 'dc:date',
59 'dcterms:modified', $date, 'dcterms:modified'
60 )), "Create RSS with dc:date $date";
61
62 is $feed->modified, $dt, 'Feed modified dc:date should be correct';
63 ($entry) = $feed->entries;
64 is $entry->issued, $dt, 'Entry issued dc:date should be correct';
65
66 # Try RSS with dcterms:date and atom:updated.
67 ok $feed = XML::Feed->parse(\sprintf($rss,
68 'dc:date', $date, 'dc:date',
69 'dcterms:date', $date, 'dcterms:date',
70 'atom:updated', $date, 'atom:updated'
71 )), "Create RSS with dcterms:date and atom:updated $date";
72
73 ($entry) = $feed->entries;
74 is $entry->issued, $dt, 'Entry issued dcterms:date should be correct';
75 is $entry->modified, $dt, 'Entry modified atom:updated should be correct';
76
77 # Try Atom feed.
78 ok $feed = XML::Feed->parse(\sprintf($atom,
79 'updated', $date, 'updated',
80 'published', $date, 'published',
81 'updated', $date, 'updated',
82 )), "Create Atom with date $date";
83
84 is $feed->modified, $dt, 'Atom feed updated date should be correct';
85 ($entry) = $feed->entries;
86 is $entry->issued, $dt, 'Atom entry published date should be correct';
87 is $entry->modified, $dt, 'Atom entry updated date should be correct';
88 }
89
90 done_testing;
91
92 __DATA__
93 <?xml version="1.0" encoding="UTF-8"?>
94 <rss version="2.0"
95 xmlns:content="http://purl.org/rss/1.0/modules/content/"
96 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
97 xmlns:dc="http://purl.org/dc/elements/1.1/"
98 xmlns:dcterms="http://purl.org/dc/terms/"
99 xmlns:atom="http://www.w3.org/2005/Atom"
100 >
101 <channel>
102 <title>Simple RSS Feed</title>
103 <link>http://example.net</link>
104 <%s>%s</%s>
105 <item>
106 <%s>%s</%s>
107 <%s>%s</%s>
108 </item>
109 </channel>
110 </rss>
111 ---
112 <?xml version="1.0" encoding="utf-8"?>
113 <feed xmlns="http://www.w3.org/2005/Atom">
114 <title>Simple Atom Feed</title>
115 <%s>%s</%s>
116 <entry>
117 <%s>%s</%s>
118 <%s>%s</%s>
119 </entry>
120 </feed>