diff --git a/src/core/Date.pm6 b/src/core/Date.pm6 index da2a8c0f428..a68161435e4 100644 --- a/src/core/Date.pm6 +++ b/src/core/Date.pm6 @@ -21,9 +21,15 @@ my class Date does Dateish { !! X::DateTime::InvalidDeltaUnit.new(:$unit).throw } - method !SET-SELF( - $!year,$!month,$!day,&!formatter,$!daycount = Int - --> Date:D) { self } + method !SET-SELF(\year,\month,\day,\formatter,$daycount? --> Date:D) { + nqp::bind($!year, year); # R#2581 + nqp::bind($!month, month); + nqp::bind($!day, day); + nqp::bind(&!formatter, formatter); + nqp::bind($!daycount, + nqp::isconcrete($daycount) ?? $daycount !! nqp::null); + self + } proto method new(|) {*} multi method new(Date: diff --git a/src/core/DateTime.pm6 b/src/core/DateTime.pm6 index 605fd7b30bc..7a114dde7dc 100644 --- a/src/core/DateTime.pm6 +++ b/src/core/DateTime.pm6 @@ -1,8 +1,8 @@ my class DateTime does Dateish { - has int $.hour; - has int $.minute; + has Int $.hour; + has Int $.minute; has $.second; - has int $.timezone; # UTC + has Int $.timezone; # UTC # Not an optimization but a necessity to ensure that # $dt.utc.local.utc is equivalent to $dt.utc. Otherwise, # DST-induced ambiguity could ruin our day. @@ -48,17 +48,27 @@ my class DateTime does Dateish { } method !SET-SELF( - $!year, - $!month, - $!day, - $!hour, - $!minute, - $!second, - $!timezone, - &!formatter, + Int:D \year, + Int:D \month, + Int:D \day, + Int:D \hour, + Int:D \minute, + \second, + Int:D \timezone, + &formatter --> DateTime:D) { + nqp::bind($!year, year); # R#2581 + nqp::bind($!month, month); + nqp::bind($!day, day); + nqp::bind(&!formatter, &formatter); + nqp::bind($!daycount, nqp::null); + $!hour := hour; + $!minute := minute; + $!second := second; + $!timezone := timezone; self } + method !new-from-positional(DateTime: Int() $year, Int() $month, diff --git a/src/core/Dateish.pm6 b/src/core/Dateish.pm6 index ab2562af9ef..1d3fac4c8df 100644 --- a/src/core/Dateish.pm6 +++ b/src/core/Dateish.pm6 @@ -1,7 +1,7 @@ my role Dateish { has Int $.year; - has Int $.month; # should be int - has Int $.day; # should be int + has Int $.month; + has Int $.day; has Int $.daycount; has &.formatter; @@ -44,14 +44,19 @@ my role Dateish { multi method gist(Dateish:D: --> Str:D) { self.Str } method daycount(--> Int:D) { - $!daycount //= do { - # taken from - my int $m = $!month < 3 ?? $!month + 12 !! $!month; - my int $y = $!year - ($!month < 3); - -678973 + $!day + (153 * $m - 2) div 5 - + 365 * $y + $y div 4 - - $y div 100 + $y div 400; - } + nqp::ifnull( + $!daycount, + $!daycount := self!calculate-daycount + ) + } + method !calculate-daycount(--> Int:D) { + # taken from + my int $d = $!day; + my int $m = $!month < 3 ?? $!month + 12 !! $!month; + my int $y = $!year - ($!month < 3); + -678973 + $d + (153 * $m - 2) div 5 + + 365 * $y + $y div 4 + - $y div 100 + $y div 400 } method !ymd-from-daycount($daycount,\year,\month,\day --> Nil) {