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
22 23 24 25 26 27 28 29 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 22 def parsable?(string) begin parse(string) true rescue ArgumentError false end end |
#parse_as_future(string) ⇒ Object
31 32 33 34 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 31 def parse_as_future(string) date = parse(string) date > current ? date : date + 1.year end |
#parse_gengo(string) ⇒ Object
50 51 52 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 50 def parse_gengo(string) parse_heisei(string) || parse_reiwa(string) end |
#parse_heisei(string) ⇒ Object
36 37 38 39 40 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 36 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
60 61 62 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 60 def parse_ja(string) parse_nengappi(string) || parse_gengo(string) end |
#parse_nengappi(string) ⇒ Object
54 55 56 57 58 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 54 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
42 43 44 45 46 47 48 |
# File 'lib/fe_core_ext/core_ext/date.rb', line 42 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 |