Sha256: 688a868b5a1eb66fd7f7a963eaa0b7a2287cacb3d9617d1ecb70167d03f03827

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# provide natural-language date / datetime parsing to strings
class String
  # :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

  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

  # :call-seq:
  # to_chronic_date
  #
  # Parses a string into a Date object using the same methods as .to_chronic_datetime

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

  # :call-seq:
  # to_chronic_time
  #
  # Parses a string into a Time object using the same methods as .to_chronic_datetime

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

  protected

  # :call-seq:
  # parse_with_chronic
  #
  # attempt to parse a string with Chronic

  def parse_with_chronic
    Chronic.time_class = Time.zone
    Chronic.parse(self)
  end

  # :call-seq:
  # parse_with_builtins
  #
  # attempt to parse a string with DateTime
  # failing that, attempt to parse a string with Date

  def parse_with_builtins
    begin
      return DateTime.parse(self)
    rescue
      return Date.parse(self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smarter_dates-0.2.13 lib/smarter_dates/chronic_strings.rb