[CDBI] CDBI and Class::Date

Arshavir Grigorian grigorian at gmail.com
Wed Dec 12 10:56:42 GMT 2007

On Dec 6, 2007 1:24 AM, Arshavir Grigorian <grigorian at gmail.com> wrote:

> Hi,
> I have a db field of type "timestamp with time zone" (Postgres). I
> also have a CDBI class for that table which uses Class::Date, like so:
> __PACKAGE__->has_a (last_update_dt => 'Class::Date',
>                    inflate => sub {
>                        $Class::Date::DATE_FORMAT = "%a - %b %d, %Y %H:%M";
>                        Class::Date->new(shift) },
>                    deflate => 'string');
> Regardless of what the db connection's timezone is set to, the
> Class::Date object is created with tz UTC, which is incorrect since
> the date returned is in each user's timezone. To remedy this, I do
> $new_date = $db_row->last_update_dt->clone(tz => $correct_tz);
> then
> $gmt_date = $new_date->to_tz('UTC')
> The main issue though is that calling to_tz('UTC') has no effect and I
> am not sure why. When Dump-ing $gmt_date date, the timezone is UTC,
> but everything else is the same as in $new_date. However, if instead
> of reading the date from the database, I use Class::Date::now(),
> things work as advertised, but there is something wrong with operating
> on dates from the database.
> Ultimately, I would like to calculate the difference in minutes
> between the date/timestamp in the database and now, which I think is
> best done when all timestamps are in UTC.
> I would appreciate any explanation for this weird behavior.

Here is more information if that helps.I tried setting/hardcoding the
timezone in the code explicitly but still no go. The object is still created
as UTC.

__PACKAGE__->has_a (last_update_dt => 'Class::Date',
                   inflate => sub {
                       $Class::Date::DATE_FORMAT = "%a - %b %d, %Y %H:%M";
                       Class::Date->new(shift, 'PST') }, # also tried
                   deflate => 'string');

I checked $ENV{'TZ'} and it was not set. Since all my users can have
different timezones, I set the appropriate timezone on the database
connection before handling each request and I would ideally like to use that
time zone when creating the Class::Date object.

By the way, couldn't subscribe to the Class::Date mailing list but the
website seems to be out of commission.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digitalcraftsmen.net/pipermail/classdbi/attachments/20071212/ff37db9c/attachment.htm 

More information about the ClassDBI mailing list