Skip to Main Content
Learn to Program, 2nd Edition
book

Learn to Program, 2nd Edition

by Chris Pine
March 2009
Intermediate to advanced content levelIntermediate to advanced
194 pages
4h
English
Pragmatic Bookshelf
Content preview from Learn to Program, 2nd Edition

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; 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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn to Program, 3rd Edition

Learn to Program, 3rd Edition

Chris Pine
The Book of Ruby

The Book of Ruby

Huw Collingbourne

Publisher Resources

ISBN: 9781680500172Errata