Subject: | Does not do rfc822 date processing |
You use a wrong format to generate an rfc822 date. Instead of %Z you
should use %z. `man 3 strftime` describes the correct format:
RFC 2822-compliant date format (with an English locale for %a and
%b)
"%a, %d %b %Y %T %z"
RFC 822-compliant date format (with an English locale for %a and
%b)
"%a, %d %b %y %T %z"
With the capital Z the feed is invalid at http://validator.w3.org/feed/
Additionally, I dont like that all dates are converted to the local
timezone. Why is this happening?
I think it should only parse the date if it is not already in rfc822
format.
See attached script for fix and testing.
Subject: | cgi-rss.pl |
#!/usr/bin/perl -w
use strict;
use CGI::RSS;
use POSIX qw(strftime);
# run script then enter a few dates to test or remove line 102 to see rss output
{
package CGI::RSS;
sub valid_rfc822_date ($) {
$_[0] =~ m!^
(?:
(?:
Mon | Tue | Wed |
Thu | Fri | Sat |
Sun
) # day
,\s\s? # comma, space or two
)? # (these were optional)
\d\d?\s # day with 1 or 2 digit, space
(?:
Jan | Feb | Mar |
Apr | May | Jun |
Jul | Aug | Sep |
Oct | Nov | Dec
) # month
\s\d{2,4}\s # space, 2 or 4 digit year, space
\d\d:\d\d:\d\d\s # hr:min:sec, space
(?:
[\+\-]\d\d\d\d | # time zone with digits, or
UT | GMT | EST |
EDT | CST | CDT |
MST | MDT | PST |
PDT | Z | A |
M | N | Y # time zone with characters
)$
!ix;
}
sub date {
my $this = shift;
my $date = shift;
if( ! valid_rfc822_date($date) and my $pd = &ParseDate($date) ) {
warn "parsing date: $date\n";
my $rfc822_date = &UnixDate($pd, '%a, %d %b %Y %H:%M:%S %z');
return $this->pubDate($rfc822_date);
}
$this->pubDate($date);
}
}
my $rss = new CGI::RSS;
my @feed = (
{
title => "first item",
link => "http://localhost/directory/1",
guid => "http://localhost/directory/1",
desc => "this is the first item",
date => "Sun, 16 Oct 2011 06:45:03 +0900"
},
{
title => "second item",
link => "http://localhost/directory/2",
guid => "http://localhost/directory/2",
desc => "this is the second item",
date => "15 Oct 2011 12:15:06 +0200"
},
{
title => "third item",
link => "http://localhost/directory/3",
guid => "http://localhost/directory/3",
desc => "this is the third item",
date => "2011-08-11 07:02:26"
},
{
title => "fourth item",
link => "http://localhost/directory/4",
guid => "http://localhost/directory/4",
desc => "this is the fourth item",
date => "Sat, 15 Oct 2011 09:31:01 +0800 (CST)"
},
);
print $rss->header;
print $rss->begin_rss(
title => "My Feed!",
link => "http://localhost/directory",
desc => "My feed is cool!"
);
#print $rss->date($_) while (<>); exit;
foreach my $h ( @feed ) {
print $rss->item(
$rss->title ( $h->{title} ),
$rss->link ( $h->{link} ),
$rss->guid ( $h->{link} ), # unique identifier, usually
$rss->description ( $h->{desc} ), # a permalink
$rss->date ( $h->{date} ), # does rfc822 date processing
);
}
print $rss->finish_rss;