aboutsummaryrefslogblamecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/string/conversions.rb
blob: d70f5d7241089bcfc1e9bd54a69e5358c9a30b3c (plain) (tree)
1
2
3
4
5
6
7
8
9
10
              
                                                                   
                                                   
 
            




                                                                                              

           
                                  
 

                                                


                                                                                                                         
     
 


                                                                         
 
                 


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

class String
  # Returns the ASCII code of the first character of the string, assuming it belongs to ASCII.
  #
  # This method is defined for Ruby 1.9 forward compatibility on ASCII characters.
  #
  # See also <tt>ActiveSupport::Multibyte::Chars#ord</tt>.
  def ord
    self[0]
  end unless method_defined?(:ord)

  # Form can be either :utc (default) or :local.
  def to_time(form = :utc)
    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
    ::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
  end

  def to_datetime
    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