Add Book to My BookshelfPurchase This Book Online

Chapter 7 - Time of Day Operations

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

The Complexities of Time
Converting a quantity such as the number of seconds since some epoch time into a date and time string usable by humans is an extraordinarily difficult problem. If everyone used Coordinated Universal Time, it would be fairly simple. Divide the number of seconds since the epoch by 86,400 (the number of seconds in a day), and you have the number of days since the epoch and a remainder. Divide the remainder by 3,600 (the number of seconds in an hour) and you have the current hour. Divide the remainder of that by 60 and you have the current minute, and the remainder gives the current second. Divide the number of days by 365 and you have the current year (but don't forget leap years), and the remainder gives the current month and day, which can be separated just as easily.
Unfortunately, everyone doesn't use Coordinated Universal Time. Coordinated Universal Time is the time of day at the Prime Meridian, which passes through Greenwich, England (hence the name Greenwich Mean Time). Local time in other parts of the world is determined by taking an offset, either positive or negative, from Greenwich Mean Time. If the location is east of Greenwich, the offset is negative (meaning local time is earlier than UTC); if the location is west of Greenwich, the offset is positive (meaning local time is later than UTC). For example, local time in New York City is five hours earlier than UTC. So when it's 8:00 a.m. in New York, it's already 1:00 p.m. in Greenwich.
Each of these offsets is called a time zone. The purpose of time zones is to allow human beings to shift the clock such that it agrees with local day and night. For example, local noon should be the time at which the sun is at its highest point in the sky. But when it's local noon at Greenwich, England, it's still dark in Los Angeles, California. So Los Angeles shifts its local time by eight hours from UTC. In most parts of the world, the local time zone is offset by a whole number of hours from UTC. However, in some parts of the world, the local time zone is offset by some number of half hours from UTC; for example, in Adelaide, Australia, local time is 10.5 hours ahead of UTC.
To complicate things even further, humans have invented another artificial time adjustment called Daylight Savings Time (DST). This adjustment shifts clocks forward by (usually) one hour in the spring, and shifts them back again in the fall. The purpose of this shift is to seemingly make daylight last longer each day during the summer, so that farmers and other people who have to work outdoors can get more done. (Of course, the number of daylight hours doesn't actually change, DST just makes it seem like the days are longer by moving bedtime ahead one hour.)
In order to write a function that converts UNIX time format to a date and time string representing local time, you have to keep track of a number of different things. First, you have to know what time zone you are in and how that time zone is offset from UTC. This means that the conversion is different depending on whether you're in New York City, Los Angeles, or Moscow. Furthermore, you have to know the rules for DST in this time zone; this is even more complicated. DST is determined differently in different parts of the world; some areas observe it, and some don't. Consider the United States' rules for DST observance. Prior to 1967, observance of DST was by local option except during World War I and II, when it was mandatory. Since 1967, DST has been observed by nearly the entire country. But even this has exceptions; the state of Indiana, with the exception of the northwest and southeast corners, does not observe DST. To further complicate matters, prior to 1987, DST began on the last Sunday in April; since 1987 it has begun on the first Sunday in April. DST ends on the last Sunday in October. This seems fairly straightforward. But in 1974 and 1975, because of the energy crisis, DST began on January 6 and February 23, respectively. And in 1989, the U.S. House of Representatives passed a bill that would make DST in the Pacific time zone end on the first Sunday after November 7th in presidential election years and on the last Sunday in October otherwise (this bill was never signed into law).
Fortunately, this whole mess is taken care of for you by the UNIX library routines that manipulate time and date strings. However, I wanted to provide you with some idea of the complexity involved in making these conversions. Many older versions of UNIX had numerous problems with time zones. Some would only handle time zones that were whole hour offsets from UTC, some could not reliably convert between an offset and a time zone name, and so forth. More will be said about this below in the section on porting notes, but it's important to be aware that the routines described in the following sections, while they handle all time zone conversions known at the time they were released, may not handle conversions properly in the future. This is particularly true of the DST corrections, which are subject to the whims of our lawmakers.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback