Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Localtime() or gmtime()

by NetStorm (Initiate)
on Oct 14, 2002 at 20:57 UTC ( [id://205214]=perlquestion: print w/replies, xml ) Need Help??

NetStorm has asked for the wisdom of the Perl Monks concerning the following question:

hi, i'm new in perl. Here is my problem. I want to change the time zone for the localtime(). The server is using standart pacific time, but i want to return the time in East European Time, how could i change the time zone????? any suggestions

Replies are listed 'Best First'.
Re: Localtime() or gmtime()
by swiftone (Curate) on Oct 14, 2002 at 21:18 UTC
    Some quick experimentation on my system (Debian Linux) shows that changing $ENV{TZ} changes the results of localtime. I found some documentation at http://www.aota.net/Script_Installation_Tips/perltime.php4, and Googling for "perl $ENV{TZ}" will get you quite a bit of info.

    Hope that helps.

Re: Localtime() or gmtime()
by Flexx (Pilgrim) on Oct 15, 2002 at 00:52 UTC

    Hi,

    Altering your $ENV{TZ}, for a very quick hack could do. A local $ENV{TZ} = 'EET' # EET? is a bit better.

    But beware (know what you do)! This is potentially dangerous, if you use qx// or [system]() calls, require other perl scripts/use modules, do IPC (interact with other running processes), have changing configuration (see below), etc.

    In a production script/environment you'll want to use a Date/Time manipulation module, like the excellent Date::Manip or Date::Calc. These will do what you need (and more), safely with regard to daylight saving times and/or configuration changes. Imagine what happens if your servers' sysop one day decides he'd switch his servers to use UTC as their RTC's timezone. These modules will return the correct date, simply adding the n hours offset will fail in such a case...

    Times are just data. Operate on it. If you want to know what time it is in Zimbabwe, you'd calculate the difference to your local time -- you wouldn't reset your wristwatch (i. e. change your configuration) to see what the time would be (I hope ;)...

    Hope this helps a bit,

    so long,
    Flexx

      Imagine what happens if your servers' sysop one day decides he'd switch his servers to use UTC as their RTC'S timezone. These modules will return the correct date, simply adding the n hours offset will fail in such a case...

      Times are just data. Operate on it. If you want to know what time it is in Zimbabwe, you'd calculate the difference to your local time

      So, what is it? Can you, or can't you calculate the difference? First you say you can't, then you say you can.

      Also, how would Date::Manip or Date::Calc continue to return "the correct date" when the server switches timezones (wrongly apparently, otherwise localtime() would return the correct time).

      Abigail

Re: Localtime() or gmtime()
by nothingmuch (Priest) on Oct 14, 2002 at 21:16 UTC
    perhaps this is a bit makeshift, but have you considered altering the return from time() to add/deduct the correct number of hours and then apply localtime()?

    i am not sure, but i think pacific and east euro are 10 hours apart, so that should be

    localtime(time() - 10 * 60 * 60);

    actually changing the time zone is very platform specific, and you probably want to ask your sysop instead of the people here.

    Perhaps the Time::Local module (standard distribution) will also be of some use to you.

    -nuffin zz zZ Z Z #!perl
      Of course, it isn't as simple as that. The main problem is daylight savings time. The world doesn't switch at the same time - or does the Pacific coast switch to DST in the middle of the day? ;-) DST happens at different dates, different times, and with different differences in different timezones, and some zones don't switch at all.

      On Unix, you can often get the time in a different zone by setting the TZ environment variable properly.

      Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://205214]
Approved by adrianh
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-26 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found