1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
package XML::Feed::Util;
use strict;
use warnings;
our $VERSION = '0.65';
use base qw( Exporter );
use DateTime::Format::Flexible;
use DateTime::Format::ISO8601;
use DateTime::Format::Natural;
use DateTime::Format::W3CDTF;
use DateTime::Format::Mail;
our @EXPORT_OK = qw(
format_w3cdtf
parse_datetime
parse_w3cdtf_date
parse_mail_date
);
sub format_w3cdtf {
my $dt = shift;
my $date = DateTime::Format::W3CDTF->format_datetime($dt);
# Add timezone "Z" if "floating" DateTime.
$date .= 'Z' if $dt->time_zone->is_floating;
return $date;
}
sub parse_datetime {
my $ts = shift or return undef;
$ts = _strip_spaces($ts);
return eval { DateTime::Format::ISO8601->parse_datetime($ts) }
|| eval { DateTime::Format::Flexible->parse_datetime($ts) }
|| do {
my $p = DateTime::Format::Natural->new;
my $dt = $p->parse_datetime($ts);
$p->success ? $dt : undef;
};
};
sub parse_mail_date {
my $ts = shift or return undef;
$ts = _strip_spaces($ts);
return eval { DateTime::Format::Mail->new(loose => 1)->parse_datetime($ts) }
|| parse_datetime($ts);
};
sub parse_w3cdtf_date {
my $ts = shift or return undef;
$ts = _strip_spaces($ts);
return eval { DateTime::Format::W3CDTF->parse_datetime($ts) }
|| parse_datetime($ts);
};
sub _strip_spaces {
local $_ = shift;
s/^\s+//, s/\s+$// if $_;
return $_;
}
1;
__END__
=head1 NAME
XML::Feed::Util - Utility functions
=head1 SYNOPSIS
use XML::Feed::Util qw(
format_w3cdtf
parse_datetime
parse_mail_date
parse_w3cdtf_date
);
use DateTime;
print format_w3cdtf(DateTime->now);
my $dt;
$dt = parse_datetime('January 8, 1999');
$dt = parse_mail_date('Fri, 23 Nov 2001 21:57:24 -0600');
$dt = parse_w3cdtf_date('2003-02-15T13:50:05-05:00');
=head1 DESCRIPTION
Common utility or helper functions.
=head1 USAGE
=head2 format_w3cdtf($datetime)
Convert DateTime object to W3CDTF format string.
Uses default timezone "Z" for "floating" DateTime.
=head2 parse_datetime($date)
Parse any date string using L<DateTime::Format::ISO8601>,
L<DateTime::Format::Flexible> or L<DateTime::Format::Natural>.
Returns DateTime object or undef.
=head2 parse_mail_date($date)
Parse date in RFC2822/822 format using L<DateTime::Format::Mail>.
Fallback to C<parse_datetime()> for other formats.
Returns DateTime object or undef.
=head2 parse_w3cdtf_date($date)
Parse date W3CDTF format using L<DateTime::Format::W3CDTF>.
Fallback to C<parse_datetime()> for other formats.
Returns DateTime object or undef.
=head1 AUTHOR & COPYRIGHT
Please see the I<XML::Feed> manpage for author, copyright, and license
information.
=cut
|