Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 648
hari Avatar asked Dec 05 '22 01:12

hari


2 Answers

/* 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.

like image 171
Alex Marcotte Avatar answered Dec 27 '22 18:12

Alex Marcotte


Standard C provides only one way to get the time - time() - which can be converted to a time/date/year with localtime() or gmtime(). So trivially, that must be the most efficient way.

Any other methods are operating-system specific, and you haven't told us what operating system you're using.

like image 40
caf Avatar answered Dec 27 '22 18:12

caf