Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
DateRole: Handling 'of' in textual date formats (#3982)
Browse files Browse the repository at this point in the history
* DateRole: Adding failing test for `$nth of $month $year`

* DateRole: Now parses "7th of march 2017"

* DateRole: Adding failing test for '5 of jan 1993' (no suffix and short month)

* DateRole: Handles 5 of march

* DateRole: Now also handle "march the 30th, 2017"
  • Loading branch information
mintsoft authored and moollaza committed Apr 11, 2017
1 parent 011c78e commit 264282b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/DDG/GoodieRole/Dates.pm
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,12 @@ sub build_datestring_regex {
# month-first date formats
push @regexes, qr#$date_number$date_delim$short_month$date_delim$full_year#i;
push @regexes, qr#$date_number$date_delim$full_month$date_delim$full_year#i;
push @regexes, qr#(?:$short_month|$full_month) $date_number(?: ?$number_suffixes)?[,]? $full_year#i;
push @regexes, qr#(?:$short_month|$full_month) (?:the )?$date_number(?: ?$number_suffixes)?[,]? $full_year#i;

# day-first date formats
push @regexes, qr#$short_month$date_delim$date_number$date_delim$full_year#i;
push @regexes, qr#$full_month$date_delim$date_number$date_delim$full_year#i;
push @regexes, qr#$date_number[,]?(?: ?$number_suffixes)? (?:$short_month|$full_month)[,]? $full_year#i;
push @regexes, qr#$date_number[,]?(?: ?$number_suffixes)? (?:of )?(?:$short_month|$full_month)[,]? $full_year#i;

## Ambiguous, but potentially valid date formats
push @regexes, $ambiguous_dates;
Expand Down Expand Up @@ -391,6 +391,7 @@ sub parse_formatted_datestring_to_date {
}

$d =~ s/(\d+)\s?$number_suffixes/$1/i; # Strip ordinal text.
$d =~ s/(\sof\s)|(\sthe\s)/ /i; # Strip "of" for "4th of march" and "the" for "march the 4th"
$d =~ s/,//i; # Strip any random commas.
$d =~ s/($full_month)/$full_month_to_short{lc $1}/i; # Parser deals better with the shorter month names.
$d =~ s/^($short_month)$date_delim(\d{1,2})/$2-$short_month_fix{lc $1}/i; # Switching Jun-01-2012 to 01 Jun 2012
Expand Down
3 changes: 3 additions & 0 deletions t/00-roles.t
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ subtest 'Dates' => sub {
'29 feb, 2012' => 1330473600,
'2038-01-20' => 2147558400, # 32-bit signed int UNIX epoch ends 2038-01-19
'1780-01-20' => -5994172800, # Way before 32-bit signed int epoch
'5th of january 1993' => 726192000,
'5 of jan 1993' => 726192000,
'june the 1st 2012' => 1338508800,
);

foreach my $test_date (sort keys %dates_to_match) {
Expand Down

0 comments on commit 264282b

Please sign in to comment.