The Linux Programming Interface

(nextflipdebug5) #1
Time 187

If the tz argument is supplied, then it returns a timezone structure whose fields
contain whatever values were specified in the (obsolete) tz argument in a previ-
ous call to settimeofday(). This structure contains two fields: tz_minuteswest and
tz_dsttime. The tz_minuteswest field indicates the number of minutes that must
be added to times in this zone to match UTC, with a negative value indicating
that an adjustment of minutes to the east of UTC (e.g., for Central European
Time, one hour ahead of UTC, this field would contain the value –60). The
tz_dsttime field contains a constant that was designed to represent the day-
light saving time (DST) regime in force in this timezone. It is because the
DST regime can’t be represented using a simple algorithm that the tz argu-
ment is obsolete. (This field has never been supported on Linux.) See the
gettimeofday(2) manual page for further details.

The time() system call returns the number of seconds since the Epoch (i.e., the same
value that gettimeofday() returns in the tv_sec field of its tv argument).

If the timep argument is not NULL, the number of seconds since the Epoch is also
placed in the location to which timep points.
Since time() returns the same value in two ways, and the only possible error that
can occur when using time() is to give an invalid address in the timep argument
(EFAULT), we often simply use the following call (without error checking):

t = time(NULL);

The reason for the existence of two system calls (time() and gettimeofday()) with
essentially the same purpose is historical. Early UNIX implementations pro-
vided time(). 4.3BSD added the more precise gettimeofday() system call. The
existence of time() as a system call is now redundant; it could be implemented
as a library function that calls gettimeofday().

10.2 Time-Conversion Functions..........................................................................................


Figure 10-1 shows the functions used to convert between time_t values and other
time formats, including printable representations. These functions shield us from
the complexity brought to such conversions by timezones, daylight saving time
(DST) regimes, and localization issues. (We describe timezones in Section 10.3 and
locales in Section 10.4.)

#include <time.h>

time_t time(time_t *timep);
Returns number of seconds since the Epoch,or (time_t) –1 on error
Free download pdf