If you have the experience of working with different time zone, writing scripts that generates the time that matches with your local time is something that you might have come across.

Well, here’s one perl module which is just what you might be looking for.


Use Time::Zone
Use Date::Format
Use Date::Parse

### Get our local time, MALAYSIA time.
$my_time = time()

### First we need to make the time into the Merridiane Green Which blab la bla ISO zone
### So since our zone is HK, we need to minus HK time, to bring the time to MGT
$hk_time = tz_offset(‘hkt’);
$mg_time = $my_time - $hk_time;

### Now, add back the time to US time, and TA DA, we get US time
$us_time = tz_offset(‘ewt’) + $mg_time;

### But that time is in number format, just like our unix atime command
### Let’s turn it back to make it readable
$str = time2str( "%a %Y-%m-%d %H:%M:%S %Z", $us_time)

Print $str;

For a full list of supported time zone abbreviation, u can read the source file of the Time::Zone (which was what I did ;p)

You can just read the module file by typing this in your terminal :-

%perldoc –m Time::Zone

Or for brevity, here’s it:-

        my @Zone = (
            "gmt"       =>   0,          # Greenwich Mean
            "ut"        =>   0,          # Universal (Coordinated)
            "utc"       =>   0,
            "wet"       =>   0,          # Western European
            "wat"       =>  -1*3600,     # West Africa
            "at"        =>  -2*3600,     # Azores
            "fnt"       =>  -2*3600,     # Brazil Time (Extreme East - Fernando Noronha)
            "brt"       =>  -3*3600,     # Brazil Time (East Standard - Brasilia)
        # For completeness.  BST is also British Summer, and GST is also Guam Standard.
        #   "bst"       =>  -3*3600,     # Brazil Standard
        #   "gst"       =>  -3*3600,     # Greenland Standard
        #   "nft"       =>  -3*3600-1800,# Newfoundland
        #   "nst"       =>  -3*3600-1800,# Newfoundland Standard
            "mnt"       =>  -4*3600,     # Brazil Time (West Standard - Manaus)
            "ewt"       =>  -4*3600,     # U.S. Eastern War Time
            "ast"       =>  -4*3600,     # Atlantic Standard
            "est"       =>  -5*3600,     # Eastern Standard
            "act"       =>  -5*3600,     # Brazil Time (Extreme West - Acre)
            "cst"       =>  -6*3600,     # Central Standard
            "mst"       =>  -7*3600,     # Mountain Standard
            "pst"       =>  -8*3600,     # Pacific Standard
            "yst"       =>  -9*3600,     # Yukon Standard
            "hst"       => -10*3600,     # Hawaii Standard
            "cat"       => -10*3600,     # Central Alaska
            "ahst"      => -10*3600,     # Alaska-Hawaii Standard
            "nt"        => -11*3600,     # Nome
            "idlw"      => -12*3600,     # International Date Line West
            "cet"       =>  +1*3600,     # Central European
            "mez"       =>  +1*3600,     # Central European (German)
            "ect"       =>  +1*3600,     # Central European (French)
            "met"       =>  +1*3600,     # Middle European
            "mewt"      =>  +1*3600,     # Middle European Winter
            "swt"       =>  +1*3600,     # Swedish Winter
            "set"       =>  +1*3600,     # Seychelles
            "fwt"       =>  +1*3600,     # French Winter
            "eet"       =>  +2*3600,     # Eastern Europe, USSR Zone 1
            "ukr"       =>  +2*3600,     # Ukraine
            "bt"        =>  +3*3600,     # Baghdad, USSR Zone 2
        #   "it"        =>  +3*3600+1800,# Iran
            "zp4"       =>  +4*3600,     # USSR Zone 3
            "zp5"       =>  +5*3600,     # USSR Zone 4
        #   "ist"       =>  +5*3600+1800,# Indian Standard
            "zp6"       =>  +6*3600,     # USSR Zone 5
        # For completeness.  NST is also Newfoundland Stanard, and SST is also Swedish Summer.
        #   "nst"       =>  +6*3600+1800,# North Sumatra
        #   "sst"       =>  +7*3600,     # South Sumatra, USSR Zone 6
        #   "jt"        =>  +7*3600+1800,# Java (3pm in Cronusland!)
            "wst"       =>  +8*3600,     # West Australian Standard
            "hkt"       =>  +8*3600,     # Hong Kong
            "cct"       =>  +8*3600,     # China Coast, USSR Zone 7
            "jst"       =>  +9*3600,     # Japan Standard, USSR Zone 8
            "kst"       =>  +9*3600,     # Korean Standard
        #   "cast"      =>  +9*3600+1800,# Central Australian Standard
            "east"      => +10*3600,     # Eastern Australian Standard
            "gst"       => +10*3600,     # Guam Standard, USSR Zone 9
            "nzt"       => +12*3600,     # New Zealand
            "nzst"      => +12*3600,     # New Zealand Standard
            "idle"      => +12*3600,     # International Date Line East
        );

  2 Responses to “Finding Time On Different Time Zone With Perl”

  1. [...] the article here: Finding Time On Different Time Zone With Perl AKPC_IDS += "2058,";Popularity: unranked [...]

  2. Sorry Lionel you silly azn, you’re code doesn’t work.
    tz_offset returns a non-scalar for starters. Azn quality at work again!

 Leave a Reply

(required)

(required)


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   

Visitors Since Nov 2009

© 2012 Lionel's Blog Suffusion theme by Sayontan Sinha