Skip to content

Commit

Permalink
rebuild, test for release
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrowder committed Feb 11, 2024
1 parent 2c52c02 commit 5f802b3
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 595 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Revision history for DateTime-US

{{$NEXT}}
- Remove all reduntant DST test data into a separate file
- Require module UUID::V4 for generating GUIDs
- Add new module DateTime::Subs for new subroutines
- Add new subs to provide DST begin/end DateTimes for a year
(all require explicit export tags)
Expand Down
1 change: 1 addition & 0 deletions META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],
"depends": [
"Date::Utils",
"UUID::V4",
"Timezones::US"
],
"description": "Provides time zone and Daylight Saving Time (DST) infomation for US states and territories",
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ 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 $localtime = $tz.to-localtime :utc($sunrisez);
my $sunriseZ = DateTime.new: "2022-10-03T05:45:00Z";
my $localtime = $tz.to-localtime :utc($sunriseZ);

Class methods
-------------
Expand All @@ -39,6 +39,25 @@ Class methods

Given a local time, convert to UTC with adjustment for DST.

Subroutines
-----------

It is useful to have a "perpetual" calculation of the begin/end dates for DST for all years covered by the current governing federal law for the US thanks to the Date::Utils module. The following routines do that, and they require an `export` tag for use to avoid possible conflict from other modules.

The `:$year` argument defaults to the current year if it is not provided.

* begin-dst(:$year --> Date) is export(:begin-dst) {...}

* dst-begin(:$year --> Date) is export(:dst-begin) {...}

* end-dst(:$year --> Date) is export(:end-dst) {...}

* dst-end(:$year --> Date) is export(:dst-end) {...}

The final routine is for use by module Date::Event. The `:$set-id` argument is used to provide a globally unique ID (GUID) to allow multiple Date::Event objects for a single `Date` object.

* get-dst-dates(:$year!, :$set-id! --> Hash) is export(:get-dst-date) {...}

SEE ALSO
========

Expand Down
27 changes: 25 additions & 2 deletions docs/README.rakudoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ 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 $localtime = $tz.to-localtime :utc($sunrisez);
my $sunriseZ = DateTime.new: "2022-10-03T05:45:00Z";
my $localtime = $tz.to-localtime :utc($sunriseZ);
=end code

=head2 Class methods
Expand All @@ -50,6 +50,29 @@ B<to-utc>(DateTime :$localtime! --> DateTime) {...}

Given a local time, convert to UTC with adjustment for DST.
=end item

=head2 Subroutines

It is useful to have a "perpetual" calculation
of the begin/end dates for DST for all years covered by the
current governing federal law for the US thanks to the
Date::Utils module. The following routines do that, and they
require an C<export> tag for use to avoid possible conflict from
other modules.

The C<:$year> argument defaults to the current year if it is not provided.

=item begin-dst(:$year --> Date) is export(:begin-dst) {...}
=item dst-begin(:$year --> Date) is export(:dst-begin) {...}
=item end-dst(:$year --> Date) is export(:end-dst) {...}
=item dst-end(:$year --> Date) is export(:dst-end) {...}

The final routine is for use by module Date::Event.
The C<:$set-id> argument is used to provide a globally unique ID (GUID)
to allow multiple Date::Event objects for a single C<Date> object.

=item get-dst-dates(:$year!, :$set-id! --> Hash) is export(:get-dst-date) {...}

=head1 SEE ALSO

=begin item
Expand Down
8 changes: 6 additions & 2 deletions lib/DateTime/Subs.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use Date::Utils;
our &begin-dst is export(:begin-dst) = &dst-begin;
our &end-dst is export(:end-dst) = &dst-end;

sub dst-begin(:$year --> Date) is export(:dst-begin) {
sub dst-begin(:$year is copy --> Date) is export(:dst-begin) {
$year = Date.new(now).year if not $year.defined;

# nth(2) dow(7) in month 3 at 0200 local
my $nth = 2;
my $dow = 7;
Expand All @@ -14,7 +16,9 @@ sub dst-begin(:$year --> Date) is export(:dst-begin) {
$d
}

sub dst-end(:$year --> Date) is export(:dst-end) {
sub dst-end(:$year is copy --> Date) is export(:dst-end) {
$year = Date.new(now).year if not $year.defined;

# nth(1) dow(7) in month 11 at 0200 local
my $nth = 1;
my $dow = 7;
Expand Down
226 changes: 10 additions & 216 deletions t/2-dst.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,20 @@ use Test;
use DateTime::US;
use Timezones::US;

# test dst start and stop dates against known fed data
my %dst;
# test DST start and stop dates against known fed data
use lib 't/data';
use DST-Data;
my %DST = %dst-data;

=begin comment
Update the DST (daylight savings time) module with the desired year
('DST.pm').
See: https://en.wikipedia.org/wiki/Uniform_Time_Act
effective as of 2007:
begins: 0200 local, second Sunday in March
ends: 0200 local, first Sunday in November
=end comment

my $ntests = %dst.elems * 4;
my $ntests = %DST.elems * 4;
plan $ntests;

for %dst.keys -> $year {
for %DST.keys -> $year {
# get the known data
my $begin-month = %dst{$year}<begin><month>;
my $begin-day = %dst{$year}<begin><day>;
my $end-month = %dst{$year}<end><month>;
my $end-day = %dst{$year}<end><day>;
my $begin-month = %DST{$year}<begin><month>;
my $begin-day = %DST{$year}<begin><day>;
my $end-month = %DST{$year}<end><month>;
my $end-day = %DST{$year}<end><day>;

my $dtb = begin-dst $year;
my $dte = end-dst $year;
Expand All @@ -38,198 +27,3 @@ for %dst.keys -> $year {
is $dte.month, $end-month;
is $dte.day, $end-day;
}

=begin comment
Note: This is hand generated for now.
Update the DST (daylight savings time) module with the desired year
('DST.pm').
See: https://en.wikipedia.org/wiki/Uniform_Time_Act
effective as of 2007:
begins: 0200 local, second Sunday in March
ends: 0200 local, first Sunday in November
=end comment

BEGIN {

%dst = [

'2016' => {
begin => {
day => '13',
month => 3, # 'mar',
},
end => {
day => '6',
month => 11, # 'nov',
},
},
'2017' => {
begin => {
day => '12',
month => 3, # 'mar',
},
end => {
day => '5',
month => 11, # 'nov',
},
},
'2018' => {
begin => {
day => '11',
month => 3, # 'mar',
},
end => {
day => '4',
month => 11, # 'nov',
},
},
'2019' => {
begin => {
day => '10',
month => 3, # 'mar',
},
end => {
day => '3',
month => 11, # 'nov',
},
},


'2020' => {
begin => {
day => '8',
month => 3, # 'mar',
},
end => {
day => '1',
month => 11, # 'nov',
},
},


'2021' => {
begin => {
day => '14',
month => 3, # 'mar',
},
end => {
day => '7',
month => 11, # 'nov',
},
},


'2022' => {
begin => {
day => '13',
month => 3, # 'mar',
},
end => {
day => '6',
month => 11, # 'nov',
},
},


'2023' => {
begin => {
day => '12',
month => 3, # 'mar',
},
end => {
day => '5',
month => 11, # 'nov',
},
},


'2024' => {
begin => {
day => '10',
month => 3, # 'mar',
},
end => {
day => '3',
month => 11, # 'nov',
},
},


'2025' => {
begin => {
day => '9',
month => 3, # 'mar',
},
end => {
day => '2',
month => 11, # 'nov',
},
},


'2026' => {
begin => {
day => '8',
month => 3, # 'mar',
},
end => {
day => '1',
month => 11, # 'nov',
},
},


'2027' => {
begin => {
day => '14',
month => 3, # 'mar',
},
end => {
day => '7',
month => 11, # 'nov',
},
},


'2028' => {
begin => {
day => '12',
month => 3, # 'mar',
},
end => {
day => '5',
month => 11, # 'nov',
},
},


'2029' => {
begin => {
day => '11',
month => 3, # 'mar',
},
end => {
day => '4',
month => 11, # 'nov',
},
},


'2030' => {
begin => {
day => '10',
month => 3, # 'mar',
},
end => {
day => '3',
month => 11, # 'nov',
},
},
]; # end of %dst

}
5 changes: 5 additions & 0 deletions t/3-local-utc.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use Test;
use DateTime::US;
use Timezones::US;

# test DST start and stop dates against known fed data
use lib 't/data';
use DST-Data;
my %DST = %dst-data;

plan 13;

# test conversion between local time and UTC
Expand Down
Loading

0 comments on commit 5f802b3

Please sign in to comment.