Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/smarter_dates/chronic_strings.rb

Overview

provide natural-language date / datetime parsing to strings

Instance Method Summary collapse

Instance Method Details

#to_chronic_dateObject

:call-seq: to_chronic_date

Parses a string into a Date object using the same methods as .to_chronic_datetime



22
23
24
25
# File 'lib/smarter_dates/chronic_strings.rb', line 22

def to_chronic_date
  obj = to_chronic_datetime
  obj && obj.to_date
end

#to_chronic_datetimeObject

:call-seq: to_chronic_datetime

Parses a string into a DateTime object using the Chronic gem if available. If not, try parsing the string using :parse_with_builtins raise an error if the string fails to parse



10
11
12
13
14
15
# File 'lib/smarter_dates/chronic_strings.rb', line 10

def to_chronic_datetime
  dt = defined?(Chronic) ? parse_with_chronic : parse_with_builtins
  #raise RuntimeError, "#{dt.inspect} unparsable Date/DateTime" unless dt
  return unless dt
  dt.to_datetime
end

#to_chronic_timeObject

:call-seq: to_chronic_time

Parses a string into a Time object using the same methods as .to_chronic_datetime



32
33
34
35
# File 'lib/smarter_dates/chronic_strings.rb', line 32

def to_chronic_time
  obj = to_chronic_datetime
  obj && obj.to_time
end