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

Added plugin for converting non-Gregorian years #150

Merged
merged 2 commits into from May 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 65 additions & 0 deletions lib/DDG/Goodie/AltCalendars.pm
@@ -0,0 +1,65 @@
package DDG::Goodie::AltCalendars;
# ABSTRACT: Convert non-Gregorian years to the Gregorian calendar

use DDG::Goodie;

#use constant MD5HERF => "http://en.wikipedia.org/wiki/MD5";
#use constant SHA1HREF => "http://en.wikipedia.org/wiki/SHA-1";
#use constant SHA2HREF => "http://en.wikipedia.org/wiki/SHA-2";
#use constant SHA3HREF => "http://en.wikipedia.org/wiki/SHA-3";

primary_example_queries 'heisei 24';
secondary_example_queries 'meiji 1';
description 'Convert non-Gregorian years to the Gregorian calendar';
name 'Alternative Calendars';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/AltCalendars.pm';
category 'conversions';
topics 'everyday';

attribution web => ['http://kyokodaniel.com/tech/', 'Daniel Davis'],
github => ['https://github.com/tagawa', 'tagawa'],
twitter => ['https://twitter.com/ourmaninjapan', 'ourmaninjapan'];

triggers any => 'juche', 'minguo', 'meiji', 'taisho', 'taishou', 'showa', 'shouwa', 'heisei';

zci answer_type => 'date_conversion';
zci is_cached => 1;

handle query_parts => sub {
# Ignore single word queries
return unless scalar(@_) > 1;

my %eras = (
'Meiji' => [1867, 'Meiji_period'], # Japanese Meiji era
'Taisho' => [1911, 'Taisho_period'], # Japanese Taisho era
'Taishou' => [1911, 'Taisho_period'], # Alternative spelling of "Taisho"
'Showa' => [1925, 'Showa_period'], # Japanese Showa era
'Shouwa' => [1925, 'Showa_period'], # Alternative spelling of "Showa"
'Heisei' => [1988, 'Heisei_period'], # Japanese Heisei era
'Juche' => [1911, 'North_Korean_calendar'], # North Korean Juche era
'Minguo' => [1911, 'Minguo_calendar'], # ROC (Taiwanese) Minguo era
);

if ($_ =~ /^(.*\b)(meiji|taisho|taishou|showa|shouwa|heisei|juche|minguo)\s+(\d*[1-9]\d*)(\b.*)$/i) {
my $era_name = ucfirst($2);
my $era_year = $3;
my $year = $eras{$era_name}[0] + $era_year;
my $result = $1.$year.$4;
my $wiki = 'https://en.wikipedia.org/wiki/';
my $answer;

if ($result =~ /^[0-9]{4}$/) {
$answer = "$era_name $era_year is equivalent to $year in the Gregorian Calendar";
} else {
$answer = "$result ($era_name $era_year is equivalent to $year in the Gregorian Calendar)";
}

my $answer_html = $answer.'<br><a href="'.$wiki.$eras{$era_name}[1].'">More at Wikipedia</a>';

return $answer, html => $answer_html;
};

return ;
};

1;
26 changes: 26 additions & 0 deletions t/AltCalendars.t
@@ -0,0 +1,26 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;

zci answer_type => 'date_conversion';
zci is_cached => 1;

ddg_goodie_test(
[qw(
DDG::Goodie::AltCalendars
)],
'heisei 25' => test_zci('Heisei 25 is equivalent to 2013 in the Gregorian Calendar', html => 'Heisei 25 is equivalent to 2013 in the Gregorian Calendar<br><a href="https://en.wikipedia.org/wiki/Heisei_period">More at Wikipedia</a>'),
'shouwa 39' => test_zci('Shouwa 39 is equivalent to 1964 in the Gregorian Calendar', html => 'Shouwa 39 is equivalent to 1964 in the Gregorian Calendar<br><a href="https://en.wikipedia.org/wiki/Showa_period">More at Wikipedia</a>'),
'taisho 11' => test_zci('Taisho 11 is equivalent to 1922 in the Gregorian Calendar', html => 'Taisho 11 is equivalent to 1922 in the Gregorian Calendar<br><a href="https://en.wikipedia.org/wiki/Taisho_period">More at Wikipedia</a>'),
'meiji 1' => test_zci('Meiji 1 is equivalent to 1868 in the Gregorian Calendar', html => 'Meiji 1 is equivalent to 1868 in the Gregorian Calendar<br><a href="https://en.wikipedia.org/wiki/Meiji_period">More at Wikipedia</a>'),
'minguo 50' => test_zci('Minguo 50 is equivalent to 1961 in the Gregorian Calendar', html => 'Minguo 50 is equivalent to 1961 in the Gregorian Calendar<br><a href="https://en.wikipedia.org/wiki/Minguo_calendar">More at Wikipedia</a>'),
'juche 07' => test_zci('Juche 07 is equivalent to 1918 in the Gregorian Calendar', html => 'Juche 07 is equivalent to 1918 in the Gregorian Calendar<br><a href="https://en.wikipedia.org/wiki/North_Korean_calendar">More at Wikipedia</a>'),
'Heisei 12 was a leap year' => test_zci('2000 was a leap year (Heisei 12 is equivalent to 2000 in the Gregorian Calendar)', html => '2000 was a leap year (Heisei 12 is equivalent to 2000 in the Gregorian Calendar)<br><a href="https://en.wikipedia.org/wiki/Heisei_period">More at Wikipedia</a>'),
'it\'s heisei 25 now' => test_zci('it\'s 2013 now (Heisei 25 is equivalent to 2013 in the Gregorian Calendar)', html => 'it\'s 2013 now (Heisei 25 is equivalent to 2013 in the Gregorian Calendar)<br><a href="https://en.wikipedia.org/wiki/Heisei_period">More at Wikipedia</a>'),
'January 1st Meiji 33' => test_zci('January 1st 1900 (Meiji 33 is equivalent to 1900 in the Gregorian Calendar)', html => 'January 1st 1900 (Meiji 33 is equivalent to 1900 in the Gregorian Calendar)<br><a href="https://en.wikipedia.org/wiki/Meiji_period">More at Wikipedia</a>'),
);

done_testing;