Class: TaxJp::Gengou
- Inherits:
-
Object
- Object
- TaxJp::Gengou
- Defined in:
- lib/tax_jp/gengou.rb
Constant Summary collapse
Class Method Summary collapse
- .to_seireki(gengou, date_jp) ⇒ Object
- .to_wareki(date, only_date: false, format: '%y年%m月%d日') ⇒ Object
Class Method Details
.to_seireki(gengou, date_jp) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tax_jp/gengou.rb', line 7 def self.to_seireki(gengou, date_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 |
.to_wareki(date, only_date: false, format: '%y年%m月%d日') ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tax_jp/gengou.rb', line 22 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 |