DatesRole: simplify parse_datestring_to_date.
Since they guard their own inputs and return undef in case of failure, we can just let them each try until we either get something or not. Anchoring the guards makes sure we can tell descriptive from formatted.master
parent
02c114607d
commit
8b38680560
|
@ -124,9 +124,8 @@ sub build_date_regex {
|
|||
# Parses any string that *can* be parsed to a date object
|
||||
sub parse_datestring_to_date {
|
||||
my ($d) = @_;
|
||||
return parse_formatted_datestring_to_date($d) if ($d =~ $formatted_datestring);
|
||||
return parse_descriptive_datestring_to_date($d) if ($d =~ $descriptive_datestring);
|
||||
return;
|
||||
|
||||
return parse_formatted_datestring_to_date($d) // parse_descriptive_datestring_to_date($d);
|
||||
}
|
||||
|
||||
# Accepts a string which looks like date per the supplied date_regex (e.g. '31/10/1980')
|
||||
|
@ -134,7 +133,7 @@ sub parse_datestring_to_date {
|
|||
sub parse_formatted_datestring_to_date {
|
||||
my ($d) = @_;
|
||||
|
||||
return unless ($d =~ $formatted_datestring); # Only handle white-listed strings, even if they might otherwise work.
|
||||
return unless ($d =~ qr/^$formatted_datestring$/); # Only handle white-listed strings, even if they might otherwise work.
|
||||
if ($d =~ $ambiguous_dates_matches) {
|
||||
# guesswork for ambigous DMY/MDY and switch to ISO
|
||||
my ($month, $day, $year) = ($+{'m'}, $+{'d'}, $+{'y'}); # Assume MDY, even though it's crazy, for backward compatibility
|
||||
|
@ -187,7 +186,7 @@ sub parse_all_strings_to_date {
|
|||
sub parse_descriptive_datestring_to_date {
|
||||
my ($string) = @_;
|
||||
|
||||
return unless ($string =~ qr/$descriptive_datestring_matches/);
|
||||
return unless ($string =~ qr/^$descriptive_datestring_matches$/);
|
||||
|
||||
my $now = DateTime->now();
|
||||
my $month = $+{'m'}; # Set in each alternative match.
|
||||
|
|
Loading…
Reference in New Issue