[Catalyst] New Bundle::Catalyst
Brandon Black
blblack at gmail.com
Wed Nov 16 07:16:32 CET 2005
On 11/15/05, Bill Moseley <moseley at hank.org> wrote:
> On Tue, Nov 15, 2005 at 08:07:09AM -0600, Brandon Black wrote:
> > The most compelling one was to use some data from $c->session in
> > choosing the timezone of DateTime objects being inflated/deflated from
> > time columns in the database. I haven't found a solution I'm really
> > happy with for this one yet, but currently I just have an unrelated
> > utility function in lib/MyApp/Utils.pm to display DateTime objects
> > using a timezone derived from $c->session, and I have to wrap usage of
> > my timestamp columns in that function ( my $string_for_html_output =
> > MyApp::Utils::format_for_user($c, $row->time_stamp) ).
>
> I use an inflate that looks up the timezone in a related record. So
> maybe you could shove the timezone into the database where the inflate
> method could find it. %ENV would probably be just as good.
>
> Anyway here's what I do:
>
> inflate => sub {
> my ($value, $item) = @_;
>
> my $dt = DateTime::Format::Pg->parse_datetime( $value );
> $dt->set_formatter( $format );
>
> $dt->set_time_zone( $item->location->time_zone->name );
> $dt;
> }
>
My timezones are not properties of (or related to) the stored
timestamps. All my timestamp columns are basically in UTC, and users
choose what personal timezone perspective they wish to see the data
from.
Ultimately the information which determines the desired timezone
(which is a per-user preference potentially overridden by a
per-session temporary preference) is obtainable straight from the
database, since my users and sessions are in database tables - but
still it is user/session specific, and without the Catalyst context, I
don't see how I would go about determining which user/session's
timezone preference to look up in the tables directly.
> Another ugly hack you might be able to do is at the start of your
> request you replace the inflate sub in your classes.
>
> Untested and maybe way off, but a wild guess might be:
>
>
> Class->meta_info('has_a', $method )->args->{inflate} = sub {
> my $dt = DateTime::Format::Pg->parse_datetime( shift );
> $dt->set_time_zone( $c->session->{timezone} );
> return $dt;
> };
>
>
That's certainly interesting - I hadn't considered that angle before.
It's promising, but I think someone else working on my project down
the road may come find me and shoot me for doing that when they see it
:)
As alluded to in a seperate post I just made a little while ago, since
that post you responded to, the least hackish way I've found to do it
so far is to use Catalyst::Singleton and reference
MyApp->context->session->{timezone} directly in the inflate/deflate
code back at Model initialization time. I'm just not really clear on
whether Singleton was meant to be used from a Model like that, whether
it's a good idea at all, or whether there might be an ever better way
to go about this.
-- Brandon
More information about the Catalyst
mailing list