Time and Date
The ANSI C library includes a set of functions to determine the current time and date, to convert time and date information, and to generate formatted time and date strings for output. These functions are declared in the header file time.h .
The principal functions for determining the current time are:
-
clock_tclock( void ); Returns the CPU time used by the program so far, with type
clock_t(usually equivalent tolong). The result can be converted to seconds by dividing it by the constantCLOCKS_PER_SEC.-
time_ttime( time_t *pSec); Returns the number of seconds that have elapsed since a certain time (usually January 1, 1970, 00:00:00 o’clock). If the pointer
pSecis not NULL, the result is also copied to the location it addresses. The typetime_tis generally defined aslong.
The functions for converting and formatting date and time information are:
-
doubledifftime( time_tt1, time_tt0); Returns the number of seconds between
t0andt1.-
struct tm *gmtime( const time_t *pSec); Returns a pointer to the current Greenwich Mean Time as a structure of type
struct tm, with members of typeintfor the second, minute, hour, day, etc.-
struct tm *localtime( const time_t *pSec); Like
gmtime(), but returns the local time rather than Greenwich Mean Time.-
char *ctime( const time_t *pSec);char *asctime( const struct tm *ptm);size_tstrftime(char *dest, size_tmaxsize, const char *format, const struct tm *ptm); These functions generate a string representing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access