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 (GMT, the One True Time Zone, dontcha know). This can be because of being in a different time zone or daylight saving time or who knows what else. So, you can see that I was born in daylight saving time, while it was not daylight saving time when Y2K struck. (By the way, the parentheses are to group the parameters to local together; otherwise, puts might start thinking those are its parameters.) The more parameters you add to local, the more accurate your time becomes.

On the other hand, if you want to avoid time zones and daylight saving time altogether and just use GMT, there’s always Time.gm.

puts Time.gm(1955, 11, 5) ​# A red-letter day.
1955-11-05 00:00:00 UTC

You can compare times using the comparison methods (an earlier time is less than a later time), and if you subtract one time from another, you’ll get the number of seconds between them. Play around with it!

If you happen to be using an older version of Ruby, there’s this problem with the Time class. It thinks the world began at epoch: the stroke of midnight, January 1, 1970, GMT. I don’t really know of any satisfying way of explaining this, but here goes: at some point, probably before I was even born, some people (Unix folks, I believe) decided that a good way to represent time on computers was to count the number of seconds since the very beginning of the 70s. So, time “zero” stood for the birth of that great decade, and they called it epoch.

Now this was all long before Ruby. In those ancient days (and programming in those ancient languages), you often had to worry about your numbers getting too large. In general, a number would either be from 0 to around 4 billion or be from -2 billion to +2 billion, depending on how they chose to store it.

For whatever reasons (compatibility, tradition, cruelty…whatever), older versions of Ruby decided to go with these conventions. So (and this is the important point), you couldn’t have times more than 2 billion seconds away from epoch! This restriction wasn’t too painful, though, because this span is from sometime in December 1901 to sometime in January 2038.

In all fairness, Ruby did (and still does) provide other classes, such as Date and DateTime, for handling just about any point in history. But these are such a pain to use compared to Time that I don’t feel like figuring them out myself, much less teaching them to you. What’s the difference between civil time and commercial time? I have no idea. Julian calendar vs. Gregorian calendar? Italian vs. English reform dates? I’m sure there’s a Perfectly Good Reason for all that complexity. (In case you weren’t sitting across from me as I typed that, I was rolling my eyes.) But anyway, that’s fixed in newer versions of Ruby (1.9 and up).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.123.208