Subject: | truncate() has a validation problem |
Problem:
Any argument to ->truncate( to => ... ) that begins with "year", "month", "week", "day", "hour", "minute", or "second" passes validation, but if the argument isn't an exact match, truncate silently does nothing.
For example, "hours" passes validation, but truncate fails.
Reproduce by:
print DateTime->now->truncate(to => "hours");
2013-03-26T17:27:17
Expected result:
The 'to' parameter ("hours") to DateTime::truncate did not pass regex check
Here's a fix:
*** DateTime.pm 2012-11-16 10:40:40.000000000 -0700
--- DateTime-rev.pm 2013-03-26 00:47:32.000000000 -0700
***************
*** 1911,1917 ****
);
my $re = join '|', 'year', 'week',
grep { $_ ne 'nanosecond' } keys %TruncateDefault;
! my $spec = { to => { regex => qr/^(?:$re)/ } };
sub truncate {
my $self = shift;
--- 1911,1917 ----
);
my $re = join '|', 'year', 'week',
grep { $_ ne 'nanosecond' } keys %TruncateDefault;
! my $spec = { to => { regex => qr/^(?:$re)$/ } };
sub truncate {
my $self = shift;
... or to change truncate to allow plurals:
*** DateTime.pm 2012-11-16 10:40:40.000000000 -0700
--- DateTime-rev.pm 2013-03-26 00:47:32.000000000 -0700
***************
*** 1911,1917 ****
);
my $re = join '|', 'year', 'week',
grep { $_ ne 'nanosecond' } keys %TruncateDefault;
! my $spec = { to => { regex => qr/^(?:$re)/ } };
sub truncate {
my $self = shift;
--- 1911,1917 ----
);
my $re = join '|', 'year', 'week',
grep { $_ ne 'nanosecond' } keys %TruncateDefault;
! my $spec = { to => { regex => qr/^(?:$re)s?$/ } };
sub truncate {
my $self = shift;