Module: FeCoreExt::CoreExt::DateClassMethods
- Included in:
- Date
- Defined in:
- lib/fe_core_ext/core_ext/date.rb
Instance Method Summary collapse
- #parsable?(string) ⇒ Boolean
- #parse_as_future(string) ⇒ Object
- #parse_gengo(string) ⇒ Object
- #parse_heisei(string) ⇒ Object
- #parse_ja(string) ⇒ Object
- #parse_nengappi(string) ⇒ Object
- #parse_reiwa(string) ⇒ Object
Instance Method Details
#parsable?(string) ⇒ Boolean
29 30 31 32 33 34 35 36 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 29 def parsable?(string) begin parse(string) true rescue ArgumentError false end end |
#parse_as_future(string) ⇒ Object
38 39 40 41 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 38 def parse_as_future(string) date = parse(string) date > current ? date : date + 1.year end |
#parse_gengo(string) ⇒ Object
57 58 59 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 57 def parse_gengo(string) parse_heisei(string) || parse_reiwa(string) end |
#parse_heisei(string) ⇒ Object
43 44 45 46 47 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 43 def parse_heisei(string) string.match('平成(\d+)年(\d+)月(\d+)日') do Date.new($1.to_i + 1988, $2.to_i, $3.to_i) end end |
#parse_ja(string) ⇒ Object
67 68 69 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 67 def parse_ja(string) parse_nengappi(string) || parse_gengo(string) end |
#parse_nengappi(string) ⇒ Object
61 62 63 64 65 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 61 def parse_nengappi(string) string.match(/(\d{4})年(\d+)月(\d+)日/) do Date.new($1.to_i, $2.to_i, $3.to_i) end end |
#parse_reiwa(string) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 49 def parse_reiwa(string) string.match('令和(\S+)年(\d+)月(\d+)日') do year = 1 if $1 == '元' year ||= $1.to_i Date.new(year + 2018, $2.to_i, $3.to_i) end end |