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(&ltime) == -1)
   {
      printf("Calendar time not available.\n");
      exit(1);
   }
   printf("The time is %s\n", ctime(&ltime));
}
 
/******************  Output should be similar to:  ****************
 
The time is Mon Mar 22 19:01:41 2004
*/

Related Information