Leverage DateTime era in order to get AD/BC info

master
rgaul 2016-04-23 18:18:08 +00:00
parent 11e8b96ce9
commit 6e93004eb4
2 changed files with 40 additions and 27 deletions

View File

@ -2,6 +2,8 @@ package DDG::Goodie::AltCalendars;
# ABSTRACT: Convert non-Gregorian years to the Gregorian calendar
use strict;
use DateTime;
use DDG::Goodie;
use JSON;
@ -23,15 +25,17 @@ handle query_parts => sub {
my $gregorian_year_started = $year_definitions->{$era_name}->{'gregorian_year_started'};
my $wikipedia_link = $year_definitions->{$era_name}->{'wikipedia_link'};
my $year = $gregorian_year_started + $era_year;
my $era = DateTime->now->set_year($year)->era;
$era_name = ucfirst($era_name);
$year = abs($year);
my $answer = "$era_name $era_year is equivalent to $year in the Gregorian Calendar";
my $answer = "$era_name $era_year is equivalent to $year $era in the Gregorian Calendar";
return $answer,
structured_answer => {
data => {
title => $year,
title => "$year $era",
subtitle => "$era_name Year $era_year"
},
meta => {

View File

@ -17,63 +17,72 @@ my %eras = (
'Heisei' => 'Heisei_period',
'Juche' => 'North_Korean_calendar',
'Minguo' => 'Minguo_calendar',
'Discordian' => 'Discordian_calendar'
'Discordian' => 'Discordian_calendar',
'Suriyakhati' => 'Thai_solar_calendar'
);
ddg_goodie_test(
[qw( DDG::Goodie::AltCalendars )],
'heisei 25' => test_zci(
'Heisei 25 is equivalent to 2013 in the Gregorian Calendar',
make_structured_answer('Heisei', '25', '2013')
'Heisei 25 is equivalent to 2013 AD in the Gregorian Calendar',
make_structured_answer('Heisei', '25', '2013', 'AD')
),
'shouwa 39' => test_zci(
'Shouwa 39 is equivalent to 1964 in the Gregorian Calendar',
make_structured_answer('Shouwa', '39', '1964')
'Shouwa 39 is equivalent to 1964 AD in the Gregorian Calendar',
make_structured_answer('Shouwa', '39', '1964', 'AD')
),
'taisho 11' => test_zci(
'Taisho 11 is equivalent to 1922 in the Gregorian Calendar',
make_structured_answer('Taisho', '11', '1922')
'Taisho 11 is equivalent to 1922 AD in the Gregorian Calendar',
make_structured_answer('Taisho', '11', '1922', 'AD')
),
'meiji 1' => test_zci(
'Meiji 1 is equivalent to 1868 in the Gregorian Calendar',
make_structured_answer('Meiji', '1', '1868')
'Meiji 1 is equivalent to 1868 AD in the Gregorian Calendar',
make_structured_answer('Meiji', '1', '1868', 'AD')
),
'minguo 50' => test_zci(
'Minguo 50 is equivalent to 1961 in the Gregorian Calendar',
make_structured_answer('Minguo', '50', '1961')
'Minguo 50 is equivalent to 1961 AD in the Gregorian Calendar',
make_structured_answer('Minguo', '50', '1961', 'AD')
),
'juche 07' => test_zci(
'Juche 07 is equivalent to 1918 in the Gregorian Calendar',
make_structured_answer('Juche', '07', '1918')
'Juche 07 is equivalent to 1918 AD in the Gregorian Calendar',
make_structured_answer('Juche', '07', '1918', 'AD')
),
'Heisei 12 was a leap year' => test_zci(
'Heisei 12 is equivalent to 2000 in the Gregorian Calendar',
make_structured_answer('Heisei', '12', '2000')
'Heisei 12 is equivalent to 2000 AD in the Gregorian Calendar',
make_structured_answer('Heisei', '12', '2000', 'AD')
),
'it\'s heisei 25 now' => test_zci(
'Heisei 25 is equivalent to 2013 in the Gregorian Calendar',
make_structured_answer('Heisei', '25', '2013')
'Heisei 25 is equivalent to 2013 AD in the Gregorian Calendar',
make_structured_answer('Heisei', '25', '2013', 'AD')
),
'January 1st Meiji 33' => test_zci(
'Meiji 33 is equivalent to 1900 in the Gregorian Calendar',
make_structured_answer('Meiji', '33', '1900')
'Meiji 33 is equivalent to 1900 AD in the Gregorian Calendar',
make_structured_answer('Meiji', '33', '1900', 'AD')
),
'heisei 24' => test_zci(
'Heisei 24 is equivalent to 2012 in the Gregorian Calendar',
make_structured_answer('Heisei', '24', '2012')
'Heisei 24 is equivalent to 2012 AD in the Gregorian Calendar',
make_structured_answer('Heisei', '24', '2012', 'AD')
),
'when was Discordian 2000' => test_zci(
'Discordian 2000 is equivalent to 834 in the Gregorian Calendar',
make_structured_answer('Discordian', '2000', '834')
'Discordian 2000 is equivalent to 834 AD in the Gregorian Calendar',
make_structured_answer('Discordian', '2000', '834', 'AD')
),
'suriyakhati 543' => test_zci(
'Suriyakhati 543 is equivalent to 0 BC in the Gregorian Calendar',
make_structured_answer('Suriyakhati', '543', '0', 'BC')
),
'suriyakhati 43' => test_zci(
'Suriyakhati 43 is equivalent to 500 BC in the Gregorian Calendar',
make_structured_answer('Suriyakhati', '43', '500', 'BC')
)
);
sub make_structured_answer {
my ($era_name, $era_year, $gregorian_year) = @_;
my ($era_name, $era_year, $gregorian_year, $era) = @_;
return structured_answer => {
data => {
title => $gregorian_year,
title => "$gregorian_year $era",
subtitle => "$era_name Year $era_year"
},
meta => {