Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make &formatter a Dateish attribute
This effectively adds :formatter parameter to Date as well.  Because?
Well, why not, it would make things consistent and further simplify
Date/DateTime
  • Loading branch information
lizmat committed Dec 29, 2015
1 parent 74ab052 commit 461e635
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
52 changes: 26 additions & 26 deletions src/core/Date.pm
@@ -1,5 +1,7 @@
my class Date does Dateish {

method !formatter() { sprintf '%s-%02d-%02d',self!year-Str,$!month,$!day }

my $valid-units := nqp::hash(
'day', 1,
'days', 1,
Expand All @@ -16,17 +18,17 @@ my class Date does Dateish {
!! X::DateTime::InvalidDeltaUnit.new(:$unit).throw
}

method BUILD($!year,$!month,$!day,$!daycount = Int) { self }
method BUILD($!year,$!month,$!day,&!formatter,$!daycount = Int) { self }

multi method new(Date: Int() $year, Int() $month, Int() $day) {
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');
nqp::create(self).BUILD($year,$month,$day)
nqp::create(self).BUILD($year,$month,$day,&formatter)
}
multi method new(Date: :$year!, :$month = 1, :$day = 1) {
self.new($year,$month,$day)
multi method new(Date: :$year!, :$month = 1, :$day = 1, :&formatter) {
self.new($year,$month,$day,:&formatter)
}
multi method new(Date: Str $date) {
multi method new(Date: Str $date, :&formatter) {
X::Temporal::InvalidFormat.new(
invalid-str => $date,
target => 'Date',
Expand All @@ -38,23 +40,20 @@ my class Date does Dateish {
'-'
(\d\d) # day
$/;
self.new($0,$1,$2)
self.new($0,$1,$2,:&formatter)
}
multi method new(Date: Dateish $d) {
nqp::create(self).BUILD($d.year,$d.month,$d.day)
multi method new(Date: Dateish $d, :&formatter) {
nqp::create(self).BUILD($d.year,$d.month,$d.day,&formatter)
}
multi method new(Date: Instant $i) {
self.new(DateTime.new($i))
multi method new(Date: Instant $i, :&formatter) {
self.new(DateTime.new($i),:&formatter)
}

method new-from-daycount($daycount) {
method new-from-daycount($daycount,:&formatter) {
self!ymd-from-daycount($daycount, my $year, my $month, my $day);
nqp::create(self).BUILD($year,$month,$day,$daycount)
nqp::create(self).BUILD($year,$month,$day,&formatter,$daycount)
}

method today() {
self.new(DateTime.now);
}
method today(:&formatter) { self.new(DateTime.now, :&formatter) }

multi method WHICH(Date:D:) {
nqp::box_s(
Expand Down Expand Up @@ -91,13 +90,13 @@ my class Date does Dateish {
$month = ($month - 1) % 12 + 1;
# If we overflow on days in the month, rather than throw an
# exception, we just clip to the last of the month
Date.new($year,$month,$!day > 28
self.new($year,$month,$!day > 28
?? $!day min self!DAYS-IN-MONTH($year,$month)
!! $!day);
}
else { # year
my int $year = $!year + $amount;
Date.new($year,$!month,$!day > 28
self.new($year,$!month,$!day > 28
?? $!day min self!DAYS-IN-MONTH($year,$!month)
!! $!day);
}
Expand All @@ -106,17 +105,19 @@ my class Date does Dateish {
method clone(*%_) {
my $h := nqp::getattr(%_,Map,'$!storage');
self.new(
nqp::existskey($h,'year') ?? nqp::atkey($h,'year') !! $!year,
nqp::existskey($h,'month') ?? nqp::atkey($h,'month') !! $!month,
nqp::existskey($h,'day') ?? nqp::atkey($h,'day') !! $!day,
nqp::existskey($h,'year') ?? nqp::atkey($h,'year') !! $!year,
nqp::existskey($h,'month') ?? nqp::atkey($h,'month') !! $!month,
nqp::existskey($h,'day') ?? nqp::atkey($h,'day') !! $!day,
:&!formatter,
)
}
method !clone-without-validating(*%_) { # A premature optimization.
my $h := nqp::getattr(%_,Map,'$!storage');
nqp::create(self).BUILD(
nqp::existskey($h,'year') ?? nqp::atkey($h,'year') !! $!year,
nqp::existskey($h,'month') ?? nqp::atkey($h,'month') !! $!month,
nqp::existskey($h,'day') ?? nqp::atkey($h,'day') !! $!day,
nqp::existskey($h,'year') ?? nqp::atkey($h,'year') !! $!year,
nqp::existskey($h,'month') ?? nqp::atkey($h,'month') !! $!month,
nqp::existskey($h,'day') ?? nqp::atkey($h,'day') !! $!day,
&!formatter,
)
}

Expand All @@ -127,7 +128,6 @@ my class Date does Dateish {
self.new-from-daycount(self.daycount - 1);
}

multi method Str(Date:D:) { self.yyyy-mm-dd }
multi method perl(Date:D:) {
self.^name ~ ".new($!year,$!month,$!day)"
}
Expand Down
4 changes: 0 additions & 4 deletions src/core/DateTime.pm
Expand Up @@ -3,7 +3,6 @@ my class DateTime does Dateish {
has int $.minute;
has $.second;
has int $.timezone; # UTC
has &.formatter;
# 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.
Expand Down Expand Up @@ -343,9 +342,6 @@ my class DateTime does Dateish {

method Date() { Date.new($!year,$!month,$!day) }

multi method Str(DateTime:D:) {
&!formatter ?? &!formatter(self) !! self!formatter
}
multi method perl(DateTime:D:) {
self.^name
~ ".new($!year,$!month,$!day,$!hour,$!minute,$!second,$!timezone)"
Expand Down
12 changes: 8 additions & 4 deletions src/core/Dateish.pm
@@ -1,8 +1,9 @@
my role Dateish {
has Int $.year;
has Int $.month;
has Int $.day;
has Int $.month; # should be int
has Int $.day; # should be int
has Int $.daycount;
has &.formatter;

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

Expand Down Expand Up @@ -33,6 +34,10 @@ my role Dateish {
multi method new(Dateish:) {
fail X::Cannot::New.new(class => self);
}

multi method Str(Dateish:D:) {
&!formatter ?? &!formatter(self) !! self!formatter
}
multi method gist(Dateish:D:) { self.Str }

method daycount() {
Expand Down Expand Up @@ -107,7 +112,7 @@ my role Dateish {
+ ($!month > 2 && IS-LEAP-YEAR($!year));
}

method yyyy-mm-dd() { sprintf '%s-%02d-%02d',self!year-Str,$!month,$!day }
method yyyy-mm-dd() { sprintf '%04d-%02d-%02d',$!year,$!month,$!day }

method earlier(*%unit) { self.later(:earlier, |%unit) }

Expand All @@ -124,7 +129,6 @@ my role Dateish {
}
%parts;
}

}

# =begin pod
Expand Down

0 comments on commit 461e635

Please sign in to comment.