Skip to content

Commit

Permalink
Merge pull request #119 from shuhei/jp-non-monday-substitute-holiday
Browse files Browse the repository at this point in the history
Fix Japanese non-Monday substitute holidays such as 2015-05-06
  • Loading branch information
ppeble committed May 23, 2015
2 parents 4b1ef02 + 4c979b5 commit 965f232
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
16 changes: 15 additions & 1 deletion data/jp.yaml
Expand Up @@ -9,6 +9,7 @@
# CHANGES:
# 2010-12-25: Initial version by Tatsuki Sugiura <sugi@nemui.org>
# 2014-11-09: Added substitute holiday by Yoshiyuki Hirano <yoshiyuki.hirano@henteco-labs.com>
# 2015-05-10: Non-Monday substitute holidays by Shuhei Kagawa <shuhei.kagawa@gmail.com>
#
---
months:
Expand Down Expand Up @@ -57,6 +58,9 @@ months:
- name: 振替休日
regions: [jp]
function: jp_substitute_holiday(year, 5, 3)
- name: 振替休日
regions: [jp]
function: jp_substitute_holiday(year, 5, 4)
- name: 振替休日
regions: [jp]
function: jp_substitute_holiday(year, 5, 5)
Expand Down Expand Up @@ -165,7 +169,14 @@ methods:
jp_substitute_holiday: |
def self.jp_substitute_holiday(*date)
date = date[0].kind_of?(Date) ? date.first : Date.civil(*date)
date.wday == 0 ? date+1 : nil
date.wday == 0 ? Holidays.jp_next_weekday(date+1) : nil
end
jp_next_weekday(date): |
def self.jp_next_weekday(date)
is_holiday = Holidays::JP.holidays_by_month[date.month].any? do |holiday|
holiday[:mday] == date.day
end
date.wday == 0 || is_holiday ? Holidays.jp_next_weekday(date+1) : date
end
tests: |
{Date.civil(2008,1,1) => '元日',
Expand All @@ -183,6 +194,9 @@ tests: |
Date.civil(2010,3,22) => '振替休日',
Date.civil(2008,11,24) => '振替休日',
Date.civil(2012,1,2) => '振替休日',
Date.civil(2013,5,6) => '振替休日',
Date.civil(2014,5,6) => '振替休日',
Date.civil(2015,5,6) => '振替休日'
}.each do |date, name|
assert_equal name, (Holidays.on(date, :jp, :informal)[0] || {})[:name]
end
Expand Down
11 changes: 10 additions & 1 deletion lib/holidays/jp.rb
Expand Up @@ -31,6 +31,7 @@ def self.holidays_by_month
{:mday => 4, :name => "みどりの日", :regions => [:jp]},
{:mday => 5, :name => "こどもの日", :regions => [:jp]},
{:function => lambda { |year| Holidays.jp_substitute_holiday(year, 5, 3) }, :function_id => "jp_substitute_holiday(year, 5, 3)", :name => "振替休日", :regions => [:jp]},
{:function => lambda { |year| Holidays.jp_substitute_holiday(year, 5, 4) }, :function_id => "jp_substitute_holiday(year, 5, 4)", :name => "振替休日", :regions => [:jp]},
{:function => lambda { |year| Holidays.jp_substitute_holiday(year, 5, 5) }, :function_id => "jp_substitute_holiday(year, 5, 5)", :name => "振替休日", :regions => [:jp]}],
7 => [{:wday => 1, :week => 3, :name => "海の日", :regions => [:jp]},
{:function => lambda { |year| Holidays.jp_substitute_holiday(year, 7, Date.calculate_mday(year, 7, 3, 1)) }, :function_id => "jp_substitute_holiday(year, 7, Date.calculate_mday(year, 7, 3, 1))", :name => "振替休日", :regions => [:jp]}],
Expand Down Expand Up @@ -104,7 +105,15 @@ def self.jp_citizons_holiday(year)

def self.jp_substitute_holiday(*date)
date = date[0].kind_of?(Date) ? date.first : Date.civil(*date)
date.wday == 0 ? date+1 : nil
date.wday == 0 ? Holidays.jp_next_weekday(date+1) : nil
end


def self.jp_next_weekday(date)
is_holiday = Holidays::JP.holidays_by_month[date.month].any? do |holiday|
holiday[:mday] == date.day
end
date.wday == 0 || is_holiday ? Holidays.jp_next_weekday(date+1) : date
end


Expand Down
3 changes: 3 additions & 0 deletions test/defs/test_defs_jp.rb
Expand Up @@ -22,6 +22,9 @@ def test_jp
Date.civil(2010,3,22) => '振替休日',
Date.civil(2008,11,24) => '振替休日',
Date.civil(2012,1,2) => '振替休日',
Date.civil(2013,5,6) => '振替休日',
Date.civil(2014,5,6) => '振替休日',
Date.civil(2015,5,6) => '振替休日'
}.each do |date, name|
assert_equal name, (Holidays.on(date, :jp, :informal)[0] || {})[:name]
end
Expand Down

0 comments on commit 965f232

Please sign in to comment.