Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Mod_Perl multiple timezones

by dga (Hermit)
on Jul 23, 2003 at 17:56 UTC ( #277277=note: print w/replies, xml ) Need Help??


in reply to Mod_Perl multiple timezones

The only thing I can think of off hand is the persistance of the mod_perl. I tried this in perl 5.6.0 and it works fine. Is this similar to what you tried in 5.8? ie set TZ print localtime and set TZ and print localtime again in the same script with a different timezone.

$ENV{TZ}='CST6CDT'; print scalar(localtime), "\n"; $ENV{TZ}='MST7MDT'; print scalar(localtime), "\n"; __END__ #output Wed Jul 23 12:52:59 2003 Wed Jul 23 11:52:59 2003

Replies are listed 'Best First'.
Re: Re: Mod_Perl multiple timezones
by dtr (Scribe) on Jul 23, 2003 at 19:14 UTC

    The following script will work beautifully when run from the command line.

    When called as a CGI under mod_perl_2 (1.99.08) it will output identical dates for all the different time zones!

    Is this a known problem???

    #! /opt/perl/bin/perl -w
    
    use strict;
    
    use POSIX;
    
    print
        "Content-type: text/plain\n\n",
        "Time zone stuff\n",
        "***************\n",
        "\n";
    
    my $now = time;
    
    
    $ENV{TZ} = "Europe/London"; POSIX::tzset();
    print "Time in Europe/London is   " . localtime($now) . " +OK\n";
    
    
    $ENV{TZ} = "Europe/Paris"; POSIX::tzset();
    print "Time in Europe/Paris is    " . localtime($now) . " +OK\n";
    
    
    $ENV{TZ} = "America/Chicago"; POSIX::tzset();
    print "Time in America/Chicago is " . localtime($now) . " +OK\n";
    
    
    exit;
    

      I'd guess this has something to do with the lack of any real environment to speak of under mod_perl. I will admit to only being familiar with mod_perl 1, however this does look like a bug of some sort (it works fine under mod_perl 1 using Apache::Registry and perl 5.6.1). To track it down, a few more specifics are needed.

      How do you have mod_perl set up (some bits of your apache config might be useful)? What version of perl is mod_perl using?

      PS -- You say that this is being called "as a CGI under mod_perl," which is a bit of an oxymoron -- either you run programs molasses-slow under CGI, or you're using the accelerated features of mod_perl. You can't be doing both -- from Apache's point of view, either you're going through mod_perl, or through mod_cgi.

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

        Hi Chmrr, you seem to have the answers to all of my problems today! Here's the relevant sections of the httpd.conf file:-
        LoadModule perl_module modules/mod_perl.so
        
        PerlOptions +SetupEnv +ParseHeaders +Parent -GlobalRequest
        PerlSwitches -w -T
        PerlRequire  "/var/www/startup.pl"
        
        Alias /test  /var/www/test
        
        <Directory /var/www/test>
            Options +ExecCGI
            Order allow,deny
            Allow from all
        </Directory>
        
        <Location /test/*.pl>
            SetHandler perl-script
            PerlResponseHandler ModPerl::Registry
        </Location>
        
        
        I've tried this with +Parent and -Parent (I'm trying to support multiple vhosts on this server), and also with +SetupEnv and -SetupEnv.
Re: Mod_Perl multiple timezones
by waldo22 (Novice) on Mar 08, 2011 at 15:36 UTC

    I realize that this thread is older than Perl itself, but it comes up very high in Google, so I thought I'd update.

    The following works perfectly for me in Perl 5.10:

    #!/usr/bin/perl use strict; use warnings; ### US Time Zones #my $time_zone = 'US/Alaska'; #my $time_zone = 'US/Aleutian'; #my $time_zone = 'US/Arizona'; #my $time_zone = 'US/Central'; my $time_zone = 'US/Eastern'; #my $time_zone = 'US/East-Indiana'; #my $time_zone = 'US/Hawaii'; #my $time_zone = 'US/Indiana-Starke'; #my $time_zone = 'US/Michigan'; #my $time_zone = 'US/Mountain'; #my $time_zone = 'US/Samoa'; #my $time_zone = 'US/Pacific'; ### set time zone for Perl to access with localtime ### can be any zone listed in /usr/share/zoneinfo and its subfolders $ENV{TZ} = $time_zone; print "System Time Zone from Perl is $ENV{TZ}\n"; my $timenow = localtime(time); print "Time from System is $timenow\n";

    There was no need to use POSIX::tzset().

    It works well from both the command line and from Apache2.

    -Wes

      One thing you may want to consider - I have been having some problems of my own with mod_perl resulting in my system running out of threads. This has forced me into researching threads in much more depth. One thing I have seen multiple times is that localtime() is not thread safe. While this doesn't seem to explain my issues it might explain yours. You can set the environment all you want, but if another thread comes along the TZ can get set to something else, even while localtime() is running, unless you have done something to lock access during your critical time between setting the TZ and getting a result from localtime().

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://277277]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2021-06-18 05:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (88 votes). Check out past polls.

    Notices?