Skip to content

Commit

Permalink
Merge pull request #5 from tbrowder/new-ver
Browse files Browse the repository at this point in the history
New version
  • Loading branch information
tbrowder authored Feb 22, 2024
2 parents fdacf8c + 3093e30 commit b1fde30
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<CST>;
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
-------------
Expand Down
6 changes: 4 additions & 2 deletions docs/README.rakudoc
Original file line number Diff line number Diff line change
Expand Up @@ -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<CST>;
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
Expand Down
17 changes: 6 additions & 11 deletions lib/DateTime/US.rakumod
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<cst>;
$lt2.dt
}

multi method to-utc(DateTime :$localtime! --> DateTime) {
Expand Down
2 changes: 1 addition & 1 deletion t/3-local-utc.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,4 +49,3 @@ is $lt4.hour, 4;
is $lt5.hour, 5;
is $lt6.hour, 4;


20 changes: 20 additions & 0 deletions t/7-doc-example.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use Test;

use DateTime::US;
use LocalTime;

my $tz = DateTime::US.new: :timezone<CST>;
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;

0 comments on commit b1fde30

Please sign in to comment.