diff --git a/Changes b/Changes index eb2842c..8099b02 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,11 @@ Revision history for DateTime-US {{$NEXT}} + - Use LocalTime to add correct suffix to local time output + - Correct README example + - Correct errors in use of LocalTime + - Remove some old comments in code + - Add test for README example 0.1.3 2024-02-11T11:38:40-06:00 - Remove all reduntant DST test data into a separate file diff --git a/META6.json b/META6.json index cbbb36f..fb4aa42 100644 --- a/META6.json +++ b/META6.json @@ -6,8 +6,9 @@ "build-depends": [ ], "depends": [ - "Date::Utils", "UUID::V4", + "LocalTime", + "Date::Utils", "Timezones::US" ], "description": "Provides time zone and Daylight Saving Time (DST) infomation for US states and territories", diff --git a/README.md b/README.md index b44c790..7cf6b90 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,11 @@ Module **DateTime::US** provides a class with methods used to help Raku programs The main use case that motivated the module is to convert time in UTC to local time for creating calendars and almanacs. For example, local Sunrise is given in UTC and will normally be shown on a calendar in local time: - my $tz = DateTime.new: :timezone('CST'); - my $sunriseZ = DateTime.new: "2022-10-03T05:45:00Z"; + my $tz = DateTime::US.new: :timezone; + my $sunriseZ = DateTime.new: "2024-02-21T12:23:00Z"; + say $sunriseZ; # OUTPUT: «2024-02-21T12:23:00Z␤» my $localtime = $tz.to-localtime :utc($sunriseZ); + say $localtime; # OUTPUT: «2024-02-21T06:23:00 CST␤» Class methods ------------- diff --git a/docs/README.rakudoc b/docs/README.rakudoc index 29a025a..cfb221b 100644 --- a/docs/README.rakudoc +++ b/docs/README.rakudoc @@ -32,9 +32,11 @@ time for creating calendars and almanacs. For example, local Sunrise is given in UTC and will normally be shown on a calendar in local time: =begin code -my $tz = DateTime.new: :timezone('CST'); -my $sunriseZ = DateTime.new: "2022-10-03T05:45:00Z"; +my $tz = DateTime::US.new: :timezone; +my $sunriseZ = DateTime.new: "2024-02-21T12:23:00Z"; +say $sunriseZ; # OUTPUT: «2024-02-21T12:23:00Z␤» my $localtime = $tz.to-localtime :utc($sunriseZ); +say $localtime; # OUTPUT: «2024-02-21T06:23:00 CST␤» =end code =head2 Class methods diff --git a/lib/DateTime/US.rakumod b/lib/DateTime/US.rakumod index 07ee972..6d96256 100644 --- a/lib/DateTime/US.rakumod +++ b/lib/DateTime/US.rakumod @@ -1,22 +1,13 @@ unit class DateTime::US; use Timezones::US; -#use Date::Utils; +use LocalTime; has $.timezone is required; has $.name; has $.utc-offset; has $.dst-exceptions; -=begin comment -#| Formatter for local time -our $lt-format = sub ($self) is export { - sprintf "%04d-%02d-%02dT%02d:%02d:%02d", - .year, .month, .day, .hour, .minute, .second - given $self -} -=end comment - submethod TWEAK { # only certain names are recognized # also translate 'Xdt' to 'Xst' @@ -40,7 +31,11 @@ multi method to-localtime(DateTime :$utc! --> DateTime) { $utc-offset += 1; # make time 1 hour later $lt = $utc + Duration.new($utc-offset * SEC-PER-HOUR); } - $lt + # Use correct localtime as suffix + my $lt2 = LocalTime.new: :year($lt.year), :month($lt.month), :day($lt.day), + :hour($lt.hour), :minute($lt.minute), + :second($lt.second), :tz-abbrev; + $lt2.dt } multi method to-utc(DateTime :$localtime! --> DateTime) { diff --git a/t/3-local-utc.t b/t/3-local-utc.t index cb78622..f527010 100644 --- a/t/3-local-utc.t +++ b/t/3-local-utc.t @@ -31,6 +31,7 @@ is $ut1.hour, 9; is $ut2.hour, 8; is $ut3.hour, 9; + # and the reverse my $ut4 = DateTime.new: :year(2000), :month(1), :day(25), :10hour; # no dst my $ut5 = DateTime.new: :year(2000), :month(3), :day(25), :10hour; # dst @@ -48,4 +49,3 @@ is $lt4.hour, 4; is $lt5.hour, 5; is $lt6.hour, 4; - diff --git a/t/7-doc-example.t b/t/7-doc-example.t new file mode 100644 index 0000000..e9c3bb9 --- /dev/null +++ b/t/7-doc-example.t @@ -0,0 +1,20 @@ +use Test; + +use DateTime::US; +use LocalTime; + +my $tz = DateTime::US.new: :timezone; +my $sunriseZ = DateTime.new: "2024-02-21T12:23:00Z"; +say $sunriseZ; # 2024-02-21T12:23:00Z +my $localtime = $tz.to-localtime :utc($sunriseZ); +say $localtime; # 2024-02-21T06:23:00 CST + +is $localtime.year, 2024; +is $localtime.month, 2; +is $localtime.day, 21; +is $localtime.hour, 6; +is $localtime.minute, 23; +is $localtime.second, 0; + +done-testing; +