time() — Determine Current Time
Format
#include <time.h>
time_t time(time_t *timeptr);
Language Level
ANSI
Threadsafe
Yes
Description
The
time()
function
determines the current calendar time, in seconds. Note: Calendar
time is the number of seconds that have elapsed since EPOCH, which
is 00:00:00, January 1, 1970 Universal Coordinate Time (UTC).
Return Value
The time()
function
returns the current calendar time. The return value is also stored
in the location that is given by timeptr.
If timeptr is NULL, the return
value is not stored. If the calendar time is not available, the value (time_t)(-1) is
returned.
Example
This example gets the time and assigns
it to ltime. The
ctime()
function
then converts the number of seconds to the current date and time.
This example then prints a message giving the current time. #include <time.h>
#include <stdio.h>
int main(void)
{
time_t ltime;
if (time(<ime) == -1)
{
printf("Calendar time not available.\n");
exit(1);
}
printf("The time is %s\n", ctime(<ime));
}
/****************** Output should be similar to: ****************
The time is Mon Mar 22 19:01:41 2004
*/
Related Information
- asctime() — Convert Time to Character String
- asctime_r() — Convert Time to Character String (Restartable)
- ctime() — Convert Time to Character String
- ctime64() — Convert Time to Character String
- ctime64_r() — Convert Time to Character String (Restartable)
- ctime_r() — Convert Time to Character String (Restartable)
- gmtime() — Convert Time
- gmtime64() — Convert Time
- gmtime64_r() — Convert Time (Restartable)
- gmtime_r() — Convert Time (Restartable)
- localtime() — Convert Time
- localtime64() — Convert Time
- localtime64_r() — Convert Time (Restartable)
- localtime_r() — Convert Time (Restartable)
- mktime() — Convert Local Time
- mktime64() — Convert Local Time
- time64() — Determine Current Time
- <time.h>