Question

I'm using Perl with Catalyst framework, DBIx class as ORM, TT for views, Postgresql as DB.

I have a column with 'timestamp without timezone' type, and if I do manual query in Postgres the column value is in such format 2012-08-30 21:30:14, but when I print the value in TT view file I get it like this 2012-08-30T15:03:13, so obviously it gets formatted but by what exactly I can't tell.

I want to use Template::Plugin::Date to format output timestamps, but I get Catalyst error:

Couldn't render template "xxx/list.tt2: date error - bad time/date string: expects 'h:m:s d:m:y' got: '2012-08-30T21:28:22'"

with code in xxx/list.tt2 [% date.format(xxx.created) %]

So how am I supposed to make it work? Thanks in advance.

Was it helpful?

Solution

You configured DBIx::Class to inflate the value of the 'created' column to a DateTime object.

Note that you only need to load the TimeStamp component which is based on InflateColumn::DateTime, not both!

Furthermore you should add on_connect_call => 'datetime_setup' to your DBIx::Class connect_info to make DBIC set the database datetime format to match what it expects for the DateTime object inflation. This does the right thing for every supported database so it will also work if you switch database or use SQLite for testing.

Template::Plugin::Date is not for handling DateTime objects, Template::Plugin::DateTime is.

Regarding the template rendering I suggest you use Catalyst::View::TT#expose_methods feature by adding a method to your Catalyst::View::TT view which gets passed a DateTime object and returns a formatted string. You can add multiple methods for different formats, for example date + time, date only etc. This way you have a central location which defines the DateTime formatting which can be easily changed if needed.

OTHER TIPS

This one should work:

[% date.format(xxx.created.strftime('%Y-%m-%d %H:%M:%S')) %]

Don't use the Date::Format function of Template Tookit, it doesn't work very well. Fortunately your DBIx::Class object can call strftime directly.

To fix your code example:

[% xxx.created.strftime( '%Y-%m-%d %R:%S' ) %]

For other formatting read the strftime manpage.

If you need to repeat the reformatting you might want to use a macro like this:

[% MACRO disp_dt(dt) BLOCK; dt.strftime( '%Y: %B %d' ) ; END %]

[% disp_dt( xxx.created ) %]

Thanks to MST for helping me with this on IRC.

date.format(xxx.created.replace('T', ' '));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top