time.h
The time.h
header provides functionality for date and time manipulation and conversion.
Type aliases
clock_t
- Alias for
__clock_t
which is
alias for__CLOCK_T_TYPE
which
expands to__SLONGWORD_TYPE
which
expands tosigned long int
Processor clock tick count. size_t
- See the stddef.h header.
time_t
- Alias for
__time_t
which is
alias for__TIME_T_TYPE
which
expands to__SLONGWORD_TYPE
which
expands tosigned long int
Calendar time since Unix epoch (00:00:00 UTC on 1 January 1970).
Macros
CLOCKS_PER_SEC
- Expands to 1 000 000
clock_t
s per second
Number of processor clock ticks per second. TIME_UTC
- Expands to 1
NULL
- See the stddef.h header.
Types
tm
- Time
Calendar date and time broken down into its components.
This data types is suitable for presentation and not calculation.int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst;
timespec
- Time specification
Time in seconds and nanoseconds.time_t tv_sec; long tv_nsec;
Functions
Manipulation
clock_t clock(void);
- Returns the processor time used by the program since it started.
If the processor time used is not available, −1 is returned. double difftime(time_t time1, time_t time0);
- Computes difference between two calender times and returns it.
time_t mktime(struct tm *timeptr);
- Normalises calendar date and time broken down into its components and returns it as calendar time since Unix epoch (00:00:00 UTC on 1 January 1970).
If calendar time can not be represented, −1 is returned.
Categorically, this function performs both manipulation and conversion. time_t time(time_t *timer);
- Determines the current calendar time and returns it.
If*timer
is not a null pointer, the determined value is assigned to it.
If calendar time is not available, −1 is returned. int timespec_get(struct timespec *ts, int base);
- Determines the current calendar time in the specified time base and assigns it to
*ts
.
Base will always beTIME_UTC
for GCC/glibc.
If successful, base is returned, otherwise 0.
Conversion
char *asctime(const struct tm *timeptr);
- Returns a
char *
(string) of a formattedtm *
. char *ctime(const time_t *timer);
- Returns a
char *
(string) of a formattedtime_t *
in local time.
Equivalent toasctime(localtime(timer))
. struct tm *gmtime(const time_t *timer);
- Returns a
tm *
representation oftime_t *
in UTC. struct tm *localtime(const time_t *timer);
- Returns a
tm *
representation oftime_t *
in local time. size_t strftime(char *restrict s, size_t maxsize, const char *restrict format, const struct tm *restrict timeptr);
- Populates a
char *
(string) with a representation oftm *
in a specified format.
Returns the number of resulting characters (including null character) placed intos *
.
If the memory allocated for the string is too small, then 0 is returned.