Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #652 from duckduckgo/mwm/jalali
Browse files Browse the repository at this point in the history
CalendarConversions: now with the Jalali calendar
  • Loading branch information
Jag Talon committed Oct 2, 2014
2 parents e9eef2e + 109bd64 commit 485a881
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 114 deletions.
1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Text::FIGlet = 2.19.3
Text::Unidecode = 0.04
Date::Calc = 6.3
Date::Hijri = 0.02
Date::Jalali2 = 0.04
Date::Leapyear = 1.72
; Dates role, et. al.
DateTime = 0.74
Expand Down
88 changes: 88 additions & 0 deletions lib/DDG/Goodie/CalendarConversion.pm
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;
66 changes: 0 additions & 66 deletions lib/DDG/Goodie/Hijri.pm

This file was deleted.

6 changes: 6 additions & 0 deletions share/goodie/calendar_conversion/calendar_conversion.css
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;
}
40 changes: 40 additions & 0 deletions share/goodie/calendar_conversion/calendars.yml
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
59 changes: 59 additions & 0 deletions t/CalendarConversion.t
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;
48 changes: 0 additions & 48 deletions t/Hijri.t

This file was deleted.

0 comments on commit 485a881

Please sign in to comment.