C Time Functions - C time






Returns time in seconds.

Get the current calendar time as a value of type time_t.

Prototype

time_t time (time_t* timer);

Parameter

This function has the following parameter.

timer
Pointer to time_t value.

Return

It returns the current calendar time as a time_t object.

If the argument is not a null pointer, the return value is the same as the one stored in the location pointed by argument timer.

If the function could not retrieve the calendar time, it returns a value of -1.

time_t is an alias of a fundamental arithmetic type capable of representing times.





Example


#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, difftime, time, mktime */
//from w  ww .ja va 2s . c  om
int main (){
  time_t timer;
  struct tm y2k = {0};
  double seconds;

  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;

  time(&timer);  /* get current time; same as: timer = time(NULL) */

  seconds = difftime(timer,mktime(&y2k));

  printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);

  return 0;
} 
       

The code above generates the following result.





Example 2


#include <stdio.h>
#include <time.h>
#include <stdint.h>
 /*  www.  j  av  a 2s . c  o m*/
int main(void)
{
    time_t result = time(NULL);
    if(result != -1)
        printf("The current time is %s(%ju seconds since the Epoch)\n",
               asctime(gmtime(&result)), (uintmax_t)result);
}

The code above generates the following result.