Module: FeCoreExt::CoreExt::DateClassMethods

Included in:
Date
Defined in:
lib/fe_core_ext/core_ext/date.rb

Instance Method Summary collapse

Instance Method Details

#parsable?(string) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/fe_core_ext/core_ext/date.rb', line 31

def parsable?(string)
  parsed_hash = _parse(string)
  return true if parsed_hash.has_key?(:year) && parsed_hash.has_key?(:mon)

  false
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(::Regexp.last_match(1).to_i + 1988, ::Regexp.last_match(2).to_i, ::Regexp.last_match(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(::Regexp.last_match(1).to_i, ::Regexp.last_match(2).to_i, ::Regexp.last_match(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 ::Regexp.last_match(1) == "元"
    year ||= ::Regexp.last_match(1).to_i
    Date.new(year + 2018, ::Regexp.last_match(2).to_i, ::Regexp.last_match(3).to_i)
  end
end