class String

provide natural-language date / datetime parsing to strings

Public Instance Methods

to_chronic_date click to toggle source

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

# File lib/smarter_dates/chronic_strings.rb, line 22
def to_chronic_date
  obj = to_chronic_datetime
  obj && obj.to_date
end
to_chronic_datetime click to toggle source

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

# 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_time click to toggle source

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

# File lib/smarter_dates/chronic_strings.rb, line 32
def to_chronic_time
  obj = to_chronic_datetime
  obj && obj.to_time
end

Protected Instance Methods

parse_with_builtins click to toggle source

attempt to parse a string with DateTime failing that, attempt to parse a string with Date

# File lib/smarter_dates/chronic_strings.rb, line 55
def parse_with_builtins
  begin
    return DateTime.parse(self)
  rescue
    return Date.parse(self)
  end
end
parse_with_chronic click to toggle source

attempt to parse a string with Chronic

# File lib/smarter_dates/chronic_strings.rb, line 44
def parse_with_chronic
  #Chronic.time_class = Time.zone
  Chronic.parse(self)
end