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

I've asked this date question and got several responses but couldn't get it to work. I have a timestamp field in my MySQL database in this format:

2008-11-03 19:03:44

I need to convert it to this:

Mon, 03 Nov 2008 20:15:00 EST

I've tried various things with HTTP::Date; but can't get it going. Can someone give me some code to use. I'm pulling the timestamp into a varible $timestamp.

Replies are listed 'Best First'.
Re: Converting timestamp to RSS pubDate
by smiffy (Pilgrim) on Nov 04, 2008 at 01:21 UTC

    So what did you do with HTTP::Date that did not work? It's a lot easier for us to help you if you show us your code.

    Just to give you a clue, HTTP::Date::time2str takes epoch time. You can actually make life much easier for yourself by getting the timestamp out of the database in the format that time2str is looking for.

    Have a look at UNIX_TIMESTAMP() in the MySQL Reference Manual. Also have a look at the very powerful DATE_FORMAT() function whilst you are there - you could theoretically do the whole thing in MySQL before Perl even sees it. I just find it easier to use HTTP::Date::time2str rather than messing about with % type format strings.

      I've tried the following
      $http_date = time2str( str2time("$timestamp", "-0600") );

      and...

      $http_date = time2str($timestamp);

      and... $http_date = parse_date($timestamp); among other things. All return dates that vary from 1969 to yesterday. I don't get it. I look at the timestamp in the database and it is set to the correct time.

      I just need to go from this:

      2008-11-03 19:03:44

      to this:

      Mon, 03 Nov 2008 20:15:00 EST

Re: Converting timestamp to RSS pubDate
by almut (Canon) on Nov 04, 2008 at 01:40 UTC

    The target date with HTTP::Date is always in GMT...  So, here's another suggestion:

    use Date::Manip; my $date = "2008-11-03 19:03:44"; Date_Init("TZ=EST"); my $http_date = UnixDate(ParseDate("$date -0612"), "%a, %d %b %Y %H:%M +:%S %Z"); print "$http_date\n"; # Mon, 03 Nov 2008 20:15:44 EST

    Not fully matching your requirement, but close :) — Adjust the input time zone (-0612) as appropriate.

    (If you seriously want to go from 19:03:44 to 20:15:00, look into the other functionality that Date::Manip has to offer for calculating with dates...)

      Thanks. This sort of worked but here what's weird. In the HTML code this was printed:

      <pubDate>Mon, 03 Nov 2008 21:37:50 EST</pubDate>

      If I look at the RSS Feed in Firefox or IE it reads this:

      Monday, November 03, 2008 4:37 PM

      I have no idea why. Any ideas? The 4:37 should read 9:37 shouldn't it? Also, the server is EST and it's only 8:37 there now so somewhere I'm gaining an hour.

        Why don't you just fiddle with the <pubDate>Mon, 03 Nov 2008 21:37:50 EST</pubDate> value until Firefox/IE is showing the correct date?  Try time zones other than EST (maybe it's expecting GMT, as would've been output by HTTP::Date), increment/decrement the hour, etc.  Shouldn't be too hard to eventually figure out how Firefox is interpreting the pubDate value...

Re: Converting timestamp to RSS pubDate
by ikegami (Patriarch) on Nov 04, 2008 at 03:05 UTC
    DateTime::Format::RSS?

    Update: Nevermind. It produces ISO8601 dates, but the spec calls for RFC822 dates. wth?