Method: String#to_time
- Defined in:
- lib/active_support/core_ext/string/conversions.rb
#to_time(form = :local) ⇒ Object
Converts a string to a Time value. The form can be either :utc or :local (default :local).
The time is parsed using Time.parse method. If form is :local, then the time is in the system timezone. If the date part is missing then the current date is used and if the time part is missing then it is assumed to be 00:00:00.
"13-12-2012".to_time # => 2012-12-13 00:00:00 +0100
"06:12".to_time # => 2012-12-13 06:12:00 +0100
"2012-12-13 06:12".to_time # => 2012-12-13 06:12:00 +0100
"2012-12-13T06:12".to_time # => 2012-12-13 06:12:00 +0100
"2012-12-13T06:12".to_time(:utc) # => 2012-12-13 05:12:00 UTC
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_support/core_ext/string/conversions.rb', line 18 def to_time(form = :local) parts = Date._parse(self, false) return if parts.empty? now = Time.now time = Time.new( parts.fetch(:year, now.year), parts.fetch(:mon, now.month), parts.fetch(:mday, now.day), parts.fetch(:hour, 0), parts.fetch(:min, 0), parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0), parts.fetch(:offset, form == :utc ? 0 : nil) ) form == :utc ? time.utc : time.getlocal end |