Sha256: dc2cd7fa2a6799a9e7b0e387b1a8b0f42f2a33207484a34d01f5077d5c3c9683

Contents?: true

Size: 1.77 KB

Versions: 47

Compression:

Stored size: 1.77 KB

Contents

require 'date'
require 'active_support/core_ext/time/publicize_conversion_methods'
require 'active_support/core_ext/time/calculations'

class String
  # Returns the codepoint of the first character of the string, assuming a
  # single-byte character encoding:
  #
  #   "a".ord # => 97
  #   "à".ord # => 224, in ISO-8859-1
  #
  # This method is defined in Ruby 1.8 for Ruby 1.9 forward compatibility on
  # these character encodings.
  #
  # <tt>ActiveSupport::Multibyte::Chars#ord</tt> is forward compatible with
  # Ruby 1.9 on UTF8 strings:
  #
  #   "a".mb_chars.ord # => 97
  #   "à".mb_chars.ord # => 224, in UTF8
  #
  # Note that the 224 is different in both examples. In ISO-8859-1 "à" is
  # represented as a single byte, 224. In UTF8 it is represented with two
  # bytes, namely 195 and 160, but its Unicode codepoint is 224. If we
  # call +ord+ on the UTF8 string "à" the return value will be 195. That is
  # not an error, because UTF8 is unsupported, the call itself would be
  # bogus.
  def ord
    self[0]
  end unless method_defined?(:ord)

  # +getbyte+ backport from Ruby 1.9
  alias_method :getbyte, :[] unless method_defined?(:getbyte)

  # Form can be either :utc (default) or :local.
  def to_time(form = :utc)
    return nil if self.blank?
    d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction).map { |arg| arg || 0 }
    d[6] *= 1000000
    ::Time.send("#{form}_time", *d)
  end

  def to_date
    return nil if self.blank?
    ::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
  end

  def to_datetime
    return nil if self.blank?
    d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).map { |arg| arg || 0 }
    d[5] += d.pop
    ::DateTime.civil(*d)
  end
end

Version data entries

47 entries across 47 versions & 3 rubygems

Version Path
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.20 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.19 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.18 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.17 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.16 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.15 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.14 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.13 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.13.rc1 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.12 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.12.rc1 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.11 lib/active_support/core_ext/string/conversions.rb
messagebus_ruby_api-0.4.7 spec/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/core_ext/string/conversions.rb
messagebus_ruby_api-0.4.4 spec/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.10 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.10.rc1 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.9 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.9.rc5 lib/active_support/core_ext/string/conversions.rb
activesupport-3.0.9.rc4 lib/active_support/core_ext/string/conversions.rb