
budman
User
Mar 27, 2012, 8:17 AM
Post #3 of 4
(982 views)
|
|
Re: [orange] tz_local_offset but on particular date
[In reply to]
|
Can't Post
|
|
There was a module on CPAN, DateTime::TimeZone that may be what your looking for.
#!/usr/bin/perl use strict; use warnings; use DateTime; use DateTime::TimeZone; my @dates = qw(20100815 20101120 20110217 20110320); my $time_zone = 'America/Montreal'; my %tz_opts = (time_zone => $time_zone); my $tz = DateTime::TimeZone->new(name=>$time_zone); foreach my $date (@dates) { @tz_opts{'year','month','day'} = $date =~ m/(\d{4})(\d\d)(\d\d)/; my $dt = DateTime->new(%tz_opts); my $offset = $tz->offset_for_datetime($dt); print "$date offset is $offset\n"; } Output: 20100815 offset is -14400 20101120 offset is -18000 20110217 offset is -18000 20110320 offset is -14400 Works with 30 minute timezones as well: Time Zone: America/St_Johns 20100815 offset is -9000 20101120 offset is -12600 20110217 offset is -12600 20110320 offset is -9000 Time Zone: America/Halifax 20100815 offset is -10800 20101120 offset is -14400 20110217 offset is -14400 20110320 offset is -10800 There are several Data Time modules, so there may be others that might help. This one seems to be what you are looking for. http://search.cpan.org/~drolsky/DateTime-TimeZone-1.45/lib/DateTime/TimeZone.pm Edit: I checked Date::Calc, you can get the offset as well, however, you are limited to the localtime timezone. From what I can see in the docs, the timezone cannot be specified.
use strict; use warnings; use Date::Calc qw(Date_to_Time Timezone); my @dates = qw(20100815 20101120 20110217 20110320); foreach my $date (@dates) { my ($year,$month,$day) = $date =~ m/(\d{4})(\d\d)(\d\d)/; my $time = Date_to_Time($year,$month,$day,0,0,0); my ($D_y,$D_m,$D_d, $Dh,$Dm,$Ds, $dst) = Timezone($time); my $offset = $Dh * 3600 + $Dm * 60 + $Ds; print "$date offset is $offset Daylight Savings: $dst\n"; } Output: 20100815 offset is -14400 Daylight Savings: 1 20101120 offset is -18000 Daylight Savings: 0 20110217 offset is -18000 Daylight Savings: 0 20110320 offset is -14400 Daylight Savings: 1
(This post was edited by budman on Mar 27, 2012, 9:10 AM)
|