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

I have a date field attached to my article but in this format:

2008-10-29

I need to convert it to something like this for my rss feed:

Sun, 29 October 2008 15:21:36 GMT

How can I do that and assign it to a variable for printing my rss feed?

Replies are listed 'Best First'.
Re: Date question
by Fletch (Bishop) on Oct 30, 2008 at 22:16 UTC

    No module in the world is going to be able to extract a timestamp like that out from just a "YYYY-MM-DD" string (it certainly could make up something on the same day (usually midnight), but . . .). If you mean you've got a date field in a database, you're similarly up the creek since that would lack the requisite resolution as well.

    Now if there's actually a datetime or timestamp (or whatever your DB calls it) value then you want to change how you're extracting the value from the database (and most likely would be able to get it returned to you in your desired format without need for further munging).

    Not that that's a Perl problem, per se . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Date question
by ccn (Vicar) on Oct 30, 2008 at 20:47 UTC
      I must be doing something wrong. I have use HTTP::Date; in there and I call it like this:

      $date2 = parse_date($date);

      but this gets returned:

      "Undefined subroutine &main::parse_date called at line 986"

        parse_date() is not exported by default, so you can either write

        $date2 = HTTP::Date::parse_date($date);

        or import the function into your namespace

        use HTTP::Date qw(parse_date);

        (See Exporter for what this is about)

Re: Date question
by smiffy (Pilgrim) on Oct 31, 2008 at 09:46 UTC

    Generally, if taking data from a database, I'd say to get all the date formats sorted out at DB level before they get passed to Perl. MySQL, for instance, has date_format() which means that the date can come straight out of the query without further need of manipulation.

    However, for the abhorrent date format used by the HTTP protocol (required for HTTP headers including expiries, cookies, etc), I would second (or third) the use of HTTP::Date. This module and Date::Calc form the best part of my Perl date-manipulation toolkit.