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)


29
30
31
32
33
# File 'lib/fe_core_ext/core_ext/date.rb', line 29

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



35
36
37
38
# File 'lib/fe_core_ext/core_ext/date.rb', line 35

def parse_as_future(string)
  date = parse(string)
  date > current ? date : date + 1.year
end

#parse_gengo(string) ⇒ Object



54
55
56
# File 'lib/fe_core_ext/core_ext/date.rb', line 54

def parse_gengo(string)
  parse_heisei(string) || parse_reiwa(string)
end

#parse_heisei(string) ⇒ Object



40
41
42
43
44
# File 'lib/fe_core_ext/core_ext/date.rb', line 40

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



64
65
66
# File 'lib/fe_core_ext/core_ext/date.rb', line 64

def parse_ja(string)
  parse_nengappi(string) || parse_gengo(string)
end

#parse_nengappi(string) ⇒ Object



58
59
60
61
62
# File 'lib/fe_core_ext/core_ext/date.rb', line 58

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



46
47
48
49
50
51
52
# File 'lib/fe_core_ext/core_ext/date.rb', line 46

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