Ruby Date & Time
The Time class represents dates and times in Ruby. It is a thin layer over the system date and time functionality provided by the operating system. This class may have the same limitations as your operating system.
This tutorial will make you familiar with all the most wanted concepts of date and time.
Getting Current Date and Time:
Following is the simple example to get current date and time:
time1 = Time.new
puts "Current Time : #{time1.inspect}"
# Time.now is a synonym:
time2 = Time.now
puts "Current Time : #{time2.inspect}"
This will produce following result:
Current Time : Mon Jun 02 12:02:39 -0700 2008
Current Time : Mon Jun 02 12:02:39 -0700 2008
Getting components of a Date & Time:
We can use Time object to get various components of date and time. Following is the example showing the same:
ctime = Time.new
puts "If current time is: #{ctime}"
outputs = [ :year, :month, :day, :wday, :yday,
:hour, :min, :sec, :usec, :zone ]
outputs.each do |method|
puts "ctime.#{method} will produce #{ctime.send(method)}"
end
This will produce following result:
If current time is: Sat Jun 05 02:04:21 -0300 2010
ctime.year will produce 2010
ctime.month will produce 6
ctime.day will produce 5
ctime.wday will produce 6
ctime.yday will produce 156
ctime.hour will produce 2
ctime.min will produce 4
ctime.sec will produce 21
ctime.usec will produce 140625
ctime.zone will produce E. South America Standard Time
Time.utc, Time.gm and Time.local Functions:
These two functions can be used to format date in standard format as follows:
# July 8, 2008
Time.local(2008, 7, 8)
# July 8, 2008, 09:10am, local time
Time.local(2008, 7, 8, 9, 10)
# July 8, 2008, 09:10 UTC
Time.utc(2008, 7, 8, 9, 10)
# July 8, 2008, 09:10:11 GMT (same as UTC)
Time.gm(2008, 7, 8, 9, 10, 11)
Following is the example to get all components in an array in the following format:
[sec,min,hour,day,month,year,wday,yday,isdst,zone]
Try the following:
time = Time.new
values = time.to_a
p values
This will generate following result:
[26, 10, 12, 2, 6, 2008, 1, 154, false, "MST"]
This array could be passed to Time.utc or Time.local functions to get different format of dates as follows:
time = Time.new
values = time.to_a
puts Time.utc(*values)
This will generate following result:
Mon Jun 02 12:15:36 UTC 2008
Following is the way to get time represented internally as seconds since the (platform-dependent) epoch:
# Returns number of seconds since epoch
time = Time.now.to_i
# Convert number of seconds into Time object.
Time.at(time)
# Returns second since epoch which includes microseconds
time = Time.now.to_f
Timezones and daylight savings time:
You can use a Time object to get all the information related to Timezones and daylight savings as follows:
time = Time.new
# Here is the interpretation
time.zone # => "UTC": return the timezone
time.utc_offset # => 0: UTC is 0 seconds offset from UTC
time.zone # => "PST" (or whatever your timezone is)
time.isdst # => false: If UTC does not have DST.
time.utc? # => true: if t is in UTC time zone
time.localtime # Convert to local timezone.
time.gmtime # Convert back to UTC.
time.getlocal # Return a new Time object in local zone
time.getutc # Return a new Time object in UTC
Formatting Times and Dates:
There are various ways to format date and time. Here is one example showing a few:
time = Time.new
puts time.to_s
puts time.ctime
puts time.localtime
puts time.strftime("%Y-%m-%d %H:%M:%S")
This will produce following result:
Mon Jun 02 12:35:19 -0700 2008
Mon Jun 2 12:35:19 2008
Mon Jun 02 12:35:19 -0700 2008
2008-06-02 12:35:19
Time arithmetic:
You can do simple arithmetic with time as follows:
now = Time.now # Current time
past = now - 10 # 10 seconds ago. Time - number => Time
future = now + 10 # 10 seconds from now Time + number => Time
future - now # => 10 Time - Time => number of seconds
Date and DateTime classes:
There are many Date and Time operations that could be performed using these classes:
date = Date.new(2008, 12, 22)
puts date
dtime = DateTime.new(2009, 7, 16, 14, 30)
puts dtime
# Convert a UTC offset measured in minutes to one measured in a
# fraction of a day.
offset = Rational(-14453, 60 * 60 * 24)
today = DateTime.new(2010, 6, 5, 02, 26, 40, offset)
puts today
birthday = Date.new(2010, 11, 23)
puts birthday
time_until = birthday - today
hours,minutes,seconds,frac = Date.day_fraction_to_time(time_until)
puts "My birthday will be in:"
puts "#{hours} hours, #{minutes} minutes and #{seconds} seconds."
This will produce following result:
2008-12-22
2009-07-16T14:30:00+00:00
2010-06-05T02:26:40-04:00
2010-11-23
My birthday will be in:
4097 hours, 32 minutes and 27 seconds.


