Class: Bizside::Gengou

Inherits:
Object
  • Object
show all
Defined in:
lib/bizside/gengou.rb

Constant Summary collapse

@@_gengou =
YAML.load_file(File.join(File.dirname(__FILE__), 'gengou.yml'))

Class Method Summary collapse

Class Method Details

.to_seireki(gengou, year_jp) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bizside/gengou.rb', line 7

def self.to_seireki(gengou, year_jp)
  # 引数 year_jpには年度の値が入る
  target_gengou = nil
  @@_gengou.invert.keys.each do |start_gengou|
    if start_gengou == gengou.to_s
      target_gengou = start_gengou
      break
    end
  end

  return nil unless target_gengou

  start_year = @@_gengou.invert[target_gengou].to_s
  return (start_year.to_i + year_jp.to_i - 1).to_s
end

.to_wareki(date) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bizside/gengou.rb', line 23

def self.to_wareki(date)
  return if date.to_s.empty?

  match = /^(\d{4})(\d{2})?(\d{2})?$/.match(date.to_s)
  match ||= /^(\d{4})(?:[-\/](\d{1,2}))?(?:[-\/](\d{1,2}))?$/.match(date.to_s)
  match ||= /^(\d{4})年(?:(\d{1,2})月)?(?:(\d{1,2})日)?$/.match(date.to_s)
  if match
    year, month, day = match.to_a[1..3].map { |m| m&.to_i }
    return unless Date.valid_date?(year, month || 1, day || 1)
    date = Date.new(year, month || 1, day || 1)
    date = date.end_of_month if day.nil?
    date = date.end_of_year if month.nil?

    start_date, gengou = @@_gengou.sort { |a, b| b[0] <=> a[0] }.detect { |k, _| k <= date }

    return if start_date.nil?

    year = date.year - start_date.year
    "#{gengou}#{(year.zero? ? "" : year + 1)}"
  end
end