Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to convert time from one time zone to another

by rameshps (Initiate)
on Dec 19, 2005 at 18:47 UTC ( [id://517849]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am working on a hobby project and am looking for an utility/logic that converts time (System Time) and gives the equivalent time of another given timezone. Thanks in advance for help. Regards, Ramesh.
  • Comment on How to convert time from one time zone to another

Replies are listed 'Best First'.
Re: How to convert time from one time zone to another
by tphyahoo (Vicar) on Dec 19, 2005 at 18:54 UTC
    Maybe you could be helped by The Many Dates and Times of Perl. snip: "The definitive source of time zone offsets and rules is the Olson time zone database....."
Re: How to convert time from one time zone to another
by zentara (Archbishop) on Dec 19, 2005 at 19:15 UTC
    use DateTime::TimeZone; #might help

    I'm not really a human, but I play one on earth. flash japh
Re: How to convert time from one time zone to another
by robin (Chaplain) on Dec 19, 2005 at 19:28 UTC
    You can just set the TZ environment variable to the timezone you want, and use localtime:
    { local $ENV{TZ} = "EST+05"; print "In New York, it is ".localtime()."\n"; } { local $ENV{TZ} = "IST-05:30"; print "In Mumbai, it is ".localtime()."\n" }
    The last time I ran it, this printed
    In New York, it is Mon Dec 19 14:25:01 2005 In Mumbai, it is Tue Dec 20 00:55:01 2005
    I think this will work on Windows as well as Unix, but I don't have a Windows box to test it on.

      You should probably use the named timezones instead of numeric pseudo timezones as you have. For example, US/Eastern for New York. Your example will show an incorrect time for New York in the summer.

        You have a good point. There is a frustrating trade-off, in that “numeric pseudo-timezones” are pretty portable. The named (Olson) timezones still won’t work everywhere, unfortunately, although the situation is improving.

        If portability is not a concern, and the Olson names work on your system, you should definitely use them. If you need to be portable and to deal with all the intricacies of daylight saving time, you probably have to use DateTime with DateTime::TimeZone (which parses the Olson database) and install a copy of the Olson database if your system doesn't already have it.

        (Unless there's a better way that I don't know about?)

        Update: Actually I think the module is distributed with a pre-parsed copy of the database, so at least you don't have to worry about that. It's still a lumbering behemoth whose documentation is rather confusing, at least to me; but it should give accurate results at least.

        Actually I don't think the statement (that parent's code will produce an incorrect time during DST) is correct but neither do I think Robin's surmise that the code in Re: How to convert time from one time zone to another will "work on Windows" is universally correct. Under Windows7:
        #!/usr/bin/perl use 5.014; # convert TZ say "\$ENV{TZ}: $ENV{TZ}"; # var had to be manually added on stock + W7 { local $ENV{TZ} = "EST+05"; say "In New York, it is " . localtime(); } { local $ENV{TZ} = "IST-05:30"; # does not return c +orrect time on W7 say "In Mumbai, it is " . localtime(); # but, rather, same + as NY localtime }

        outputs:

        $ENV{TZ}: EST5EDT # TZ is set but this does + # not appear to be defaul +t (for Win7) In New York, it is Mon Apr 2 10:41:23 2012 # correct In Mumbai, it is Mon Apr 2 10:41:23 2012 #obviously wrong

        But this may be the result of a misconception on my part about 'doze ENV and its vars.
            Anyone care to explicate?

Re: How to convert time from one time zone to another
by Anonymous Monk on Apr 02, 2012 at 13:14 UTC
    use Date::Manip or use unix command $ TZ=required_timezone date -d "21:45:56 current_timezone" e.g. $TZ=IST date -d "12:45:46 GMT" $TZ=GMT date -d "12:45:46 +0800 or HKT"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found