TimeZone calculation on Windows Vista with DateTime::TimeZone

A

Ami

Hi All,
I am using Windows Vista with ActivePerl 5.8.8.822. I have
downloaded the latest DateTime::TimeZone package from CPAN (DateTime-
TimeZone-0.67). When I write a small script to work with it, I always
get error stating that "Cannot determine local time zone.". After
debugging, I came to know that DateTime package of Perl reads the
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
\TimeZoneInformation\StandardName" to access this information which is
supposed to contain a Windows name for the time zone (e.g.) but in
Windows Vista, it contains some dll entry point information
(@tzres.dll,-492).

The code snippet with which I am trying to work is as follows:

use DateTime::TimeZone;
my $tz = DateTime::TimeZone->new( name => 'local' );
$tz = DateTime::TimeZone::Local->TimeZone();

Can any one help me to make it working on Windows Vista?

Thanks in advance for your help and time.
Regards
 
B

Ben Morrow

Quoth Ami said:
Hi All,
I am using Windows Vista with ActivePerl 5.8.8.822. I have
downloaded the latest DateTime::TimeZone package from CPAN (DateTime-
TimeZone-0.67). When I write a small script to work with it, I always
get error stating that "Cannot determine local time zone.". After
debugging, I came to know that DateTime package of Perl reads the
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
\TimeZoneInformation\StandardName" to access this information which is
supposed to contain a Windows name for the time zone (e.g.) but in
Windows Vista, it contains some dll entry point information
(@tzres.dll,-492).

The code snippet with which I am trying to work is as follows:

use DateTime::TimeZone;
my $tz = DateTime::TimeZone->new( name => 'local' );
$tz = DateTime::TimeZone::Local->TimeZone();

Can any one help me to make it working on Windows Vista?

This is clearly a bug in DateTime::TimeZone, or rather an
incompatibility with Vista. It's probably worth filing a bug report on
rt.cpan.org.

About the only workaround is to pick the appropriate timezone from the
list returned by DateTime::TimeZone->all_names and set %TZ% in the
environment to that value.

Ben
 
A

Ami

Quoth Ami <[email protected]>:







This is clearly a bug in DateTime::TimeZone, or rather an
incompatibility with Vista. It's probably worth filing a bug report on
rt.cpan.org.

About the only workaround is to pick the appropriate timezone from the
list returned by DateTime::TimeZone->all_names and set %TZ% in the
environment to that value.

Ben

Hi Ben,
Many thanks for your reply but being a new comer to Perl, I am not
sure how can I choose the appropriate timezone from array returned
from all_names function. This array consists of list of all time
zones. How can I determine which time zone my machine is using and set
the %TZ% in environment variable. To try it with hardcode, i used
following code in below given code:

use Time::Local;
my @dayofweek = (qw(Sunday Monday Tuesday Wednesday Thursday Friday
Saturday));
my @monthnames = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
Dec));
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday);
$ENV{TZ}='Asia/Calcutta';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =
localtime($TimeInSeconds);
$year += 1900;
print "This date is $dayofweek[$wday], $monthnames[$mon] $mday, $year
\n";
print "This time is $hour:$min:$sec\n";

and same code with
$ENV{'TZ'}='+530';

but it gives me following date/time:
This date is Thursday, Jan 1, 1970
This time is 5:30:0

Please could you give me some code example to make it working?
Thanks for your help.
Regards,
 
A

Ami

Quoth Ami <[email protected]>:







This is clearly a bug in DateTime::TimeZone, or rather an
incompatibility with Vista. It's probably worth filing a bug report on
rt.cpan.org.

About the only workaround is to pick the appropriate timezone from the
list returned by DateTime::TimeZone->all_names and set %TZ% in the
environment to that value.

Ben

Hi Ben,
Many Thanks for your reply.But being a new bee to perl, I am not
sure how can retrieve the current time zone from the list returned by
all_names() function. What should be the condition to match the
current time zone from list?
How can I set the TZ in ENV variable. I tried with
$ENV{'TZ'}='Asia/Calcutta';
and
$ENV{TZ}='America/Chicago';
All time I get same date/time;

The script used is as follows:
use Time::Local;
my @dayofweek = (qw(Sunday Monday Tuesday Wednesday Thursday Friday
Saturday));
my @monthnames = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
Dec));
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday);
$ENV{TZ}='America/Chicago';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime();
$year += 1900;
print "This date is $dayofweek[$wday], $monthnames[$mon] $mday, $year
\n";
print "This time is $hour:$min:$sec\n";

It would be great help, if i can get some code snippet to set the TZ
correctly to get correct time according to zones.
Thanks
 
B

Ben Morrow

Quoth Ami said:
Many Thanks for your reply.But being a new bee to perl, I am not
sure how can retrieve the current time zone from the list returned by
all_names() function. What should be the condition to match the
current time zone from list?

I'm afraid I don't know. The correct answer is to call the Win32 API
function GetTimeZoneInformation or GetDynamicTimeZoneInformation, but
while it would be possible to do this using Win32::API it's not
something I can construct an example of off the top of my head. I think
for now you're just going to have to set it manually.
How can I set the TZ in ENV variable. I tried with
$ENV{'TZ'}='Asia/Calcutta';
and
$ENV{TZ}='America/Chicago';
All time I get same date/time;

You probably need to set $ENV{TZ} in a BEGIN block before you load
DateTime::TimeZone, something like

BEGIN { $ENV{TZ} = 'Asia/Calcutta' }

use DateTime::TimeZone; # or Time::Local, or whatever

You could also set the variable in the 'real' environment, so it's
always present.

Ben
 
A

Ami

Quoth Ami <[email protected]>:





I'm afraid I don't know. The correct answer is to call the Win32 API
function GetTimeZoneInformation or GetDynamicTimeZoneInformation, but
while it would be possible to do this using Win32::API it's not
something I can construct an example of off the top of my head. I think
for now you're just going to have to set it manually.


You probably need to set $ENV{TZ} in a BEGIN block before you load
DateTime::TimeZone, something like

BEGIN { $ENV{TZ} = 'Asia/Calcutta' }

use DateTime::TimeZone; # or Time::Local, or whatever

You could also set the variable in the 'real' environment, so it's
always present.

Ben

Hi Ben,
Thanks for your reply. I have tried using
BEGIN { $ENV{TZ} = 'Asia/Calcutta' }
and than
BEGIN { $ENV{TZ} = 'America/Chicago' }
before loading DateTime::TimeZone but i find no impact. It shows
current PC time on using localtime() function. Setting the environmnet
variable TZ to different values also returns same value.
Any other idea, what i might be doing wrong?
Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top