Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 6.10 Time and Date Routines 191


kernel

time_t
(calendar time) timespec

struct tm
(broken-down time)

string formatted string

timeval

gettimeofday time clock_gettime

tv_sec tv_sec

gmtime
localtime

mktime

strptime

strftimestrftime_l

Figure 6.9 Relationship of the various time functions

The two functionslocaltimeandgmtimeconvert a calendar time into what’s
called a broken-down time, atmstructure.

struct tm { /* a broken-down time */
int tm_sec; /* seconds after the minute: [0 - 60] */
int tm_min; /* minutes after the hour: [0 - 59] */
int tm_hour; /* hours after midnight: [0 - 23] */
int tm_mday; /* day of the month: [1 - 31] */
int tm_mon; /* months since January: [0 - 11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday: [0 - 6] */
int tm_yday; /* days since January 1: [0 - 365] */
int tm_isdst; /* daylight saving time flag: <0, 0, >0 */
};
The reason that the seconds can be greater than 59 is to allow for a leap second. Note
that all the fields except the day of the month are0-based. The daylight saving time flag
is positive if daylight saving time is in effect, 0 if it’s not in effect, and negative if the
information isn’t available.

In older versions of the Single UNIX Specification, double leap seconds wereallowed. Thus
the valid range of values for thetm_secmember was 0–61. The formal definition of UTC
doesn’t allow for double leap seconds, so the valid range for seconds is now 0–60.
Free download pdf