root/trunk/plagger/lib/Plagger/Plugin/Filter/RSSLiberalDateTime.pm

Revision 869 (checked in by miyagawa, 14 years ago)

RSSLiberalDateTime: fix bug around minus timezone

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Filter::RSSLiberalDateTime;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Date::Parse ();
6 use DateTime::TimeZone::OffsetOnly;
7
8 sub register {
9     my($self, $context) = @_;
10     $context->register_hook(
11         $self,
12         'aggregator.filter.feed' => \&filter,
13     );
14 }
15
16 sub filter {
17     my($self, $context, $args) = @_;
18     $args->{content} =~ s{<(pubDate|lastBuildDate)>([^<]+)</\1>}
19                          {"<$1>" . $self->fixup_datetime($2) . "</$1>"}eg;
20 }
21
22 sub fixup_datetime {
23     my($self, $date) = @_;
24
25     my $valid = eval {  DateTime::Format::Mail->parse_datetime($date) };
26     return $date if $valid;
27
28     my @time = Date::Parse::strptime($date) or return $date;
29
30     my $dt   = DateTime->new(
31         second => $time[0],
32         minute => $time[1],
33         hour   => $time[2],
34         day    => $time[3],
35         month  => $time[4] + 1,
36         year   => $time[5] + 1900,
37     );
38
39     if ($time[6]) {
40         use integer;
41         my $tz = sprintf "%s%02d%02d", ($time[6] > 0 ? "+" : "-"), abs($time[6] / 3600), $time[6] % 3600;
42         $dt->set_time_zone($tz);
43     }
44
45     my $rfc822 = DateTime::Format::Mail->format_datetime($dt);
46     Plagger->context->log(info => "Fix $date to $rfc822");
47     $rfc822;
48 }
49
50 1;
51
52 __END__
53
54 =head1 NAME
55
56 Plagger::Plugin::Filter::RSSLiberalDateTime - Liberal datetime parsing on RSS 2.0 pubDate
57
58 =head1 SYNOPSIS
59
60   - module: Filter::RSSLiberalDateTime
61
62 =head1 DESCRIPTION
63
64 This plugin fixes a bad datetime format in RSS 2.0 pubDate and lastBuildDate.
65
66 =head1 AUTHOR
67
68 Tatsuhiko Miyagawa
69
70 =head1 SEE ALSO
71
72 L<Plagger>, L<DateTime::Format::Mail>, L<Date::Parse>
73
74 =cut
Note: See TracBrowser for help on using the browser.