diff --git a/lib/DDG/Goodie/Week.pm b/lib/DDG/Goodie/Week.pm index 4dc407160..ec9a87112 100644 --- a/lib/DDG/Goodie/Week.pm +++ b/lib/DDG/Goodie/Week.pm @@ -6,7 +6,7 @@ use DDG::Goodie; # My imports use strict; use warnings; -use Lingua::EN::Numbers::Ordinate; +use Lingua::EN::Numbers::Ordinate qw/ordinate ordsuf/; use DateTime; use Date::Calc qw(:all); @@ -43,38 +43,40 @@ my @months = qw/ /; handle query_raw => sub { - return unless / - what(?:'?s|\sis|\swas)\s - the\s - (current|(\d{1,2})(?:nd|th|rd|st)?)\s + return unless /^\s* + what(?:'?s|\sis|\swas)\s+ + the\s+ + (current|(\d{1,2})(?:nd|th|rd|st)?)\s+ week ( - \sof\s - (?:(?:the|this)\s)? + \s+of\s+ + (?:(?:the|this)\s+)? (year|\d{4}) )?\?? - /x; + \s*$/x; my $week = $1; my $year = defined $4 ? ($4 eq 'year' ? 'current' : $4) : 'current'; return if $week =~ s/(nd|th|rd|st)$// and $week > 52; - my $dt = DateTime->now(time_zone => $loc->time_zone); + my $dt = DateTime->now(time_zone => $loc->time_zone) + if ($week eq 'current' or $year eq 'current'); if ($week eq 'current' and $year eq 'current') { return "We are in currently in the " . ordinate($dt->week_number) . - " week of $dt->year"; + ' week of ' . $dt->year . '.', + html => 'We are in currently in the ' . $dt->week_number + . '' . ordsuf($dt->week_number) . '' + . ' week of ' . $dt->year . '.'; } elsif ($year eq 'current') { $year = $dt->year(); - my (undef, $month, $day) = Monday_of_Week($week, $year); - return "The " . ordinate($week) . " week of $year began on " . - "$months[--$month] " . ordinate($day); - } else { - my (undef, $month, $day) = Monday_of_Week($week, $year); - return "The " . ordinate($week) . " week of $year began on " . - "$months[--$month] " . ordinate($day); } + my (undef, $month, $day) = Monday_of_Week($week, $year); + return "The " . ordinate($week) . " week of $year began on " . + "$months[--$month] " . ordinate($day) . '.', + html =>"The $week" . ordsuf($week) . " week of $year began on " . + "$months[$month] $day" . ordsuf($day) . '.'; }; 1; diff --git a/t/Week.t b/t/Week.t index 262f2306c..67d1d416c 100644 --- a/t/Week.t +++ b/t/Week.t @@ -15,26 +15,33 @@ ddg_goodie_test( DDG::Goodie::Week )], - "what is the current week" => - test_zci(qr/We are in currently in the \d+\w+ week of \d+/), + "what is the current week" => test_zci( + qr/We are in currently in the \d+\w+ week of \d+\./, + html => qr:We are in currently in the \d+\w+ week of \d+\.:), - "what's the current week?" => - test_zci(qr/We are in currently in the \d+\w+ week of \d+/), + "what's the current week? " => test_zci( + qr/We are in currently in the \d+\w+ week of \d+\./, + html => qr:We are in currently in the \d+\w+ week of \d+\.:), - "whats the current week of the year" => - test_zci(qr/We are in currently in the \d+\w+ week of \d+/), + "whats the current week of the year" => test_zci( + qr/We are in currently in the \d+\w+ week of \d+\./, + html => qr:We are in currently in the \d+\w+ week of \d+\.:), - "what was the 5th week of this year" => - test_zci(qr/The \d+\w+ week of \d+ began on January \d+\w+/), + "what was the 5th week of this year" => test_zci( + qr/The \d+\w+ week of \d+ began on January \d+\w+\./, + html => qr:The \d+\w+ week of \d+ began on January \d+\w+\.:), - "what was the 43rd week of 1984" => - test_zci("The 43rd week of 1984 began on October 22nd"), + "what was the 43rd week of 1984" => test_zci( + "The 43rd week of 1984 began on October 22nd.", + html => "The 43rd week of 1984 began on October 22nd."), - "what was the 8th week of 1956" => - test_zci("The 8th week of 1956 began on February 20th"), + "what was the 8th week of 1956" => test_zci( + "The 8th week of 1956 began on February 20th.", + html => "The 8th week of 1956 began on February 20th."), - "what was the 21st week of 1987" => - test_zci("The 21st week of 1987 began on May 18th"), + "what was the 21st week of 1987" => test_zci( + "The 21st week of 1987 began on May 18th.", + html => "The 21st week of 1987 began on May 18th."), ); done_testing;