Using Time.parse in Ruby
There are some Time
extension methods, for example Time.parse
, which are not
automatically imported into Ruby scripts as part of the standard library. To use
these methods you probably need to add this line to the top of your script:
require 'time'
Time.parse
converts a time or date into a Time
object:
Time.parse("12:30") #=> 2013-01-14 12:30:00 +0000
Time.parse("7/23") #=> 2013-07-23 00:00:00 +0000
Time.parse("Mar 30") #=> 2013-03-30 00:00:00 +0000
If elements are missing, the more significant ones are assumed to be equivalent to
Time.now
, e.g. the date in the first example or the year in the second and third
examples above. The less significant ones are assumed to be zeros, e.g. the time in
the second and third examples or the seconds in the first example above.
A second parameter can be passed to set a different time to Time.now
.
Time.parse("12:30", Time.gm(2012, 01, 01))
#=> 2012-01-01 12:30:00 +0000
You can see full documentation for this and the other additional Time
methods
in the source of your Ruby distribution at lib/ruby/1.9.1/time.rb
(the exact
location depends on your Ruby version and distribution). Or you can see it at
Rubydoc or the
Rubinius Github distribution.
See also the standard library Time
class
reference (for Ruby 1.9.3).