houseabsolute / DateTime-TimeZone

Time zone object base class and factory
https://metacpan.org/release/DateTime-TimeZone/
Other
9 stars 25 forks source link

Error while interrogating a timezone #50

Closed japharl closed 2 years ago

japharl commented 2 years ago

I'm attempting to look at all of the time zone offstes for a given country.

my @list = DateTime::TimeZone->names_in_country($code);
foreach my $e (@list){
  my $tz = DateTime::TimeZone->new(name=>e);
  my $offset = %tz->offset_for_local_datetime;
  print $offset ; 
}

I get the error message: Can't call "local_rd_as_seconds" on an undefied value at /usr/local/share/perl/5.28.1/DateTime/TimeZone.pm or for other method can't call method utc_rd_as_seconds. From the doc, it looks like it should work... just doesn't...

autarch commented 2 years ago

I'm guessing you're not running with use strict, because this line is definitely a bug:

  my $tz = DateTime::TimeZone->new(name=>e);

That e should be $e.

I strongly recommend you start all scripts and modules with:

use strict;
use warnings;

This will save you a lot of pain.

japharl commented 2 years ago
use strict;
use DateTime::TimeZone;
my $code = lc shift;
my @list = DateTime::TimeZone->names_in_country($code);
foreach my $e (@list){
  my $tz = DateTime::TimeZone->new(name=>$e);
  my $offset = $tz->offset_for_local_datetime;
  print $offset ;
}

I was copying code from a different system. Manually moved the code over which removed typos and has the strict / warnings and is functional. Still generates the local_rd_as_seconds error mentioned above in the first post.

japharl commented 2 years ago

Can't call method "local_rd_as_seconds" on an undefined value at /usr/local/share/perl/5.28.1/DateTime/TimeZone.pm line 234.

autarch commented 2 years ago

Ah, ok.

The problem is that $tz->offset_for_local_datetime expects a DateTime object as an argument. Normally I like to do parameter validation for most of my public APIs, but in this case it's a performance-sensitive API for use primarily by DateTime (and related modules), so there's no validation and you get this unhelpful error.

But the docs do explain how this method (and related ones) are called - https://metacpan.org/pod/DateTime::TimeZone#$tz-%3Eoffset_for_local_datetime(-$dt-)

japharl commented 2 years ago

Ah, got it. That makes sense. I was able to find a work around (for my purpose). Thanks for following up.