[tpm] TZ code to hour offset conversion ideas?

Mike Ashton Mashton at 4All.com
Wed Oct 8 19:38:32 PDT 2008


I do a lot of world time calculations. I would not use timezone codes 
like est, cst or pst since they are not unique and do not give you 
enough granularity for reliable date calculations.
An example is CST: - Central Std Time (-6) North America ( some 
locations do not observe DST )
        - China Std Time (+8)
        - Central Std Time (+9.5) Australia ( some locations do not 
observe DST )
Also over time, the borders that define these timezones have 
occasionally moved, take a look at Indiana, there are 8 different rules 
based on the movement of Central and Eastern time definitions and 
changing observance or non observance of DST.

Especially if you need to do historical calculations, like in your 
example, to do this accurately you need to use the Olson database. There 
are reason's why there is a America/Toronto and America/Montreal since 
historically they did not observe the same time rules (before standard 
timezones, time was set to the real time of major cities train station 
observed time) and they did not always observe DST or observe at the 
same time.

In perl use DateTime::TimeZone it will make your life a lot easier in 
the long run. For lots of info go to datetime.perl.org

Here is part of an example program I wrote, reading a file with some 
locales ( eg. america/toronto) in it and calculating the current UTC 
offset and writing them back out.

#!/usr/bin/perl -w

use DateTime;
use DateTime::TimeZone;
use Text::Delimited;

## read in the timezones from the system and generate a text ile with 
all current UTC offsets

my $infile = 'timezone.db';
my $outfile = 'timezone_out.db';

my $file = new Text::Delimited;
$file->Open($infile) or die print 'Cant open file';

open (OUTFILE,"> $outfile");
print OUTFILE "Locale\tOffset\n";

my @header = $file->Fields;

while ( my $row = $file->Read ) {
  local ($tz,$offset);
  $tz = DateTime::TimeZone->new( name => $row->{Locale} );
  local ($tz,$offset);
  $tz = DateTime::TimeZone->new( name => $row->{Locale} );
  $utc = DateTime->now(time_zone=>'UTC');
  $offset = $tz->offset_for_datetime($utc)/(60*60);
  print OUTFILE "$row->{Locale}\t$offset\n";
close OUTFILE;



Hope this helps.


Madison Kelly wrote:
> Hi all,
>   I want to create a simple ... "library"? ... to provide data for a 
> simple method do say:
> my $offset=&get_tz_offset("est", "1998-06-15");
>   I've got the TZ data from: http://www.twinsun.com/tz/tz-link.htm
>   It would need to be parsed though, and this I am hoping I can avoid 
> by downloading an existing parser (I am not too concerned how this 
> parser actually dumps data, I can hack that up). I just need something 
> like: "For TZ X in date Y, the offset is Z".
>   I am not sure if I will need the geographic data, as I am not trying 
> to determine what the TZ is, that's given to me. I just need the 
> offset for the given date.
> Any suggestions? Should I start hacking up a parser myself? Do I ask 
> too many questions? :)
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20081008/50008ca5/attachment-0001.html>

More information about the toronto-pm mailing list