Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
switched .truncated-to to enum
The exception type X::Temporal::Truncation also goes away,
being replaced by ordinary parameter type checking.
  • Loading branch information
Carl Masak committed Jan 24, 2013
1 parent 943ecc5 commit 7e10a0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 48 deletions.
7 changes: 0 additions & 7 deletions src/core/Exception.pm
Expand Up @@ -1120,13 +1120,6 @@ my class X::Temporal::InvalidFormat does X::Temporal {
"Invalid $.target string '$.invalid-str'; use $.format instead";
}
}
my class X::Temporal::Truncation does X::Temporal {
has $.invocant;
has $.error;
method message() {
"Error while truncating $.invocant: $.error";
}
}
my class X::DateTime::TimezoneClash does X::Temporal {
method message() {
'DateTime.new(Str): :timezone argument not allowed with a timestamp offset';
Expand Down
60 changes: 19 additions & 41 deletions src/core/Temporal.pm
@@ -1,6 +1,16 @@
my class DateTime { ... }
my class Date { ... }

my enum TimeUnit (
:second(1), :seconds(2),
:minute(3), :minutes(4),
:hour(5), :hours(6),
:day(7), :days(8),
:week(9), :weeks(10),
:month(11), :months(12),
:year(13), :years(14),
);

my role Dateish {
method is-leap-year($y = $.year) {
$y %% 4 and not $y %% 100 or $y %% 400
Expand Down Expand Up @@ -104,14 +114,14 @@ my role Dateish {
1 .. self.days-in-month);
}

method truncate-parts($unit, %parts? is copy) {
method truncate-parts(TimeUnit $unit, %parts? is copy) {
# Helper for DateTime.truncated-to and Date.truncated-to.
if $unit eq 'week' {
if $unit == week | weeks {
my $dc = self.get-daycount;
my $new-dc = $dc - self.day-of-week($dc) + 1;
%parts<year month day> =
self.ymd-from-daycount($new-dc);
} else { # $unit eq 'month'|'year'
} else { # $unit == month | months | year | years
%parts<day> = 1;
$unit eq 'year' and %parts<month> = 1;
}
Expand Down Expand Up @@ -177,16 +187,6 @@ my class DateTime-local-timezone does Callable {
}
}

my enum TimeUnit (
:second(1), :seconds(2),
:minute(3), :minutes(4),
:hour(5), :hours(6),
:day(7), :days(8),
:week(9), :weeks(10),
:month(11), :months(12),
:year(13), :years(14),
);

my class DateTime does Dateish {
has Int $.year;
has Int $.month = 1;
Expand Down Expand Up @@ -403,28 +403,17 @@ my class DateTime does Dateish {
self.new(:$date, :$hour, :$minute, :$second);
}

method truncated-to(*%args) {
%args.keys == 1
or X::Temporal::Truncation.new(
invocant => self,
error => 'exactly one named argument needed',
).throw;
my $unit = %args.keys[0];
$unit eq any(<second minute hour day week month year>)
or X::Temporal::Truncation.new(
invocant => self,
error => "Unknown truncation unit '$unit'",
).throw;
method truncated-to(TimeUnit $unit) {
my %parts;
given $unit {
%parts<second> = self.whole-second;
when 'second' {}
when second {}
%parts<second> = 0;
when 'minute' {}
when minute {}
%parts<minute> = 0;
when 'hour' {}
when hour {}
%parts<hour> = 0;
when 'day' {}
when day {}
# Fall through to Dateish.
%parts = self.truncate-parts($unit, %parts);
}
Expand Down Expand Up @@ -553,18 +542,7 @@ my class Date does Dateish {
self.new(DateTime.now);
}

method truncated-to(*%args) {
%args.keys == 1
or X::Temporal::Truncation.new(
invocant => self,
error => "exactly one named argument needed",
).throw;
my $unit = %args.keys[0];
$unit eq any(<week month year>)
or X::Temporal::Truncation.new(
invocant => self,
error => "unknown truncation unit '$unit'",
).throw;
method truncated-to(TimeUnit $unit) {
self.clone(|self.truncate-parts($unit));
}

Expand Down

0 comments on commit 7e10a0d

Please sign in to comment.