alabamenhu / DateTimeTimezones

A module to add time zone support to Raku's built-in DateTime class
5 stars 4 forks source link

One-liner demands reversed declaration (`DateTime::Timezones` vs `Timezones::DateTime`) ? #6

Open jubilatious1 opened 2 years ago

jubilatious1 commented 2 years ago

Not sure I understand this, but I took the example code from DateTime::Parse and tried to run it using the DateTime::Timezones module at the command line (I was getting a 'cannot parse' error and thought the issue may be timezone related).

This is what I see:

~$ raku -MDateTime::Timezones -e ' my $rfc1123 = DateTime::Timezones.new("Sun, 06 Nov 1994 08:49:37 GMT"); say $rfc1123;'
Could not find symbol '&Timezones' in 'Timezones::DateTime'
  in block <unit> at -e line 1

~$ raku -MDateTime::Timezones -e ' my $rfc1123 = Timezones::DateTime.new("Sun, 06 Nov 1994 08:49:37 GMT"); say $rfc1123;'
     - none
     - none
-0001-0-1-31T00:00:00Z

Above after successfully installing most recent DateTime::Timezones (see below). Ancient version of Raku (rakudo-2021.06) on an even more ancient version of MacOS, but I figured I'd report it anyway.

~$ ~/rakudo/rakudo-2021.06/zef/bin/zef install DateTime::Timezones
===> Searching for: DateTime::Timezones
===> Searching for missing dependencies: Timezones::ZoneInfo:ver<0.2+>
===> Testing: Timezones::ZoneInfo:ver<0.2.1>:auth<github:alabamenhu>
===> Testing [OK] for Timezones::ZoneInfo:ver<0.2.1>:auth<github:alabamenhu>
===> Testing: DateTime::Timezones:ver<0.4.0>:auth<github:alabamenhu>
===> Testing [OK] for DateTime::Timezones:ver<0.4.0>:auth<github:alabamenhu>
===> Installing: Timezones::ZoneInfo:ver<0.2.1>:auth<github:alabamenhu>
===> Installing: DateTime::Timezones:ver<0.4.0>:auth<github:alabamenhu>
~$ 
alabamenhu commented 1 year ago

Not sure why I'm just seeing this now, so apologies.

I don't think either should work: you should just called DateTime.new(…) and the DateTime returned will be timezone aware.

That said, when I run your example line, I get a bad date returned. I'm not sure why this is happening — probably a poor handling of one of the DateTime.new() multi candidates:

raku -MDateTime::Timezones -e ' my $rfc1123 = DateTime.new("Sun, 06 Nov 1994 08:49:37 GMT"); say $rfc1123;'
1899-12-31T00:00:00Z

I will investigate this

jubilatious1 commented 1 year ago

Same issue with ver<0.4.2> on recent MacOS Ventura 13.3

===> Install [OK] for DateTime::Timezones:ver<0.4.2>:auth<zef:guifa>
admin@MBP zef % raku -MDateTime::Timezones -e ' my $rfc1123 = DateTime::Timezones.new("Sun, 06 Nov 1994 08:49:37 GMT"); say $rfc1123;'
Could not find symbol '&Timezones' in 'Timezones::DateTime'
  in block <unit> at -e line 1

admin@MBP zef % 

But something weird happens if I try to assign to an @-sigiled Array, now it says it cannot find &Timezone (singular) instead of &Timezones (plural):

admin@MBP zef % raku -MDateTime::Timezones -e ' my @rfc1123 = DateTime::Timezone.new("Sun, 06 Nov 1994 08:49:37 GMT"); say @rfc1123;'
Could not find symbol '&Timezone' in 'Timezones::DateTime'
  in block <unit> at -e line 1

admin@MBP zef % 

HTH.

alabamenhu commented 1 year ago

I'm a bit confused though. You mention wanting to use DateTime::Parse but your one liner doesn't use it. This module only exports a single symbol, DateTime -- so I'd expect you to do use DateTime.new(…). The singular versus plural difference seems to be from a typo in your code, btw.

In theory, anything that CORE::DateTime.new allows should also work (minus a current issue I'm trying to figure out now with odd offsets that need to generate a custom timezone internally).

CORE::DateTime.new("Sun, 06 Nov 1994 08:49:37 GMT"); # errors

If you use say (untested code)

use DateTime::Parse;
use DateTime::Timezones;
my $rfc1123 = DateTime::Parse.new("Sun, 06 Nov 1994 08:49:37 GMT");

things should work, as DateTime::Parse.new will ultimately call CORE::DateTime.new after parsing, and DateTime::Timezones wraps CORE::DateTime.new and will intercept the call to produce a timezone-aware object (potentially without the exact timezone information depending on how fine tuned DateTime::Parse is — I think it can only get offsets).

jubilatious1 commented 1 year ago

Hi, I'll check to see if I introduced any typos in my tests. I tried just repeating the same code as before updating my MacOS.

Cut to the chase: this is what your last 3-line code snippet returns:

use DateTime::Parse;
use DateTime::Timezones;
my $rfc1123 = DateTime::Parse.new("Sun, 06 Nov 1994 08:49:37 GMT");

Returns:

Could not find symbol '&Parse' in 'Timezones::DateTime'
  in block <unit> at - line 4

(I may have seen this Timezones::DateTime error, and unsuccessfully changed my input to match).

jubilatious1 commented 1 year ago

A few more examples (MacOS Ventura 13.3),

admin@mbp ~ % raku -MDateTime::Timezones -e ' my $rfc1123 = DateTime::Timezones.new("Sun, 06 Nov 1994 08:49:37 GMT"); say $rfc1123;'
Could not find symbol '&Timezones' in 'Timezones::DateTime'
  in block <unit> at -e line 1

admin@mbp ~ % raku -MDateTime::Timezones -e ' my $rfc1123 = Timezones::Datetime.new("Sun, 06 Nov 1994 08:49:37 GMT"); say $rfc1123;'
Could not find symbol '&Datetime' in 'Timezones'
  in block <unit> at -e line 1

admin@mbp ~ % raku -MDateTime::Timezones -e ' my $rfc1123 = Timezones::DateTime.new("Sun, 06 Nov 1994 08:49:37 GMT"); say $rfc1123;'
1899-12-31T00:00:00Z
admin@mbp ~ % raku -MDateTime::Timezones -e ' my $rfc1123 = DateTime.new("Sun, 06 Nov 1994 08:49:37 GMT"); say $rfc1123;'
1899-12-31T00:00:00Z