Chapter 7. Working with Times and Dates
One of the most complex and confusing areas of human life is that of measuring time. To come to a complete understanding of the subject, you would need to study physics, astronomy, history, law, business, and religion. Astronomers know (as most of us don't!) that solar time and sidereal time are not quite the same thing, and why a "leap second" is occasionally added to the end of the year. Historians know that the calendar skipped several days in October 1582, when Italy converted from the Julian calendar to the Gregorian. Few people know the difference between astronomical Easter and ecclesiastical Easter (which are almost always the same). Many people don't know that century years not divisible by 400 (such as the year 1900) are not leap years.
Performing calculations with times and dates is common in computing but has traditionally been somewhat tedious in most programming languages. It is tedious in Ruby, too, because of the nature of the data. However, Ruby has taken some incremental steps toward making these operations easier.
As a courtesy to the reader, we'll go over a few terms that may not be familiar to everyone. Most of these come from standard usage or from other programming languages.
Greenwich Mean Time (GMT) is an old term not really in official use anymore. The new global standard is Coordinated Universal Time (or UTC, from the French version of the name). GMT and UTC are virtually the same thing; over a period of years, the difference will be on the order of seconds. Much of the software in the industry does not distinguish between the two at all (nor does Ruby).
Daylight Saving Time is semiannual shift in the official time, amounting to a difference of one hour. Thus the U.S. time zones usually end in ST (Standard Time) or DT (Daylight Time). This annoying trick is used in most (though not all) of the United States and in many other countries.
The epoch is a term borrowed from UNIX lore. In this realm, a time is typically stored internally as a number of seconds from a specific point in time (called the epoch), which was midnight January 1, 1970, GMT. (Note that in U.S. time zones, this will actually be the preceding December 31.) The same term is used to denote not only the point of origin but also the distance in time from that point.
The Time class is used for most operations. The Date and DateTime classes provide some extra flexibility. Let's look at some common uses of these.