Sha256: f4b9e0cc1020b94e34078100419517c8f8eda5621a639294f240571ea13a5717

Contents?: true

Size: 1.22 KB

Versions: 28

Compression:

Stored size: 1.22 KB

Contents

require 'yaml'

module TaxJp
  class Gengou
    @@_gengou = TaxJp::Utils.load_yaml('元号.yml')
  
    def self.to_seireki(gengou, date_jp)
      target_gengou = @@_gengou.invert.keys.find{|start_gengou| start_gengou == gengou.to_s }
      return nil unless target_gengou

      start_date = @@_gengou.invert[target_gengou]
      Date.new(start_date.year + date_jp.year - 1, date_jp.month, date_jp.day)
    end
  
    def self.to_wareki(date, only_date: false, format: '%y年%m月%d日')
      return nil unless date.present?

      date = TaxJp::Utils.convert_to_date(date)

      target_date = nil
      @@_gengou.keys.each do |start_date|
        target_date = TaxJp::Utils.convert_to_date(start_date)
        break if date >= target_date
      end

      return nil unless target_date

      target_date = Date.strptime(target_date)
      year, month, day = *(date.split('-').map{|x| x.to_i})
      year_jp = year - target_date.year + 1

      ret = Date.new(year_jp, month, day)
      if only_date
        ret = ret.strftime(format)
      else
        gengou  = @@_gengou[target_date]
        year_jp = year_jp == 1 ? '元' : year_jp.to_s
        ret = ret.strftime(format.gsub('%y', "#{gengou}#{year_jp}"))
      end

      ret
    end

  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
tax_jp-1.3.0 lib/tax_jp/gengou.rb
tax_jp-1.2.12 lib/tax_jp/gengou.rb
tax_jp-1.2.11 lib/tax_jp/gengou.rb
tax_jp-1.2.10 lib/tax_jp/gengou.rb
tax_jp-1.2.9 lib/tax_jp/gengou.rb
tax_jp-1.2.8 lib/tax_jp/gengou.rb
tax_jp-1.2.7 lib/tax_jp/gengou.rb
tax_jp-1.2.6 lib/tax_jp/gengou.rb
tax_jp-1.2.5 lib/tax_jp/gengou.rb
tax_jp-1.2.4 lib/tax_jp/gengou.rb
tax_jp-1.2.3 lib/tax_jp/gengou.rb
tax_jp-1.2.2 lib/tax_jp/gengou.rb
tax_jp-1.2.1 lib/tax_jp/gengou.rb
tax_jp-1.2.0 lib/tax_jp/gengou.rb
tax_jp-1.1.12 lib/tax_jp/gengou.rb
tax_jp-1.1.11 lib/tax_jp/gengou.rb
tax_jp-1.1.10 lib/tax_jp/gengou.rb
tax_jp-1.1.9 lib/tax_jp/gengou.rb
tax_jp-1.1.8 lib/tax_jp/gengou.rb
tax_jp-1.1.7 lib/tax_jp/gengou.rb