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 Current Time
To obtain the current time of day in UNIX time format, all versions of UNIX provide the same function:
    #include <sys/types.h>
    #include <time.h>
    time_t time(time_t *tloc);
The time function returns the number of seconds since January 1, 1970, 00:00:00 UTC. If tloc is non-null, time also stores this value in the memory location pointed to by tloc.
Porting Notes
In 4.2 BSD, another function was introduced to obtain the current time:
    #include <sys/time.h>
    int gettimeofday(struct timeval *tp, struct timezone *tz);
The gettimeofday function places the current time into the structure pointed to by tp, and the local time zone information into the structure pointed to by tz. The structures are defined in the include file sys/time.h:
    struct timeval {
        long    tv_sec;
        long    tv_usec;
    };
    struct timezone {
        int     tz_minuteswest;
        int     tz_dsttime;
    };
The tv_sec and tv_usec elements store the time in seconds and microseconds since January 1, 1970. The tz_minuteswest element stores the offset (positive or negative) from UTC in minutes, and the tz_dsttime element contains a flag indicating the type of DST correction (if any) to be applied.
IRIX 5.x and versions of Solaris prior to Solaris 2.5 provide a single-argument version of gettimeofday for backward compatibility; the struct timezone argument is ignored. HP-UX 10.x and versions of Solaris beginning with Solaris 2.5 provide a two-argument version.

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