Sha256: bfc91182c50a5e1f819fd8565cc3da9e516be25136abb3b5242f582d1e000352

Contents?: true

Size: 1.37 KB

Versions: 40

Compression:

Stored size: 1.37 KB

Contents

require 'yaml'

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

    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

    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

  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
bizside-2.3.9 lib/bizside/gengou.rb
bizside-3.0.3 lib/bizside/gengou.rb
bizside-2.3.8 lib/bizside/gengou.rb
bizside-3.0.2 lib/bizside/gengou.rb
bizside-3.0.1 lib/bizside/gengou.rb
bizside-3.0.0 lib/bizside/gengou.rb
bizside-2.3.7 lib/bizside/gengou.rb
bizside-2.3.6 lib/bizside/gengou.rb
bizside-2.3.5 lib/bizside/gengou.rb
bizside-2.3.4 lib/bizside/gengou.rb
bizside-2.3.3 lib/bizside/gengou.rb
bizside-2.3.2 lib/bizside/gengou.rb
bizside-2.3.1 lib/bizside/gengou.rb
bizside-2.3.0 lib/bizside/gengou.rb
bizside-2.2.3 lib/bizside/gengou.rb
bizside-2.2.2 lib/bizside/gengou.rb
bizside-2.2.1 lib/bizside/gengou.rb
bizside-2.2.0 lib/bizside/gengou.rb
bizside-2.1.12 lib/bizside/gengou.rb
bizside-2.1.11 lib/bizside/gengou.rb