Permalink
Browse files

Add DateTime stringification to propper Atom format

  • Loading branch information...
1 parent 50d39bc commit bd98c7d837d007d3411669d5b8d1016713e8eb13 @omega omega committed with miyagawa Apr 9, 2010
Showing with 34 additions and 1 deletion.
  1. +7 −1 lib/XML/Atom.pm
  2. +2 −0 lib/XML/Atom/Base.pm
  3. +25 −0 t/30-datetime-stringification.t
View
@@ -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;
View
@@ -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));
@@ -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.