Skip to content

Commit d4cee89

Browse files
authored
Merge pull request #56 from nanto/date-format-timezone
Format DateTime with non-UTC time zone correctly
2 parents aa32f9c + a372094 commit d4cee89

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/XML/Feed/Util.pm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ our @EXPORT_OK = qw(
1919
);
2020

2121
sub format_w3cdtf {
22-
my $date = DateTime::Format::W3CDTF->format_datetime(shift);
22+
my $dt = shift;
23+
24+
my $date = DateTime::Format::W3CDTF->format_datetime($dt);
2325

2426
# Add timezone "Z" if "floating" DateTime.
25-
$date =~ s/(:\d\d(?:\.\d+)?)\s*$/$1Z/;
27+
$date .= 'Z' if $dt->time_zone->is_floating;
2628

2729
return $date;
2830
}

t/30-date-format-timezone.t

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use strict;
2+
use warnings;
3+
use Test::More;
4+
5+
use DateTime;
6+
use XML::XPath;
7+
8+
use XML::Feed;
9+
use XML::Feed::Entry;
10+
11+
# https://github.com/davorg/xml-feed/issues/55
12+
13+
my $feed = XML::Feed->new('Atom');
14+
15+
# Bugs are with DateTime with non-UTC time zone.
16+
my $dt = DateTime->now(time_zone => 'Asia/Tokyo');
17+
18+
$feed->title("My Atom feed");
19+
$feed->link("http://www.example.com");
20+
$feed->author("Author");
21+
$feed->updated($dt);
22+
$feed->id("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344eaa6a");
23+
24+
my $entry = XML::Feed::Entry->new('Atom');
25+
$entry->title("An important event");
26+
$entry->author("Important author");
27+
$entry->content("A very interesting event happened.");
28+
29+
$entry->issued($dt);
30+
$entry->updated($dt);
31+
$entry->id("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a");
32+
33+
$feed->add_entry($entry);
34+
35+
my $xp = XML::XPath->new(xml => $feed->as_xml);
36+
my $rfc3339 = qr!<updated>\d{4}-\d{2}-\d{2}T.+\+09:00</updated>!;
37+
like $xp->findnodes_as_string('/feed/updated'), $rfc3339;
38+
like $xp->findnodes_as_string('/feed/entry/updated'), $rfc3339;
39+
40+
done_testing();

0 commit comments

Comments
 (0)