This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #652 from duckduckgo/mwm/jalali
CalendarConversions: now with the Jalali calendar
- Loading branch information
Showing
7 changed files
with
194 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package DDG::Goodie::CalendarConversion; | ||
# ABSTRACT: convert between various calendars. | ||
|
||
use DDG::Goodie; | ||
with 'DDG::GoodieRole::Dates'; | ||
|
||
use Date::Hijri; | ||
use Date::Jalali2; | ||
|
||
use YAML::XS qw(Load); | ||
|
||
zci answer_type => "calendar_conversion"; | ||
zci is_cached => 0; | ||
|
||
primary_example_queries '22/8/2003 to the hijri calendar'; | ||
secondary_example_queries '23/6/1424 hijri to gregorian'; | ||
description 'convert dates from the Gregorian calendar to the Hijri/Jalali calendars and back'; | ||
name 'CalendarConversion'; | ||
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/CalendarConversion.pm'; | ||
category 'dates'; | ||
topics 'special_interest'; | ||
attribution github => ['http://github.com/mattlehning', 'mattlehning'], | ||
github => ['http://github.com/ehsan', 'ehsan']; | ||
|
||
triggers any => 'hijri', 'gregorian', 'jalali'; | ||
|
||
|
||
my $calendars = Load(scalar share('calendars.yml')->slurp); | ||
|
||
my $datestring_regex = datestring_regex(); | ||
|
||
sub format_date { | ||
my ($d, $m, $y, $cal) = @_; | ||
|
||
return join(' ', $d, $calendars->{$cal}->[$m - 1], $y, '(' . ucfirst $cal . ')'); | ||
} | ||
|
||
handle query_lc => sub { | ||
return unless my ($datestring, $input_calendar, $output_calendar) = $_ =~ /^ | ||
($datestring_regex)\s+ | ||
(?: | ||
(?:(?:in|on(?:\s+the))?)\s* | ||
((?:gregorian|hijri|jalali)?)\s+ | ||
(?:calendar|date|time)?\s* | ||
(?:is\s+)? | ||
)? | ||
(?: | ||
(?:(?:in|on|to)(?:\s+the|in)?)\s+ | ||
)? | ||
(gregorian|hijri|jalali)\s* | ||
(?:calendar|date|time|years)? | ||
$/x; | ||
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); | ||
|
||
$input_calendar ||= 'gregorian'; # gregorian is the default | ||
return if ($input_calendar eq $output_calendar || !$output_calendar); | ||
|
||
my ($od, $om, $oy); | ||
|
||
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") { | ||
my $t = new Date::Jalali2($y, $m, $d, 1); | ||
($od, $om, $oy) = ($t->jal_day, $t->jal_month, $t->jal_year); | ||
($od, $om, $oy) = g2h($od, $om, $oy) if ($output_calendar eq "hijri"); | ||
} | ||
my $input_date = format_date($d, $m, $y, $input_calendar); | ||
my $converted_date = format_date($od, $om, $oy, $output_calendar); | ||
|
||
return $input_date. ' is '. $converted_date, html => "<div class='zci--calendarconversion text--primary'>$input_date <span class='text--secondary'>is</span> $converted_date</div>"; | ||
}; | ||
|
||
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); | ||
} | ||
|
||
|
||
|
||
1; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.zci--answer .zci--calendarconversion { | ||
font-size: 1.5em; | ||
font-weight: 300; | ||
padding-top: .25em; | ||
padding-bottom: .25em; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
gregorian: | ||
- January | ||
- Feburary | ||
- March | ||
- April | ||
- May | ||
- June | ||
- July | ||
- August | ||
- September | ||
- October | ||
- November | ||
- December | ||
hijri: | ||
- Muharram | ||
- Safar | ||
- Rabia Awal | ||
- Rabia Thani | ||
- Jumaada Awal | ||
- Jumaada Thani | ||
- Rajab | ||
- Sha'ban | ||
- Ramadan | ||
- Shawwal | ||
- Dhul-Qi'dah | ||
- Dhul-Hijjah | ||
jalali: | ||
- Farvardin | ||
- Ordibehesht | ||
- Khordad | ||
- Tir | ||
- Mordad | ||
- Shahrivar | ||
- Mehr | ||
- Aban | ||
- Azar | ||
- Dey | ||
- Bahman | ||
- Esfand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use DDG::Test::Goodie; | ||
|
||
zci answer_type => 'calendar_conversion'; | ||
zci is_cached => 0; | ||
|
||
my @g22h = ( | ||
'22 August 2003 (Gregorian) is 23 Jumaada Thani 1424 (Hijri)', | ||
html => | ||
"<div class='zci--calendarconversion text--primary'>22 August 2003 (Gregorian) <span class='text--secondary'>is</span> 23 Jumaada Thani 1424 (Hijri)</div>" | ||
); | ||
my @h23g = ( | ||
'23 Jumaada Thani 1424 (Hijri) is 22 August 2003 (Gregorian)', | ||
html => | ||
"<div class='zci--calendarconversion text--primary'>23 Jumaada Thani 1424 (Hijri) <span class='text--secondary'>is</span> 22 August 2003 (Gregorian)</div>" | ||
); | ||
my @g22j = ( | ||
'22 August 2003 (Gregorian) is 31 Mordad 1382 (Jalali)', | ||
html => | ||
"<div class='zci--calendarconversion text--primary'>22 August 2003 (Gregorian) <span class='text--secondary'>is</span> 31 Mordad 1382 (Jalali)</div>" | ||
); | ||
|
||
ddg_goodie_test( | ||
[qw(DDG::Goodie::CalendarConversion)], | ||
'22/8/2003 to hijri' => test_zci(@g22h), | ||
'22/8/2003 to the hijri calendar' => test_zci(@g22h), | ||
'22,8,2003 to hijri' => test_zci(@g22h), | ||
'23/6/1424 in hijri to gregorian years' => test_zci(@h23g), | ||
'23/6/1424 hijri to gregorian' => test_zci(@h23g), | ||
'22/8/2003 to jalali' => test_zci(@g22j), | ||
'31/5/1382 jalali to gregorian' => test_zci( | ||
'31 Mordad 1382 (Jalali) is 22 August 2003 (Gregorian)', | ||
html => | ||
"<div class='zci--calendarconversion text--primary'>31 Mordad 1382 (Jalali) <span class='text--secondary'>is</span> 22 August 2003 (Gregorian)</div>", | ||
), | ||
'31/5/1382 jalali to hijri' => test_zci( | ||
'31 Mordad 1382 (Jalali) is 23 Jumaada Thani 1424 (Hijri)', | ||
html => | ||
"<div class='zci--calendarconversion text--primary'>31 Mordad 1382 (Jalali) <span class='text--secondary'>is</span> 23 Jumaada Thani 1424 (Hijri)</div>" | ||
), | ||
'23/6/1424 in hijri to jalali date' => test_zci( | ||
'23 Jumaada Thani 1424 (Hijri) is 31 Mordad 1382 (Jalali)', | ||
html => | ||
"<div class='zci--calendarconversion text--primary'>23 Jumaada Thani 1424 (Hijri) <span class='text--secondary'>is</span> 31 Mordad 1382 (Jalali)</div>" | ||
), | ||
'August 22nd, 2003 to jalali' => test_zci(@g22j), | ||
'22 Aug 2003 to Hijri' => test_zci(@g22h), | ||
'22/8/2003 in the hijri calendar' => test_zci(@g22h), | ||
'22nd Aug 2003 in jalali' => test_zci(@g22j), | ||
'8-22-2003 in hijri years' => test_zci(@g22h), | ||
'August 22 2003 in jalali date' => test_zci(@g22j), | ||
'22nd Aug 2003 in gregorian time' => undef, | ||
); | ||
|
||
done_testing; |