fork download
  1. require 'date'
  2. f522 = -> s {Date.parse(s) rescue nil}
  3. a = %w(1970/01/05 1990/12/31 2019/04/28 2019/05/01 1920/02/29 0000/00/00 1912/07/29)
  4. p a.map {|s| [s, !!f522.(s)]}
  5.  
  6. h = {'T' => '大正', 'S' => '昭和', 'H' => '平成', 'R' => '令和'}
  7. g = -> y {y.to_i == 1 ? '元' : y}
  8. f521 = -> d {d&.jisx0301&.gsub!(/([TSHR])(\d+).(\d+).(\d+)/) {'%s%s年%s月%s日' % [h[$1], g[$2], $3, $4]} || 'ERROR'}
  9. p a.map {|s| [s, f521.(f522.(s))]}
  10.  
Success #stdin #stdout 0s 6748KB
stdin
Standard input is empty
stdout
[["1970/01/05", true], ["1990/12/31", true], ["2019/04/28", true], ["2019/05/01", true], ["1920/02/29", true], ["0000/00/00", false], ["1912/07/29", true]]
[["1970/01/05", "昭和45年01月05日"], ["1990/12/31", "平成02年12月31日"], ["2019/04/28", "平成31年04月28日"], ["2019/05/01", "平成31年05月01日"], ["1920/02/29", "大正09年02月29日"], ["0000/00/00", "ERROR"], ["1912/07/29", "ERROR"]]