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.

Obtaining the Local Time Zone
Time zone determination has varied with almost every version of UNIX, owing mostly to the continual need to handle more and more special cases. In SVR4, the local time zone is stored in the TZ environment variable, which contains a string such as US/Eastern or Australia/West. In C programs, the program should first call the function tzset:
    #include <time.h>
    void tzset(void);
After calling tzset, four external variables are available for use:
    extern time_t timezone, altzone;
    extern char *tzname[2];
    extern int daylight;
The timezone variable contains the difference, in seconds, between UTC and local standard time; the altzone variable contains the difference, in seconds, between UTC and the alternate time zone (DST). The daylight variable is non-zero if DST is in effect, zero otherwise. The tzname array contains the names (abbreviations) of the time zones for local standard time and DST; for example, in New York City tzname[0] contains EST and tzname[1] contains EDT. Prior to calling tzset, these four variables contain values that describe Coordinated Universal Time.
HP-UX 10.x does not provide the altzone variable.
Porting Notes
In SVR2, the TZ environment variable had to contain a three-letter time zone name, followed by a number indicating the difference between local time and UTC in hours, followed by an optional three-letter name for a daylight time zone. When DST was in effect, the standard United States rules were applied. This means that SVR2 could not handle time zones that were half-hour offsets from UTC, or daylight time rules that differed from the United States'. Otherwise, the interface is the same as described previously.
SunOS 4.x provides the same interface described previously, except that it also allows the time zone name to be obtained from the struct tm structure (described next). SunOS 4.x is the only operating system that allows the time zone name to be obtained in this manner.
BSD UNIX and Version 7 offered two other functions for working with time zones:
    #include <sys/types.h>
    #include <sys/timeb.h>
    int ftime(struct timeb *tp);
    char *timezone(int zone, int dst);
The ftime function placed the current time and time zone information into the structure of type struct timeb pointed to by tp and defined in sys/timeb.h:
    struct timeb {
        time_t         time;
        unsigned short millitim;
        short          timezone;
        short          dstflag;
    };
The time element contains the time in UNIX time format, the millitim element contains up to 1,000 milliseconds of more precise information, the timezone element contains the local time zone measured in minutes west of Greenwich, and the dstflag element is non-zero when DST is in effect.
The timezone function returns the name associated with the time zone that is zone minutes west of Greenwich; if dst is zero, the standard time zone name is used, otherwise the daylight time zone name is used. This function has serious problems with returning the correct time zone name anywhere in the world, because there are multiple names for each zone depending on location.

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