Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Sanitize %UNITS out of global namespace
  • Loading branch information
lizmat committed Jul 15, 2015
1 parent 8e5525e commit 0493008
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/core/Temporal.pm
@@ -1,15 +1,20 @@
my class DateTime { ... }
my class Date { ... }

my %UNITS = (<second minute hour day week month year> X~ "","s").map: {$_ => 1};

my role Dateish {
has Int $.year;
has Int $.month = 1;
has Int $.day = 1;

method IO(|c) { IO::Path.new(self) }

method !VALID-UNIT($unit) {
state %UNITS = # core setting doesn't build if it is a my at role level
(<second minute hour day week month year> X~ "","s").map: {$_ => 1};
X::DateTime::InvalidDeltaUnit.new(:$unit).throw
unless %UNITS.EXISTS-KEY($unit);
}

method is-leap-year($y = $!year) {
$y %% 4 and not $y %% 100 or $y %% 400
}
Expand Down Expand Up @@ -108,8 +113,7 @@ my role Dateish {

method truncate-parts(Cool:D $unit, %parts? is copy) {
# Helper for DateTime.truncated-to and Date.truncated-to.
X::DateTime::InvalidDeltaUnit.new(:$unit).throw
unless %UNITS.EXISTS-KEY($unit);
self!VALID-UNIT($unit);
if $unit eq 'week' | 'weeks' {
my $dc = self.get-daycount;
my $new-dc = $dc - self.day-of-week($dc) + 1;
Expand Down Expand Up @@ -326,9 +330,7 @@ my class DateTime does Dateish {
unless %unit.keys;

my ($unit, $amount) = %unit.kv;

X::DateTime::InvalidDeltaUnit.new(:$unit).throw
unless %UNITS.EXISTS-KEY($unit);
self!VALID-UNIT($unit);

my ($hour, $minute) = $!hour, $!minute;
my $date;
Expand Down Expand Up @@ -542,9 +544,7 @@ my class Date does Dateish {
if %unit.keys > 1;

my ($unit, $amount) = %unit.kv;

X::DateTime::InvalidDeltaUnit.new(:$unit).throw
unless %UNITS.EXISTS-KEY($unit);
self!VALID-UNIT($unit);

my $date;

Expand Down

0 comments on commit 0493008

Please sign in to comment.