Sha256: 291c064b3b0607495ad935e11f1ef02ad75bf626c6092f9f6fb2c39bc254e985

Contents?: true

Size: 1.42 KB

Versions: 41

Compression:

Stored size: 1.42 KB

Contents

# frozen-string-literal: true
#
# The string_date_time extension provides String instance methods
# for converting the strings to a date (e.g. String#to_date), allowing
# for backwards compatibility with legacy Sequel code.
#
# These methods calls +parse+ on the related class, and as such, can
# result in denial of service in older versions of Ruby for large
# untrusted input, and raise exceptions in newer versions of Ruby. 
#
# To load the extension:
#
#   Sequel.extension :string_date_time

class String
  # Converts a string into a Date object.
  def to_date
    Date.parse(self, Sequel.convert_two_digit_years)
  rescue => e
    raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
  end

  # Converts a string into a DateTime object.
  def to_datetime
    DateTime.parse(self, Sequel.convert_two_digit_years)
  rescue => e
    raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
  end

  # Converts a string into a Time or DateTime object, depending on the
  # value of Sequel.datetime_class
  def to_sequel_time
    if Sequel.datetime_class == DateTime
      DateTime.parse(self, Sequel.convert_two_digit_years)
    else
      Sequel.datetime_class.parse(self)
    end
  rescue => e
    raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
  end

  # Converts a string into a Time object.
  def to_time
    Time.parse(self)
  rescue => e
    raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
  end
end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
sequel-5.86.0 lib/sequel/extensions/string_date_time.rb
sequel-5.85.0 lib/sequel/extensions/string_date_time.rb
sequel-5.84.0 lib/sequel/extensions/string_date_time.rb
sequel-5.83.1 lib/sequel/extensions/string_date_time.rb
sequel-5.83.0 lib/sequel/extensions/string_date_time.rb
sequel-5.82.0 lib/sequel/extensions/string_date_time.rb
sequel-5.81.0 lib/sequel/extensions/string_date_time.rb
sequel-5.80.0 lib/sequel/extensions/string_date_time.rb
sequel-5.79.0 lib/sequel/extensions/string_date_time.rb
sequel-5.78.0 lib/sequel/extensions/string_date_time.rb
sequel-5.77.0 lib/sequel/extensions/string_date_time.rb
sequel-5.76.0 lib/sequel/extensions/string_date_time.rb
sequel-5.75.0 lib/sequel/extensions/string_date_time.rb
sequel-5.74.0 lib/sequel/extensions/string_date_time.rb
sequel-5.73.0 lib/sequel/extensions/string_date_time.rb
sequel-5.72.0 lib/sequel/extensions/string_date_time.rb
sequel-5.71.0 lib/sequel/extensions/string_date_time.rb
sequel-5.70.0 lib/sequel/extensions/string_date_time.rb
sequel-5.69.0 lib/sequel/extensions/string_date_time.rb
sequel-5.68.0 lib/sequel/extensions/string_date_time.rb