time - C time.h

C examples for time.h:time

Type

function

From


<ctime>
<time.h>

Description

Get current time

Prototype

time_t time (time_t* timer);

Parameters

Parameter Description
timer Pointer to an object of type time_t.

Return Value

The current calendar time as a time_t object.

On error, it returns a value of -1.

Demo Code


#include <stdio.h>
#include <time.h>

int main ()/*from   w w w.j a va2 s .  c  o  m*/
{
  time_t timer;
  struct tm year2020 = {0};
  double seconds;

  year2020.tm_hour = 0;   
  year2020.tm_min = 0; 
  year2020.tm_sec = 0;
  year2020.tm_year = 120; 
  year2020.tm_mon = 0; 
  year2020.tm_mday = 1;

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

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

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

  return 0;
}

Related Tutorials