March 2009
Intermediate to advanced
194 pages
4h
English
What’s the story with this Time class? Time objects represent (you guessed it) moments in time. You can add (or subtract) numbers to (or from) times to get new times; adding 1.5 to a time makes a new time one-and-a-half seconds later:
time = Time.new # The moment we ran this code. |
time2 = time + 60 # One minute later. |
puts time |
puts time2 |
2013-06-15 23:01:14 -0700 |
2013-06-15 23:02:14 -0700 |
You can also make a time for a specific moment using Time.local:
puts Time.local(2000, 1, 1) # Y2K. |
puts Time.local(1976, 8, 3, 13, 31) # When I was born. |
2000-01-01 00:00:00 -0800 |
1976-08-03 13:31:00 -0700 |
You’ll notice the -0800 and -0900 in these times. That’s to account for the difference between the local time and Greenwich mean time ...