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

I'm using XML::RSS to setup an RSS feed from a CGI,

For each item I have a $id, $title, $link and a $blurb as follows:

$rss->add_item( title => encode_entities("$id: $title"), link => $link, description => encode_entities($blurb) );

My question is how to set the items datetime. The datetime is in a $time SELECTed through DBI from a MySQL TIMESTAMP.

What hash key and conversion should I perform on it to include this information with the add_item hash?

Or am I missing something?

Any ideas appreciated. -Andrew


Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com

Replies are listed 'Best First'.
Re: XML::RSS timestamping items
by pg (Canon) on Jul 25, 2005 at 00:30 UTC

    You have to first check RSS 2.0 specification:

    "The publication date for the content in the channel. For example, the New York Times publishes on a daily basis, the publication date flips once every 24 hours. That's when the pubDate of the channel changes. All date-times in RSS conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred)."

    Now what is the format defined in RFC822:

    date-time = [ day "," ] date time ; dd mm yy ; hh:mm:ss zzz day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun" date = 1*2DIGIT month 2DIGIT ; day month year ; e.g. 20 Jun 82 month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec" time = hour zone ; ANSI and Military hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59 zone = "UT" / "GMT" ; Universal Time ; North American : UT / "EST" / "EDT" ; Eastern: - 5/ - 4 / "CST" / "CDT" ; Central: - 6/ - 5 / "MST" / "MDT" ; Mountain: - 7/ - 6 / "PST" / "PDT" ; Pacific: - 8/ - 7 / 1ALPHA ; Military: Z = UT; ; A:-1; (J not used) ; M:-12; N:+1; Y:+12 / ( ("+" / "-") 4DIGIT ) ; Local differential ; hours+min. (HHMM)

    One example according to this format is Mon, 25 Jul 2005 00:13:28 GMT. If you follow RFC822 strictly, this should be Mon, 25 Jul 05 00:13:28 GMT. However as RSS 2.0 pointed out, the year is an exception. 4 digit year is not only allowed but also preferred.

    Last but not least check out this yahoo rss service, which would give you a good example.

      Ahh, ignore me. The Yahoo RSS setting showed pubDate in the items themselves. Thanks. I'll give that a try.


      Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com
Re: XML::RSS timestamping items
by Your Mother (Archbishop) on Jul 25, 2005 at 01:01 UTC

    I did the same thing a month ago with Template::Toolkit so I have a format string handy. You'll need your item's creation time in epoch seconds to do this (swap it for "time()").

    use Date::Format; print time2str('%a, %d %b %Y %H:%m:%S GMT', time()), "\n";

    Here's the TT2 version. This assumes a DB "item" which has an epoch seconds "created_time" that matches GMT. You can easily do hour offsets to the "gmt" with multiples of 60*60.

    [% USE date %] <pubDate>[%- gmt = item.created_time -%] [%- date.format(gmt, '%a, %d %b %Y %H:%m:%S GMT') -%]</pubDate>

    Update: I left out the conversion I'm using from TIMESTAMP|DATETIME though there are more ways to do this. I *think* the regex will match different MySQL versions of the DB strings. Still have to do your own GMT adjustment somewhere if necessary.

    use Date::Calc; my $item_time = Date::Calc::Mktime ( $mysql_timestamp =~ /^(\d\d\d\d)\D*(\d\d)\D*(\d\d)\D*(\d\d)\D*(\d\d)\D*(\d\d)$/ ); print time2str('%a, %d %b %Y %H:%m:%S GMT', $item_time), "\n";
Re: XML::RSS timestamping items
by jhourcle (Prior) on Jul 24, 2005 at 23:19 UTC

    I've never used XML::RSS, but the XML::RSS docs state:

    channel (title=>$title, link=>$link, description=>$desc, language=>$language, rating=>$rating, copyright=>$copyright, pubDate=>$pubDate, lastBuildDate=>$lastBuild, docs=>$docs, managingEditor=>$editor, webMaster=>$webMaster)

    I can only assume that 'pubDate' is short for 'publication date', given the two times it occurs in the documentation. The other time, there's a mention of 'date' under 'dc' (short for Dublin Core), and shows the format is an ISO date:

    use XML::RSS; my $rss = new XML::RSS (version => '1.0'); $rss->channel( title => "freshmeat.net", link => "http://freshmeat.net", description => "the one-stop-shop for all your Linux software need +s", dc => { date => '2000-08-23T07:00+00:00', subject => "Linux Software", creator => 'scoop@freshmeat.net', publisher => 'scoop@freshmeat.net', rights => 'Copyright 1999, Freshmeat.net', language => 'en-us', }, syn => { updatePeriod => "hourly", updateFrequency => "1", updateBase => "1901-01-01T00:00+00:00", }, taxo => [ 'http://dmoz.org/Computers/Internet', 'http://dmoz.org/Computers/PC' ] );

    Update: From your later responses, it seems that the issue isn't just what the correct keys and formats to the value are when using XML::RSS, but that you're not actually familiar with RSS. You may wish to read the RSS 2.0 specification, and if you're dealing with RSS 0.9 or 1.0, the notes on RSS in the RDF primer. Even w3schools has a lesson on it. (I haven't worked w/ RSS in years, and back then, I just generated it by hand)