From 87a7fb87bef522bf26a7f2bdf614fa5ffd43baf1 Mon Sep 17 00:00:00 2001 From: tagawa Date: Sat, 26 Jan 2013 17:10:53 +0900 Subject: [PATCH] Added plugin for converting non-Gregorian years --- lib/DDG/Goodie/AltCalendars.pm | 48 ++++++++++++++++++++++++++++++++++ t/AltCalendars.t | 23 ++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 lib/DDG/Goodie/AltCalendars.pm create mode 100644 t/AltCalendars.t diff --git a/lib/DDG/Goodie/AltCalendars.pm b/lib/DDG/Goodie/AltCalendars.pm new file mode 100644 index 000000000..14b096af7 --- /dev/null +++ b/lib/DDG/Goodie/AltCalendars.pm @@ -0,0 +1,48 @@ +package DDG::Goodie::AltCalendars; +# ABSTRACT: Convert non-Gregorian years to the Gregorian calendar + +use DDG::Goodie; + +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 start => 'juche', 'minguo', 'meiji', 'taisho', 'taishou', 'showa', 'shouwa', 'heisei'; + +zci answer_type => 'date_conversion'; +zci is_cached => 1; + +handle query_parts => sub { + return unless scalar(@_) == 2; + + my $year = $_[1]; + if ($year =~ /\d*[1-9]\d*/) { + my $era = lc($_[0]); + my %eras = ( + 'meiji' => 1867, # Japanese Meiji era + 'taisho' => 1911, # Japanese Taisho era + 'taishou' => 1911, # Alternative spelling of "taisho" + 'showa' => 1925, # Japanese Showa era + 'shouwa' => 1925, # Alternative spelling of "showa" + 'heisei' => 1988, # Japanese Heisei era + 'juche' => 1911, # North Korean Juche era + 'minguo' => 1911, # ROC (Taiwanese) Minguo era + ); + + return unless exists $eras{$era}; + + return $year + $eras{$era}; + } + + return ; +}; + +1; \ No newline at end of file diff --git a/t/AltCalendars.t b/t/AltCalendars.t new file mode 100644 index 000000000..a4c999515 --- /dev/null +++ b/t/AltCalendars.t @@ -0,0 +1,23 @@ +#!/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('2013'), + 'shouwa 39' => test_zci('1964'), + 'taisho 11' => test_zci('1922'), + 'meiji 1' => test_zci('1868'), + 'minguo 50' => test_zci('1961'), + 'juche 07' => test_zci('1918'), +); + +done_testing; \ No newline at end of file