Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm using XML::RSS to parse a number of RSS newsfeeds, each using different versions of RSS. I'm running into a problem because the pubDate for each feed is formatted differently. Is there a module/code snippet I could use to easily take the date for any RSS item and convert it to, say, this format?

Aug 19, 2004 05:22:00PM

Replies are listed 'Best First'.
Re: Parsing RSS dates
by Eimi Metamorphoumai (Deacon) on Aug 19, 2004 at 21:59 UTC
    You might try Date::Manip. It's one of the slower Date/Time modules out there, but it can accept pretty much any format.
Re: Parsing RSS dates
by Your Mother (Archbishop) on Aug 20, 2004 at 05:07 UTC

    This will do what you're after in your example.

    use Date::Manip; my $str_date = shift || die "No date!\n"; # almost *any* format my $date = UnixDate( ParseDate( $str_date ), "%b %e, %Y %r"); $date || die "Could not interpret date: $str_date!\n"; print $date, "\n";

    You could also try to write up a regex/sub to check based on what the RSSes are supposed to follow, RFC #822, but I'm sure there are RSSes that break it.

    One could argue that using Date::Manip to allow arbitrary dates (eg, it will even work on things like "10 days ago at noon UT") is really a bad idea b/c it allows RSS writers to get away with breaking the specification. And Date::Manip is slow but unless it's being run off a highly trafficked CGI it probably doesn't matter.