The Time Class
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. So, adding 1.5 to a time object makes a new time one-and-a-half seconds later:
1: | time = Time.new # The moment this code was run. |
2: | soon = time + 60 # One minute later. |
3: | |
4: | puts time |
5: | puts soon |
<= | 2020-10-10 11:57:23 -0700 |
| 2020-10-10 11:58:23 -0700 |
You can also pass values into Time.new to construct a specific time:
1: | puts Time.new(2000, 1, 1) # Y2K. |
2: | puts Time.new(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 -0700 and -0800 in these times. That’s to account for the difference ...
Get Learn to Program, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.