Skip to content

Commit

Permalink
Make Date/DateTime creation 20% faster
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Nov 10, 2016
1 parent 03a2897 commit 0e7f480
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/core/Date.pm
Expand Up @@ -23,7 +23,7 @@ my class Date does Dateish {
proto method new(|) {*}
multi method new(Date: Int() $year, Int() $month, Int() $day, :&formatter, *%_) {
(1..12).in-range($month,'Month');
(1 .. self!DAYS-IN-MONTH($year,$month)).in-range($day,'Day');
(1 .. self.DAYS-IN-MONTH($year,$month)).in-range($day,'Day');
self === Date
?? nqp::create(self)!SET-SELF($year,$month,$day,&formatter)
!! self.bless(:$year,:$month,:$day,:&formatter,|%_)
Expand Down Expand Up @@ -106,13 +106,13 @@ my class Date does Dateish {
# If we overflow on days in the month, rather than throw an
# exception, we just clip to the last of the month
self.new($year,$month,$!day > 28
?? $!day min self!DAYS-IN-MONTH($year,$month)
?? $!day min self.DAYS-IN-MONTH($year,$month)
!! $!day);
}
else { # year
my int $year = $!year + $amount;
self.new($year,$!month,$!day > 28
?? $!day min self!DAYS-IN-MONTH($year,$!month)
?? $!day min self.DAYS-IN-MONTH($year,$!month)
!! $!day);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/DateTime.pm
Expand Up @@ -67,7 +67,7 @@ my class DateTime does Dateish {
:&formatter,
) {
(1..12).in-range($month,'Month');
(1 .. self!DAYS-IN-MONTH($year,$month)).in-range($day,'Day');
(1 .. self.DAYS-IN-MONTH($year,$month)).in-range($day,'Day');
(0..23).in-range($hour,'Hour');
(0..59).in-range($minute,'Minute');
(^61).in-range($second,'Second');
Expand Down
6 changes: 3 additions & 3 deletions src/core/Dateish.pm
Expand Up @@ -7,19 +7,19 @@ my role Dateish {

method IO(Dateish:D: |c) { IO::Path.new(~self) } # because Dateish is not Cool

# this sub is also used by !DAYS-IN-MONTH, which is used by other types
# this sub is also used by DAYS-IN-MONTH, which is used by other types
sub IS-LEAP-YEAR($y) { $y %% 4 and not $y %% 100 or $y %% 400 }
method is-leap-year(Dateish:D:) { IS-LEAP-YEAR($!year) }

my $days-in-month := nqp::list_i(
0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
);
# This method is used by Date and DateTime:
method !DAYS-IN-MONTH(\year, \month) {
method DAYS-IN-MONTH(\year, \month) {
nqp::atpos_i($days-in-month,month) ||
( month == 2 ?? 28 + IS-LEAP-YEAR(year) !! Nil );
}
method days-in-month(Dateish:D:) { self!DAYS-IN-MONTH($!year,$!month) }
method days-in-month(Dateish:D:) { self.DAYS-IN-MONTH($!year,$!month) }

method !year-Str() {
sprintf 0 <= $!year <= 9999 ?? '%04d' !! '%+05d', $!year;
Expand Down

0 comments on commit 0e7f480

Please sign in to comment.