most efficient way to get current time/date/day in C

hari picture hari · Aug 4, 2010 · Viewed 9.4k times · Source

What is the most efficient way of getting current time/date/day/year in C language? As I have to execute this many times, I need a real efficient way. I am on freeBSD.

thanks in advance.

Answer

Alex Marcotte picture Alex Marcotte · Aug 4, 2010
/* ctime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;

  time ( &rawtime );
  printf ( "The current local time is: %s", ctime (&rawtime) );

  return 0;
}

You can use ctime, if you need it as a string.