FORMAT | = | { :db => "%Y-%m-%d %H:%M:%S", :dbase => "%Y-%m-%d %H:%M:%S", :datbase => "%Y-%m-%d %H:%M:%S", :utc => "%Y-%m-%d %H:%M:%S", :number => "%Y%m%d%H%M%S", :short => "%d %b %H:%M", :time => "%H:%M", :long => "%B %d, %Y %H:%M", :day1st => "%d-%m-%Y %H:%M", :dmYHM => "%d-%m-%Y %H:%M", :rfc822 => "%a, %d %b %Y %H:%M:%S %z", nil => "%a %b %d %H:%M:%S %Z %Y" |
Returns a new Time where one or more of the elements have been changed according to the options parameter. The time options (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and minute is passed, then sec and usec is set to 0.
t1 = Time.at(10000) t1.ctime #=> "Wed Dec 31 21:46:40 1969" t2 = t1.change(:hour => 11) t2.ctime #=> "Wed Dec 31 11:00:00 1969"
Adjust DST
TODO: Can‘t seem to get this to pass ActiveSupport tests, even though it is essentially identical to the ActiveSupport code (see Time#since in time/calculations.rb). It handles all but 4 tests.
Round time at the nearest range (in seconds).
t1 = Time.now t2 = t1.round_to(60*60) t2.min #=> 0 t2.sec #=> 0
TODO: What about `round(:minute)`?
TODO: Fractional seconds should round the usec.
Returns a new Time representing the time shifted by the time-units given. Positive number shift the time forward, negative number shift the time backward.
t = Time.utc(2010,10,10,0,0,0) t.shift( 4, :days) #=> Time.utc(2010,10,14,0,0,0) t.shift(-4, :days) #=> Time.utc(2010,10,6,0,0,0)
More than one unit of time can be given.
t.shift(4, :days, 3, :hours) #=> Time.utc(2010,10,14,3,0,0)
The shift method can also take a hash.
t.shift(:days=>4, :hours=>3) #=> Time.utc(2010,10,14,3,0,0)
Create a time stamp.
t = Time.at(10000) t.stamp(:short) #=> "31 Dec 21:46"
Supported formats come from the Time::FORMAT constant.
CREDIT: Trans
Converts a Time instance to a Ruby DateTime instance, preserving UTC offset.
my_time = Time.now # Mon Nov 12 23:04:21 -0500 2007 my_time.to_datetime # Mon, 12 Nov 2007 23:04:21 -0500 your_time = Time.parse("1/13/2009 1:13:03 P.M.") # Tue Jan 13 13:13:03 -0500 2009 your_time.to_datetime # Tue, 13 Jan 2009 13:13:03 -0500
Truncate time at give range (in seconds).
t = Time.now t = t.trunc(60*60) t.min #=> 0 t.sec #=> 0