Name

time

Synopsis

Obtains the current calendar time

#include <time.h>
time_ttime( time_t *timeptr );

The time() function returns the current calendar time as a single arithmetic value. The return type, time_t, is defined in time.h, generally as long or unsigned long. If the argument is not a null pointer, the return value is also assigned to the location it references.

Many operating systems specify that the type time_t represents an integer number of seconds, and that the time() function returns the number of seconds passed since a specified epoch, such as midnight on January 1, 1970, Greenwich Mean Time. However, according to the C standard, neither of these conditions is required. The type time_t is an arithmetic type whose range and precision are defined by the implementation, as is the encoding of the time() function’s return value.

Example

time_t sec;time(&sec);
printf("This line executed at %.24s.\n", ctime(&sec));

This code produces the following output:

This line executed at Tue Mar 15 13:05:16 2005.

See also the examples at asctime(), ctime(), fprintf(), freopen(), gmtime(), rand(), and strftime() in this chapter.

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.