Skip to content

Commit

Permalink
Add DateTime stringification to propper Atom format
Browse files Browse the repository at this point in the history
  • Loading branch information
omega authored and miyagawa committed May 23, 2011
1 parent 50d39bc commit bd98c7d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/XML/Atom.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ use 5.008_001;
our $VERSION = '0.37';

BEGIN {
@XML::Atom::EXPORT = qw( LIBXML );
@XML::Atom::EXPORT = qw( LIBXML DATETIME);
if (eval { require XML::LibXML }) {
*{XML::Atom::LIBXML} = sub() {1};
} else {
require XML::XPath;
*{XML::Atom::LIBXML} = sub() {0};
}
if (eval { require DateTime::Format::Atom }) {
*{XML::Atom::DATETIME} = sub() {1};
} else {
*{XML::Atom::DATETIME} = sub() {0};
}

local $^W = 0;
*XML::XPath::Function::namespace_uri = sub {
my $self = shift;
Expand Down
2 changes: 2 additions & 0 deletions lib/XML/Atom/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ sub set {
$elem->appendAttribute($attr);
}
}
} elsif (DATETIME && UNIVERSAL::isa($val, "DateTime")) {
return $obj->set($ns, $name, DateTime::Format::Atom->format_datetime($val), $attr, $add);
} else {
if (LIBXML) {
$elem->appendChild(XML::LibXML::Text->new($val));
Expand Down
25 changes: 25 additions & 0 deletions t/30-datetime-stringification.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use strict;
use warnings;
use Test::More;

BEGIN {
unless (eval { require DateTime } and eval { require DateTime::Format::Atom }) {
plan skip_all => 'DateTime is required for tests';
}
}

plan tests => 2;

use XML::Atom::Feed;

my $f = XML::Atom::Feed->new();

my $dt = DateTime->now();

$f->updated($dt);

my $xml = $f->as_xml;
my $dt_string = DateTime::Format::Atom->format_datetime($dt);

like($xml, qr/$dt_string/, "correct format made");
unlike($xml, qr|<modified/>|, "no empty modified elements");

0 comments on commit bd98c7d

Please sign in to comment.