[Catalyst] Setting time zone and locale on dates.

Pedro Melo melo at simplicidade.org
Tue May 22 10:20:26 GMT 2007


Hi,

On May 21, 2007, at 9:54 PM, Bill Moseley wrote:

> This likely falls into one of the many "how do I get $c into the
> model" threads.
>
> My session has the locale and timezone fetched from the user's
> preferences at login.  It's not really locale, but a language tag
> which is also used by Locale::Maketext, but should work for DateTime's
> locale setting.
>
> I use timestamp with time zone on my Postgresql timestamps and the
> model inflates to DataTime objects.  Most if not all dates should be
> localized for the user.
>
> I'm tempted to use model class data to pass this to the model.  So at
> the start of every request do something like:
>
>     My::Model->current_timezone( $c->session->{timezone} );
>     My::Model->current_locale( $c->session->{locale} );
>
> And then have the model use those (if set) when inflating.
>
> Any better ideas for dealing with this?
>
> I ask because (besides not having that much I18N experience) I
> think this is something that really belongs in the view.
>
>     [% c.local_date( user.last_updated_time ) | html %]
>
> But, setting the locale and time_zone in the inflator would be handy
> since it's all one place instead of every time I use a date in the
> view.
>
> What do you do?

I always do my model datetime stuff in UTC.

Formatting according to user prefs belongs in the view, IMHO.

I understand your wish to format in only one place, but IMHO, thats  
whar BLOCKs or PROCESSed files are for. Just do a date.tt and use it  
every where. Or write a Template::Plugin that takes in account your  
user preferences and just do [% my.date_field | user_pref_date %]

Best regards,
-- 
Pedro Melo
Blog: http://www.simplicidade.org/notes/
Jabber ID: melo at simplicidade.org
Use Jabber!





More information about the Catalyst mailing list