2013-08-10 13:11:30 -07:00
|
|
|
package DDG::Goodie::Week;
|
2013-08-16 12:50:15 -07:00
|
|
|
# ABSTRACT: Find the current week number or when a week began
|
2013-08-10 13:11:30 -07:00
|
|
|
|
|
|
|
use DDG::Goodie;
|
|
|
|
|
|
|
|
# My imports
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2013-08-18 20:17:59 -07:00
|
|
|
use Lingua::EN::Numbers::Ordinate qw/ordinate ordsuf/;
|
2013-08-10 17:15:46 -07:00
|
|
|
use DateTime;
|
|
|
|
use Date::Calc qw(:all);
|
|
|
|
|
|
|
|
# File metadata
|
2013-08-15 13:27:02 -07:00
|
|
|
primary_example_queries "what is the current week";
|
|
|
|
secondary_example_queries "what was the 5th week of this year",
|
|
|
|
"what was the 5th week of 1944";
|
2013-08-16 12:50:15 -07:00
|
|
|
description "find the current week number or when a week began";
|
2013-08-10 17:15:46 -07:00
|
|
|
name "Week";
|
2013-08-16 12:50:15 -07:00
|
|
|
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Week.pm";
|
2013-08-10 17:15:46 -07:00
|
|
|
category "dates";
|
|
|
|
topics "everyday", "special_interest";
|
2015-01-07 10:24:47 -08:00
|
|
|
attribution twitter => ["garrettsquire", 'Garrett Squire'],
|
|
|
|
github => ["gsquire", 'Garrett Squire'];
|
2013-08-10 13:11:30 -07:00
|
|
|
|
2013-08-16 12:50:15 -07:00
|
|
|
triggers any => 'week';
|
2013-08-10 13:11:30 -07:00
|
|
|
|
|
|
|
zci is_cached => 1;
|
2013-08-10 17:15:46 -07:00
|
|
|
zci answer_type => "week";
|
2013-08-10 13:11:30 -07:00
|
|
|
|
2013-08-16 12:50:15 -07:00
|
|
|
my @months = qw/
|
|
|
|
January
|
|
|
|
February
|
|
|
|
March
|
|
|
|
April
|
|
|
|
May
|
|
|
|
June
|
|
|
|
July
|
|
|
|
August
|
|
|
|
September
|
|
|
|
October
|
|
|
|
November
|
|
|
|
December
|
|
|
|
/;
|
|
|
|
|
|
|
|
handle query_raw => sub {
|
2013-08-18 20:17:59 -07:00
|
|
|
return unless /^\s*
|
2013-08-19 11:02:41 -07:00
|
|
|
what(?:'?s|\sis|\swas)?\s+
|
|
|
|
(?:the\s+)?
|
|
|
|
(?:(current|(\d{1,2})(?:nd|th|rd|st)?)\s+)?
|
2013-08-16 12:50:15 -07:00
|
|
|
week
|
|
|
|
(
|
2013-08-18 20:17:59 -07:00
|
|
|
\s+of\s+
|
|
|
|
(?:(?:the|this)\s+)?
|
2013-08-16 12:50:15 -07:00
|
|
|
(year|\d{4})
|
2013-08-19 11:02:41 -07:00
|
|
|
|
|
|
|
|
\s+is\s+this
|
2013-08-16 12:50:15 -07:00
|
|
|
)?\??
|
2013-08-18 20:17:59 -07:00
|
|
|
\s*$/x;
|
2013-08-16 12:50:15 -07:00
|
|
|
|
|
|
|
my $week = $1;
|
|
|
|
my $year = defined $4 ? ($4 eq 'year' ? 'current' : $4) : 'current';
|
2013-08-19 11:02:41 -07:00
|
|
|
($week, $year) = qw/current current/ if (not defined $week);
|
2013-08-16 12:50:15 -07:00
|
|
|
|
|
|
|
return if $week =~ s/(nd|th|rd|st)$// and $week > 52;
|
|
|
|
|
2013-08-18 20:17:59 -07:00
|
|
|
my $dt = DateTime->now(time_zone => $loc->time_zone)
|
|
|
|
if ($week eq 'current' or $year eq 'current');
|
2013-08-16 12:50:15 -07:00
|
|
|
|
|
|
|
if ($week eq 'current' and $year eq 'current') {
|
|
|
|
return "We are in currently in the " . ordinate($dt->week_number) .
|
2013-08-18 20:17:59 -07:00
|
|
|
' week of ' . $dt->year . '.',
|
|
|
|
html => 'We are in currently in the ' . $dt->week_number
|
|
|
|
. '<sup>' . ordsuf($dt->week_number) . '</sup>'
|
|
|
|
. ' week of ' . $dt->year . '.';
|
2013-08-16 12:50:15 -07:00
|
|
|
} elsif ($year eq 'current') {
|
|
|
|
$year = $dt->year();
|
|
|
|
}
|
2013-08-18 20:17:59 -07:00
|
|
|
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<sup>" . ordsuf($week) . "</sup> week of $year began on " .
|
|
|
|
"$months[$month] $day<sup>" . ordsuf($day) . '</sup>.';
|
2013-08-16 12:50:15 -07:00
|
|
|
};
|
|
|
|
|
2013-08-10 13:11:30 -07:00
|
|
|
1;
|