Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
laziness, impatience, and hubris
 
PerlMonks  

Difficult on use time

by exilepanda (Monk)
on Apr 07, 2012 at 14:07 UTC ( #963918=perlquestion: print w/ replies, xml ) Need Help??
exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a problem on using time, please advise. But in short, how can I force Perl to PERMANENTLY consider I am working in GMT+0 location? ( besides from change my system timezone )

I am in location GMT+8, and work with Win32. Then, when I call localtime(0), It's actually point to Jan 1, 1970, 08:00 in local time.

In some cases, I'll use gmtime($random_second) to have the time data array which based from Jan 1, 1970, 00:00. (eg. for time compare )

But in other often cases, I have to use know system time ( eg. for logging )I'll call localtime(time).

However, when things are mixed together to run, I am really very headache to decide when to localtime and when to gmtime, in a same package.

Anyway, perhaps I cannot express well either from above, but what concerns me is simply the question asked at the beginning. I just want a constant way that I can relay on, without want to check against which timezone I am in, but always correct.

UPDATE at Apr 09, 01:32 GMT+8
After server replies, seems my given example has side tracked monks around. Deep sorry about this. I would take chance to highlight that, my question is "how can I force Perl to PERMANENTLY consider I am working in GMT+0 location?" This means and implies, gmtime and locatime will return me the same result; OR same effect as I change my system timezone to GMT+0.

Comment on Difficult on use time
Select or Download Code
Re: Difficult on use time
by kennethk (Monsignor) on Apr 07, 2012 at 14:26 UTC

    In general, when I need to do any sort of interaction with dates and times, and especially if I need to do math, I use the DateTime module. It handles all this in a clean, consistent way.

    If you are disinclined to go outside built-ins, I would suggest using GMT for all your times rather than mixing. You can then either log in GMT, or do the conversion on output. Playing games with differing internal representations just creates unnecessary headaches in my experience. In this case, just consistently calling gmtime will "force Perl to PERMANENTLY consider I am working in GMT+0 location", rather than creating a strange hack that will confuse you next time you have to work with this module and may possibly cause messy action at a distance.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      That's right, if I all use gmtime or localtime, I can have a constant base, ( but believe this is not same as working a system in a GMT+0 TZ ). And for me, both has its drawback. If all gmtime, I might not reflecting to the real time to my local system; if I hard code the conversion, I lost the portability if the system moves to other time zone. And if all localtime, it sometimes surprised me like if I use Date_to_Time in Date::Calc (this returns me a gmt+0 seconds)...

      DateTime is pretty good enough for conditions, however it complexity increased the interpret time and maintenance effort. Though I really use them them in some real difficult calculation. But for something just like :

      doSomething() if ( time == $triggerSecond );
      I really don't want to call a module

      However, thank you for your reminder! I think I should consider to use DateTime for now, which could be a nice tools I've forgotten when I am dealing with this package.

Re: Difficult on use time
by ikegami (Pope) on Apr 07, 2012 at 21:31 UTC

    In the directory identified by

    perl -V:sitelib

    Add the following to sitecustomize.pl

    require POSIX; $ENV{TZ} = 'UTC'; POSIX::tzset(); 1;

    (Create it if it doesn't exist.)

      Thanks for introducing me the sitecustomize.pl. This is new for me to notice! However, could you explain a little for what purpose of this work is supposed to be done ? Though, still can't do the trick I want. what I would expect is :
      $tm = time; @gmt = gmtime ( $tm ) ; @local = localtime ( $tm ); # and @gmt should identical to @local
      This could happen if I change my system tz to GMT+0

        They are identical

        $tm=time; print scalar(gmtime($tm)), "\n"; print scalar(localtime($tm)), "\n"; require POSIX; $ENV{TZ} = 'UTC'; POSIX::tzset(); print scalar(gmtime($tm)), "\n"; print scalar(localtime($tm)), "\n";
        Sun Apr 8 21:29:55 2012 Sun Apr 8 17:29:55 2012 Sun Apr 8 21:29:55 2012 Sun Apr 8 21:29:55 2012
Re: Difficult on use time
by Marshall (Prior) on Apr 08, 2012 at 04:48 UTC
    My advice is to log everything in GMT (UTC) and use GMT for all of your calculations. Standardize upon that. Use one of the Date/Time modules to convert to local time for presentation.

    if I hard code the conversion, I lost the portability if the system moves to other time zone.
    store all the data as UTC, convert when necessary for user presentation.

      Thanks for the advise! For my development scope, I don't see the necessity to maintain 2 time manner. I could change my system timezone for this situation as last resort. As I do not need the timezone auto convert in this way. However, I am looking if there's any Perl way can do this.

      For more real case, I do not hesitate to do the conversation, but rather reserve the timezone for public users' own tz for their own location( not my system ). Writing log or so is just one example, out of many scenarios.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963918]
Approved by kennethk
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: (14)
As of 2014-04-18 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls