module HolidayJapan

Constants

DATA

祝日データ: 1948年7月20日以降で有効

FURIKAE_START
INF
SAT
TABLE
VERSION
WEEK1
WEEK2
WEEK3
WEEK4

Public Instance Methods

_print_year(year) click to toggle source
# File lib/holiday_japan.rb, line 187
def _print_year(year)
  puts "listing year #{year}..."
  list_year(year).each do |y|
    puts "#{y[0].strftime('%Y-%m-%d %a')} #{y[1]}"
  end
end
between(from_date,to_date) click to toggle source
# File lib/holiday_japan.rb, line 149
def between(from_date,to_date)
  case from_date
  when String
    from_date = Date.parse(from_date)
  when Integer
    from_date = Date.new(from_date,1,1)
  when Date
  else
    raise ArgumentError, "invalid type fpr from_date"
  end
  case to_date
  when String
    to_date = Date.parse(to_date)
  when Integer
    to_date = Date.new(to_date,12,31)
  when Date
  else
    raise ArgumentError, "invalid type fpr to_date"
  end
  if from_date > to_date
    raise ArgumentError, "to_date is earlier than from_date"
  end
  y1 = from_date.year
  y2 = to_date.year
  if y1 == y2
    result = hash_year(y1).select{|d,n| d >= from_date && d <= to_date}
  else
    result = hash_year(y1).select{|d,n| d >= from_date}
    y = y1 + 1
    while y < y2
      result.merge!(hash_year(y))
      y += 1
    end
    hash_year(y).each{|d,n| result[d]=n if d <= to_date}
  end
  result
end
check(date) click to toggle source
# File lib/holiday_japan.rb, line 135
def check(date)
  !HolidayJapan.name(date).nil?
end
create_table(y) click to toggle source
# File lib/holiday_japan.rb, line 94
def create_table(y)
  h={}
  a=[]
  # list holidays
  DATA.each do |x|
    if d = holiday_date(y,x)
      h[d] = x[0]
      a << d
    end
  end
  # compensating holiday
  if y >= 2007
    a.each do |d|
      if d.wday==SUN
        d+=1 while h[d]
        h[d] = "振替休日"
      end
    end
  elsif y >= 1973
    a.each do |d|
      if d.wday==SUN and d>=FURIKAE_START
        h[d+1] = "振替休日"
      end
    end
  end
  # consecutive holiday
  if y >= 1986
    a.each do |d|
      if h[d+2] and !h[d+1] and d.wday!=SAT
        h[d+1] = "国民の休日"
      end
    end
  end
  Hash[h.sort_by{|d,| d}].freeze
end
hash_year(year) click to toggle source
# File lib/holiday_japan.rb, line 145
def hash_year(year)
  TABLE[year] ||= create_table(year)
end
holiday_date(year,data) click to toggle source
# File lib/holiday_japan.rb, line 71
def holiday_date(year,data)
  data.each do |item|
    next if item.kind_of?(String)
    year_range,mon,day = *item
    if year_range === year
      case day
      when Integer
        # skip
      when Array
        day0,wday = day
        wday0 = Date.new(year,mon,day0).wday
        day = day0+(wday-wday0+7)%7
      when Proc
        day = day.call(year)
      else
        raise "invalid holiday data"
      end
      return Date.new( year, mon, day )
    end
  end
  nil
end
list_year(year) click to toggle source
# File lib/holiday_japan.rb, line 139
def list_year(year)
  year = Integer(year)
  TABLE[year] ||= create_table(year)
  TABLE[year].sort_by{|x| x[0]}
end
name(date) click to toggle source
# File lib/holiday_japan.rb, line 130
def name(date)
  y = date.year
  (TABLE[y] ||= create_table(y))[date]
end
print_between(from_date,to_date) click to toggle source
print_year(year) click to toggle source