Sha256: eef4b4f2f440b4a630158e50632fb554f91d7938a9794c91cc517bfad9c30126

Contents?: true

Size: 1.81 KB

Versions: 98

Compression:

Stored size: 1.81 KB

Contents

# encoding: utf-8
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, :offset).map { |arg| arg || 0 }
    d[6] *= 1000000
    ::Time.send("#{form}_time", *d[0..6]) - d[7]
  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

98 entries across 85 versions & 6 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.7.4 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.7.3 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.7.2 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.7.1 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.7.0 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.9 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.8 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.7 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.6 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.5 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.4 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.3 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.2 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.1 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.6.0 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.5.17 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.5.16 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.5.15 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb
classiccms-0.5.14 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/string/conversions.rb