zeroclickinfo-goodies/lib/DDG/Goodie/Week.pm

86 lines
2.4 KiB
Perl
Raw Normal View History

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;
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";
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 {
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
(
\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
)?\??
\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;
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) .
' 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();
}
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;