TimeZone calculation on Windows Vista with DateTime::TimeZone

Discussion in 'Perl Misc' started by Ami, Sep 20, 2007.

  1. Ami

    Ami Guest

    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
    Ami, Sep 20, 2007
    #1
    1. Advertising

  2. Ami

    Ben Morrow Guest

    Quoth 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?


    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
    Ben Morrow, Sep 20, 2007
    #2
    1. Advertising

  3. Ami

    Ami Guest

    On Sep 21, 3:36 am, Ben Morrow <> wrote:
    > Quoth 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?

    >
    > 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,
    Ami, Sep 21, 2007
    #3
  4. Ami

    Ami Guest

    On Sep 21, 3:36 am, Ben Morrow <> wrote:
    > Quoth 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?

    >
    > 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
    Ami, Sep 21, 2007
    #4
  5. Ami

    Ben Morrow Guest

    Quoth Ami <>:
    > On Sep 21, 3:36 am, Ben Morrow <> wrote:
    > > Quoth 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?

    > >
    > > 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.

    >
    > 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
    Ben Morrow, Sep 21, 2007
    #5
  6. Ami

    Ami Guest

    On Sep 21, 9:35 pm, Ben Morrow <> wrote:
    > Quoth Ami <>:
    >
    >
    >
    > > On Sep 21, 3:36 am, Ben Morrow <> wrote:
    > > > Quoth 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?

    >
    > > > 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.

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


    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
    Ami, Sep 24, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    690
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    523
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    377
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    329
    Martin
    Dec 27, 2008
  5. Justin Voelker
    Replies:
    2
    Views:
    254
    Dr J R Stockton
    Dec 2, 2008
Loading...

Share This Page