ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-mod_perl
perl-mod_perl
Re: mod_perl, ENV{'TZ'}, and localtime
by Michael Schout other posts by this author
Dec 28 2007 9:13PM messages near this date
mod_perl, ENV{'TZ'}, and localtime | Re: mod_perl, ENV{'TZ'}, and localtime
Kirk Noda wrote:
>  The thread seemed to die off.  Still, is there a way to use $ENV{TZ} to
>  modify the behavior of localtime?

The reason this does not work under modperl version 2.0 is because under
handler "perl-script", %ENV is untied from the C environment.  The
localtime() function is implemented in C, and as a result, it will never
see the changes you made to $ENV{TZ} from mod_perl.

The way I got around this was to use Env::C, and override
CORE::localtime() with something like:

package MyLocaltime;

use Env::C;

sub import {
    my $class = shift;
    $class-> export('CORE::GLOBAL', 'localtime');
}

sub localtime {
    my $time = shift;
    $time = time unless defined $time;

    my $orig_tz = Env::C::getenv('TZ');
    Env::C::setenv('TZ', $ENV{TZ}, 1);

    my @ret = CORE::localtime($time);

    Env::C::setenv('TZ', $orig_tz, 1);

    return @ret;
}

The real problem is that this is only safe under a prefork MPM because
it is not thread safe.  There really ought to be an option (IMO) where
you can make the untie of %ENV under perl-script to be optional.  Maybe
something like PerlOptions +NoUntieEnv or something so that if you are
running under a prefork MPM, you do not need to resort to tactics like
the above.

Regards,
Michael Schout
Thread:
Kirk Noda
Michael Schout
Kirk Noda

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved