CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
tz_local_offset but on particular date

 



orange
Novice

Mar 27, 2012, 1:24 AM

Post #1 of 4 (1333 views)
tz_local_offset but on particular date Can't Post

I need a function that will return timezone offset, not current but on particular date.
something like tz_local_offset_date($date)
can someone help me?

edit: or, how to find out the date of last Sunday in March? for eg. summerdate(2012)=25


(This post was edited by orange on Mar 27, 2012, 1:39 AM)


BillKSmith
Veteran

Mar 27, 2012, 7:20 AM

Post #2 of 4 (1315 views)
Re: [orange] tz_local_offset but on particular date [In reply to] Can't Post

I do not understand "timezone offset".

Your second question is answered in the documentation for the module Date::Calc. Search perldoc Date::Calc for


Quote
How do I calculate the last and the next Saturday for any given date?

Good Luck,
Bill


budman
User

Mar 27, 2012, 8:17 AM

Post #3 of 4 (1312 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.



Code
#!/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.


Code
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)


orange
Novice

Mar 28, 2012, 12:14 AM

Post #4 of 4 (1277 views)
Re: [budman] tz_local_offset but on particular date [In reply to] Can't Post

thanks budman, that works beautifully.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives