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.

Converting Between UNIX Time and Human Time
There are four functions provided to convert between UNIX time and human time:
    #include <time.h>
    struct tm *gmtime(const time_t *clock);
    struct tm *localtime(const time_t *clock);
    time_t mktime(struct tm *tp);
    double difftime(time_t t1, time_t t0);
The gmtime function returns a structure of type struct tm. This structure contains the broken out components of the date and time represented by the value of the variable pointed to by clock, which should contain a value such as that returned by the time function. The time represented in the struct tm structure is in Coordinated Universal Time. The localtime function makes the same conversion, but if the program has called the tzset function first, the resulting time is corrected for the local time zone and daylight time. The struct tm structure is defined in the include file time.h:
    struct tm {
        int    tm_sec;
        int    tm_min;
        int    tm_hour;
        int    tm_mday;
        int    tm_mon;
        int    tm_year;
        int    tm_wday;
        int    tm_yday;
        int    tm_isdst;
    };
The tm_sec element contains the seconds after the minute (0–61; the 61 is for leap seconds), the tm_min element contains the minutes after the hour (0–59), the tm_hour element contains the hours since midnight (0–23), the tm_mday element contains the day of the month (1–31), the tm_mon element contains the month (0–11, 0=January), the tm_year element contains the year since 1900, the tm_wday element contains the day of the week (0–6, 0=Sunday), the tm_yday element contains the day of the year (0–365, 0=January 1st), and the tm_isdst element is non-zero if daylight time is in effect.
The mktime function performs the opposite conversion. It takes a struct tm structure as input and returns the number of seconds since January 1, 1970 00:00:00 UTC. The mktime function also normalizes the time in the structure, so that the values do not have to be within the limits described previously. For example, a tm_hour value of -1 indicates one hour before midnight. The conversions performed by mktime are corrected for the local time zone and daylight time; in general, you'll want to set the tm_isdst field to -1 to avoid surprises.
The difftime function computes the difference between two time values, t1 and t0, and returns the result as a double precision value. This function is required by the ANSI C standard, because there are no arithmetic operations defined on the time_t data type (not all systems use a long for time_t).
One useful thing that gmtime (which should really be called utctime, but history prevails) can be used for is printing out the difference between two times in human-readable format. For example, if you have two times, a login time and a logout time, you can compute the duration of the login session as follows:
    #include <time.h>
    ·
    ·
    ·
    struct tm *tp;
    time_t login, logout, session;
    ·
    ·
    ·
    session = (time_t) difftime(logout, login);
    
    tp = gmtime(&session);
    
    printf("Session length: %d days, %d hours, %d minutes\n",
        tp->tm_yday, tp->tm_hour, tp->tm_min);
Porting Notes
The difftime function is specific to ANSI C environments, although it's easy to define for other environments.
The mktime function is a generalization of two other functions, timelocal and timegm, which have been introduced in a number of UNIX versions.
There is disagreement between various versions of UNIX as to whether the include file for these functions belongs in time.h or sys/time.h. Some versions have it in one place, others have it in the other. Newer versions have sidestepped the issue by making it available in both places.

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