Here is my situation.  I use the Rose Framework as my ORM under mod_perl,
fast_cgi, and server_side scripts.  I have the framework set up to use UTC
to store all datetimes.  I do all work in my controllers in UTC time.  The
majority of the time I need a datetime to be converted to a specific
time_zone is when it is being displayed to a user.

Doing the work in parse_datetime does not work for me.  In fact I almost
never call parse_datetime directly and when I do it's to get the datetime
passed in from a form into UTC.  I need/want the time_zone conversion to
also happen within the format_datetime function when necessary.

The idea is that I should be able to have 1 datetime object that has already
be created in UTC and easily show that same datetime object to multiple
users in the pattern and time_zone they prefer.


An Example Using my code from before:

In Controller:
my $dt = $my_rose_object->transaction_date;  # UTC Time
## Do work more queries, etc. all with UTC

In Template using the Strptime Plugin:

[% USE f = DateTime::Format('My::DateTime::Format::Strptime', { pattern =>
"%m/%d/%Y %T %Z",  format_with_time_zone=>1, time_zone => user.time_zone})
%]

[%# The above creates a DateTime Formatter for this user %]

[% f.format(dt) %] [%# Formats dt which is in UTC in the time_zone that is
associated with the user %]


Looking at the code in DateTimeX::Format::POSIX::Strptime the
format_datetime function still only works with the pattern calling strftime
in DateTime, no time_zone conversion is being done.

Does this make sense?

Thanks,
-Kevin

On Mon, Dec 14, 2009 at 10:46 PM, Evan Carroll <m...@evancarroll.com> wrote:

> If I understand you right, I have a framework that does this for you:
> DateTimeX::Format. And, a module for it DateTimeX::Format::Strptime -
> it permits you to override all of the environment in runtime, and it
> doesn't use a perl-rewrite of strptime, instead it uses the POSIX
> binding.
>
>
>        use DateTimeX::Format::Strptime;
>
>        my $dtf = DateTimeX::Format::Strptime({ locale => 'en_US',
> timezone => 'America/Chicago', pattern => $pattern });
>
>        $dtf->parse_datetime( "time" );
>
>        $dtf->pattern( $newPattern );
>
>        ## Call-only pattern won't be cached
>        $dtf->parse_datetime( "time", { pattern => $pattern } );
>
>        $dtf->format_datetime( $dt, $pattern );
>
> Just override the environmnent in the anon hash that is the second
> argument to ->parse_datetime, all DateTimeX::Format modules file the
> same api (there is no current API for other DateTime::Format modules).
>
> --
> Evan Carroll
> System Lord of the Internets
> http://www.evancarroll.com
>

Reply via email to