Sha256: 6e590534ee4461e17f67bae34bbd800f757643317cac53ae06c6eb0ce4911d8e

Contents?: true

Size: 1000 Bytes

Versions: 17

Compression:

Stored size: 1000 Bytes

Contents

require 'yaml'

module TaxJp
  class Gengou
    @@_gengou = TaxJp::Utils.load_yaml('元号.yml')
  
    def self.to_seireki(gengou, 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_i
      return (start_year + year_jp.to_i - 1).to_s
    end
  
    def self.to_wareki(year, only_year: false)
      return nil unless year.present?

      target_year = nil
      @@_gengou.keys.sort.each do |start_year|
        break if start_year.to_i > year.to_i
        target_year = start_year
      end

      return nil unless target_year

      gengou  = @@_gengou[target_year]
      year_jp = year - target_year + 1

      if only_year
        year_jp.to_s
      else
        gengou + (year_jp == 1 ? '元' : year_jp.to_s)
      end
    end

  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
tax_jp-0.6.2 lib/tax_jp/gengou.rb
tax_jp-0.6.1 lib/tax_jp/gengou.rb
tax_jp-0.6.0 lib/tax_jp/gengou.rb
tax_jp-0.5.6 lib/tax_jp/gengou.rb
tax_jp-0.5.5 lib/tax_jp/gengou.rb
tax_jp-0.5.4 lib/tax_jp/gengou.rb
tax_jp-0.5.3 lib/tax_jp/gengou.rb
tax_jp-0.5.2 lib/tax_jp/gengou.rb
tax_jp-0.5.1 lib/tax_jp/gengou.rb
tax_jp-0.4.0 lib/tax_jp/gengou.rb
tax_jp-0.3.13 lib/tax_jp/gengou.rb
tax_jp-0.3.12 lib/tax_jp/gengou.rb
tax_jp-0.3.11 lib/tax_jp/gengou.rb
tax_jp-0.3.10 lib/tax_jp/gengou.rb
tax_jp-0.3.9 lib/tax_jp/gengou.rb
tax_jp-0.3.8 lib/tax_jp/gengou.rb
tax_jp-0.3.7 lib/tax_jp/gengou.rb