Node:Broken-down Time,
Next:High Accuracy Clock,
Previous:High-Resolution Calendar,
Up:Calendar Time
Broken-down Time
Calendar time is represented by the usual GNU C library functions as an
elapsed time since a fixed base calendar time.  This is convenient for
computation, but has no relation to the way people normally think of
calendar time.  By contrast, broken-down time is a binary
representation of calendar time separated into year, month, day, and so
on.  Broken-down time values are not useful for calculations, but they
are useful for printing human readable time information.
A broken-down time value is always relative to a choice of time
zone, and it also indicates which time zone that is.
The symbols in this section are declared in the header file time.h.
| This is the data type used to represent a broken-down time.  The structure
contains at least the following members, which can appear in any order. 
int tm_secThis is the number of full seconds since the top of the minute (normally
in the range 0through59, but the actual upper limit is60, to allow for leap seconds if leap second support is
available).
int tm_minThis is the number of full minutes since the top of the hour (in the
range 0through59).
int tm_hourThis is the number of full hours past midnight (in the range 0through23).
int tm_mdayThis is the ordinal day of the month (in the range 1through31). 
Watch out for this one!  As the only ordinal number in the structure, it is
inconsistent with the rest of the structure.
int tm_monThis is the number of full calendar months since the beginning of the
year (in the range 0through11).  Watch out for this one! 
People usually use ordinal numbers for month-of-year (where January = 1).
int tm_yearThis is the number of full calendar years since 1900.
int tm_wdayThis is the number of full days since Sunday (in the range 0through6).
int tm_ydayThis is the number of full days since the beginning of the year (in the
range 0through365).
int tm_isdstThis is a flag that indicates whether Daylight Saving Time is (or was, or
will be) in effect at the time described.  The value is positive if
Daylight Saving Time is in effect, zero if it is not, and negative if the
information is not available.
long int tm_gmtoffThis field describes the time zone that was used to compute this
broken-down time value, including any adjustment for daylight saving; it
is the number of seconds that you must add to UTC to get local time. 
You can also think of this as the number of seconds east of UTC.  For
example, for U.S. Eastern Standard Time, the value is -5*60*60. 
Thetm_gmtofffield is derived from BSD and is a GNU library
extension; it is not visible in a strict ISO C environment.
const char *tm_zoneThis field is the name for the time zone that was used to compute this
broken-down time value.  Like tm_gmtoff, this field is a BSD and
GNU extension, and is not visible in a strict ISO C environment. | 
| struct tm * localtime (const time_t *time) | Function | 
| The localtimefunction converts the simple time pointed to by
time to broken-down time representation, expressed relative to the
user's specified time zone.The return value is a pointer to a static broken-down time structure, which
might be overwritten by subsequent calls to ctime,gmtime,
orlocaltime.  (But no other library function overwrites the contents
of this object.) The return value is the null pointer if time cannot be represented
as a broken-down time; typically this is because the year cannot fit into
an int. Calling localtimehas one other effect: it sets the variabletznamewith information about the current time zone.  See Time Zone Functions. | 
Using the localtime function is a big problem in multi-threaded
programs.  The result is returned in a static buffer and this is used in
all threads.  POSIX.1c introduced a variant of this function.
| struct tm * localtime_r (const time_t *time, struct tm *resultp) | Function | 
| The localtime_rfunction works just like thelocaltimefunction.  It takes a pointer to a variable containing a simple time
and converts it to the broken-down time format.But the result is not placed in a static buffer.  Instead it is placed
in the object of type struct tmto which the parameter
resultp points. If the conversion is successful the function returns a pointer to the
object the result was written into, i.e., it returns resultp. 
 | 
| struct tm * gmtime (const time_t *time) | Function | 
| This function is similar to localtime, except that the broken-down
time is expressed as Coordinated Universal Time (UTC) (formerly called
Greenwich Mean Time (GMT)) rather than relative to a local time zone. | 
As for the localtime function we have the problem that the result
is placed in a static variable.  POSIX.1c also provides a replacement for
gmtime.
| struct tm * gmtime_r (const time_t *time, struct tm *resultp) | Function | 
| This function is similar to localtime_r, except that it converts
just likegmtimethe given time as Coordinated Universal Time.If the conversion is successful the function returns a pointer to the
object the result was written into, i.e., it returns resultp. 
 | 
| time_t mktime (struct tm *brokentime) | Function | 
| The mktimefunction is used to convert a broken-down time structure
to a simple time representation.  It also "normalizes" the contents of
the broken-down time structure, by filling in the day of week and day of
year based on the other date and time components.The mktimefunction ignores the specified contents of thetm_wdayandtm_ydaymembers of the broken-down time
structure.  It uses the values of the other components to determine the
calendar time; it's permissible for these components to have
unnormalized values outside their normal ranges.  The last thing thatmktimedoes is adjust the components of the brokentime
structure (including thetm_wdayandtm_yday). If the specified broken-down time cannot be represented as a simple time,
mktimereturns a value of(time_t)(-1)and does not modify
the contents of brokentime. Calling mktimealso sets the variabletznamewith
information about the current time zone.  See Time Zone Functions. | 
| time_t timelocal (struct tm *brokentime) | Function | 
| timelocalis functionally identical tomktime, but more
mnemonically named.  Note that it is the inverse of thelocaltimefunction.
 Portability note:  mktimeis essentially universally
available.timelocalis rather rare. | 
| time_t timegm (struct tm *brokentime) | Function | 
| timegmis functionally identical tomktimeexcept it
always takes the input values to be Coordinated Universal Time (UTC)
regardless of any local time zone setting.
 Note that timegmis the inverse ofgmtime. Portability note:  mktimeis essentially universally
available.timegmis rather rare.  For the most portable
conversion from a UTC broken-down time to a simple time, set
theTZenvironment variable to UTC, callmktime, then setTZback. |