2014-08-09 18:19:16 -07:00
|
|
|
package DDG::Goodie::CalendarConversion;
|
|
|
|
# ABSTRACT: convert between various calendars.
|
2013-02-21 13:22:31 -08:00
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2013-02-21 13:22:31 -08:00
|
|
|
use DDG::Goodie;
|
2014-09-20 13:32:42 -07:00
|
|
|
with 'DDG::GoodieRole::Dates';
|
2014-08-20 11:45:33 -07:00
|
|
|
|
2013-02-21 13:22:31 -08:00
|
|
|
use Date::Hijri;
|
2014-08-09 18:56:47 -07:00
|
|
|
use Date::Jalali2;
|
|
|
|
|
2015-05-29 15:18:03 -07:00
|
|
|
use YAML::XS 'LoadFile';
|
2014-10-02 00:51:52 -07:00
|
|
|
|
2014-08-09 18:56:47 -07:00
|
|
|
zci answer_type => "calendar_conversion";
|
2014-10-02 00:51:52 -07:00
|
|
|
zci is_cached => 0;
|
2013-02-21 13:22:31 -08:00
|
|
|
|
2013-04-07 17:45:52 -07:00
|
|
|
primary_example_queries '22/8/2003 to the hijri calendar';
|
2014-08-09 18:56:47 -07:00
|
|
|
secondary_example_queries '23/6/1424 hijri to gregorian';
|
|
|
|
description 'convert dates from the Gregorian calendar to the Hijri/Jalali calendars and back';
|
2014-08-09 18:19:16 -07:00
|
|
|
name 'CalendarConversion';
|
|
|
|
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/CalendarConversion.pm';
|
2013-04-07 17:45:52 -07:00
|
|
|
category 'dates';
|
|
|
|
topics 'special_interest';
|
2014-08-09 18:56:47 -07:00
|
|
|
attribution github => ['http://github.com/mattlehning', 'mattlehning'],
|
|
|
|
github => ['http://github.com/ehsan', 'ehsan'];
|
2013-02-21 13:22:31 -08:00
|
|
|
|
2014-08-09 18:56:47 -07:00
|
|
|
triggers any => 'hijri', 'gregorian', 'jalali';
|
2013-02-21 13:22:31 -08:00
|
|
|
|
2015-05-29 15:18:03 -07:00
|
|
|
my $calendars = LoadFile(share('calendars.yml'));
|
2013-04-06 15:32:34 -07:00
|
|
|
|
2014-09-20 13:32:42 -07:00
|
|
|
my $datestring_regex = datestring_regex();
|
|
|
|
|
2014-10-02 00:51:52 -07:00
|
|
|
sub format_date {
|
|
|
|
my ($d, $m, $y, $cal) = @_;
|
2013-12-23 12:49:01 -08:00
|
|
|
|
2014-10-02 00:51:52 -07:00
|
|
|
return join(' ', $d, $calendars->{$cal}->[$m - 1], $y, '(' . ucfirst $cal . ')');
|
2013-12-23 12:49:01 -08:00
|
|
|
}
|
2013-02-21 13:22:31 -08:00
|
|
|
|
2013-12-23 12:49:01 -08:00
|
|
|
handle query_lc => sub {
|
2014-09-20 13:32:42 -07:00
|
|
|
return unless my ($datestring, $input_calendar, $output_calendar) = $_ =~ /^
|
|
|
|
($datestring_regex)\s+
|
2013-04-06 15:32:34 -07:00
|
|
|
(?:
|
2014-08-09 18:56:47 -07:00
|
|
|
(?:(?:in|on(?:\s+the))?)\s*
|
|
|
|
((?:gregorian|hijri|jalali)?)\s+
|
|
|
|
(?:calendar|date|time)?\s*
|
|
|
|
(?:is\s+)?
|
2013-04-06 15:32:34 -07:00
|
|
|
)?
|
|
|
|
(?:
|
|
|
|
(?:(?:in|on|to)(?:\s+the|in)?)\s+
|
|
|
|
)?
|
2014-08-09 18:56:47 -07:00
|
|
|
(gregorian|hijri|jalali)\s*
|
2014-10-01 23:51:52 -07:00
|
|
|
(?:calendar|date|time|years)?
|
2013-04-06 15:32:34 -07:00
|
|
|
$/x;
|
2014-09-20 13:32:42 -07:00
|
|
|
my $in_date = parse_datestring_to_date($datestring);
|
|
|
|
return unless $in_date;
|
|
|
|
my ($d, $m, $y) = ($in_date->day, $in_date->month, $in_date->year);
|
2013-12-23 13:04:41 -08:00
|
|
|
|
2014-10-01 23:51:52 -07:00
|
|
|
$input_calendar ||= 'gregorian'; # gregorian is the default
|
|
|
|
return if ($input_calendar eq $output_calendar || !$output_calendar);
|
2014-08-09 18:56:47 -07:00
|
|
|
|
2014-09-20 14:10:28 -07:00
|
|
|
my ($od, $om, $oy);
|
2013-12-23 12:49:01 -08:00
|
|
|
|
2014-09-20 14:10:28 -07:00
|
|
|
if ($input_calendar eq "hijri") {
|
|
|
|
($od, $om, $oy) = h2g($d, $m, $y); # To Gregorian;
|
|
|
|
($od, $om, $oy) = g2j($od, $om, $oy) if ($output_calendar eq "jalali");
|
|
|
|
} elsif ($input_calendar eq "gregorian") {
|
|
|
|
($od, $om, $oy) = g2h($d, $m, $y) if ($output_calendar eq "hijri");
|
|
|
|
($od, $om, $oy) = g2j($d, $m, $y) if ($output_calendar eq "jalali");
|
|
|
|
} elsif ($input_calendar eq "jalali") {
|
2014-08-09 18:56:47 -07:00
|
|
|
my $t = new Date::Jalali2($y, $m, $d, 1);
|
2014-09-20 14:10:28 -07:00
|
|
|
($od, $om, $oy) = ($t->jal_day, $t->jal_month, $t->jal_year);
|
|
|
|
($od, $om, $oy) = g2h($od, $om, $oy) if ($output_calendar eq "hijri");
|
2014-08-09 18:56:47 -07:00
|
|
|
}
|
2014-10-06 01:39:48 -07:00
|
|
|
my $input_date = format_date($d, $m, $y, $input_calendar);
|
2014-10-02 00:51:52 -07:00
|
|
|
my $converted_date = format_date($od, $om, $oy, $output_calendar);
|
2013-12-23 12:49:01 -08:00
|
|
|
|
2014-10-06 01:39:48 -07:00
|
|
|
return $input_date . ' is ' . $converted_date,
|
|
|
|
structured_answer => {
|
|
|
|
input => [$input_date],
|
2015-01-08 23:56:33 -08:00
|
|
|
operation => 'Calendar conversion',
|
2014-10-06 01:39:48 -07:00
|
|
|
result => $converted_date
|
|
|
|
};
|
2013-02-21 13:22:31 -08:00
|
|
|
};
|
2013-04-06 15:32:34 -07:00
|
|
|
|
2014-09-20 14:10:28 -07:00
|
|
|
sub g2j {
|
|
|
|
my ($id, $im, $iy) = @_;
|
|
|
|
|
|
|
|
my $t = new Date::Jalali2($iy, $im, $id, 0);
|
|
|
|
return ($t->jal_day, $t->jal_month, $t->jal_year);
|
|
|
|
}
|
|
|
|
|
2013-02-21 13:22:31 -08:00
|
|
|
1;
|