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

How do I pass a MySQL timestamp (YYYY-MM-DD HH:MI:SS) into Template Toolkit's Date plugin (which expects h:m:s d/m/y) when I'm pulling the value via CDBI? I found one thread via Google here, but it suggests what I would normally do which is modify the result within SQL. I don't know CDBI very well, so what should I do?

  • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
  • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
  • Comment on MySQL Timestamp from CDBI to TT Date Plugin

Replies are listed 'Best First'.
Re: MySQL Timestamp from CDBI to TT Date Plugin
by cees (Curate) on Jun 16, 2005 at 14:56 UTC

    You should look at inflating your timestamp values into objects using Class::DBI. You can use Time::Piece::mysql for this, or DateTime::Format::MySQL (there are others as well that will work like Class::Date which is described on the cdbi wiki).

    use Time::Piece::mysql; __PACKAGE__->has_a(date_col => 'Time::Piece'); # or use DateTime::Format::MySQL; __PACKAGE__->has_a(date_col => 'DateTime', inflate => sub { DateTime::Format::MySQL->parse_datetime(shift) }, deflate => sub { DateTime::Format::MySQL->format_datetime(shift) });

    Then when you have your Class::DBI object, and you retrieve the date column, you will get a Time::Piece or DateTime object that you can pass to the TT Date Plugin in whatever format it accepts.

    Or, since your dates are now objects already, just use them directly in TT to format your dates. This removes the need for the Date plugin.

    [% date_col.ymd('-') %]
Re: MySQL Timestamp from CDBI to TT Date Plugin
by jfroebe (Parson) on Jun 16, 2005 at 15:02 UTC

    Hi Dragonchild

    Take a look at date_format if you want to use a sql type method to retrieve the datetime. The perl modules mentioned in earlier replies would be for a perl type solution. Personally I would go for the sql type method.

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: MySQL Timestamp from CDBI to TT Date Plugin
by mugwumpjism (Hermit) on Jun 16, 2005 at 21:43 UTC

    I find it laughable that this standard ISO8660 date format is not as widely supported as you'd expect of a standard date format.

    You can use this via the Date plugin if you have Date::Manip installed;

    [% manip = date.manip %] [% manip.UnixDate("2012-12-21 06:32:12","%Y %b %d %H:%M") %]
    $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";
Re: MySQL Timestamp from CDBI to TT Date Plugin
by cool_jr256 (Acolyte) on Jun 16, 2005 at 14:51 UTC
    I don't know much about handling timestamps withing MySQL but you could do this in perl:
    $stamp="YYYY-MM-DD HH:MI:SS"; ($date,$time)=split(/\ /,$stamp); ($year,$month,$day)=split(/\-/,$date); $newstamp=$time." $day/$month/$year";
    I'm sure there is a module out there which does this...