format DATE & TIMESTAMP columns

2 messages Options
Embed this post
Permalink
Alex-26

format DATE & TIMESTAMP columns

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)

Hi all!

 

I’m displaying a view on a web page. The view contains a timestamp and a date field. The database is MySQL 5.0.x.

 

As raw values from timestamp fields are no eye candy, I decided to use DBIx::Class::InflateColumn::DateTime, using its default formatter to format the timestamp. Unfortunately, it formats the date column, too. But as the date column has no time information, I get something like “YYYY-MM-DDT00:00:00”. I don’t want the time information.

 

Usually, I would format the date and timestamp within the sql statement, but I wanted to try to format it in the ResultClass. So, how do I do that?

 

There is a handicap tough. Sure, I could iterate over each item of the resultset and call some formatting routine for the date field. But I’m too lazy to do that, because it looks a lot simpler to simply give the resultset to the template (bzw, using HTML::Template::Compiled) and let the template handle the output.

 

Here are some sniplets:

# code:

my $rs = $schema->resultset('View')->search(…);

$template->param(view_loop => $rs);

 

# template:

<TMPL_LOOP view_loop>

<TMPL_VAR column>

</TMPL_LOOP>

 

So, any suggestions?


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Octavian Râşniţă-2

Re: format DATE & TIMESTAMP columns

Reply Threaded More More options
Print post
Permalink
From: "Alex" <[hidden email]>

> Hi all!
>
>
>
> I'm displaying a view on a web page. The view contains a timestamp and a
> date field. The database is MySQL 5.0.x.
>
>
>
> As raw values from timestamp fields are no eye candy, I decided to use
> DBIx::Class::InflateColumn::DateTime, using its default formatter to
> format
> the timestamp. Unfortunately, it formats the date column, too. But as the
> date column has no time information, I get something like
> "YYYY-MM-DDT00:00:00". I don't want the time information.
>
>
>
> Usually, I would format the date and timestamp within the sql statement,
> but
> I wanted to try to format it in the ResultClass. So, how do I do that?

I use to do it in the template, but I use TT which can do more than H:T:C
using:

At the top of the template:
[% MACRO d(date) BLOCK; date.set_locale(lang).strftime('%e %b %Y');END -%]

Then in the template I just need to use

[% d(date_field) %]

which formats the date depending on the pattern I used in the d() macro at
the top of the template.
(lang is a variable that holds the language name - 'en', 'fr'...)

You can apply this in the program too, using:

my $formatted_date = $date_field->strftime('the format you want your date to
be');

or also specify a locale:

my $formatted_locale_date = $date_field->set_locale('ro')->strftime('the
format you want');

Or you can apply any method from the DateTime class like:

my $localized_month_name = $date_field->set_locale('ro')->month_name;

HTH.

Octavian


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...