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

What is the most efficient way to get the current time / date / day / year in C? Since I have to do this many times, I need a real effective way. I am on FreeBSD.

early.

+3
source share
8 answers

Standard C provides only one way to get the time - time()- which can be converted to time / date / year with localtime()or gmtime(). So trivial, this should be the most effective way.

Any other methods are operating system dependent, and you did not tell us which operating system you are using.

+5
source
/* 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;
}

ctime, .

+6

, "": -)

, , , , time() localtime() ISO . , "Intel(R) Core(TM)2 Duo CPU E6850 @ 3.00GHz", , time() 1.045 , <<24 > - 0,98 . , , , , , .

time() , localtime() ( UTC) , struct tm.

#include <time.h>
time_t t = time (NULL);
struct tm* lt = localtime (&t);
// Use lt->tm_year, lt->tm_mday, and so forth.

/ , , clock(), :

  • ;
  • , .
+2

gettimeofday(), , ( ) ( Linux , do_gettimeofday()), ( , .

, .

+1

#include <time.h>
//...
time_t current_time = time (NULL);
struct tm* local_time = localtime (&current_time); 
printf ("the time is %s\n", asctime (local_time));
0

( , ) time, localtime gmtime.

0

, , OS API , , , .....

C- .

0

, , FreeBSD ( POSIX), ,

  • setitimer (ITIMER_REAL, ...)
  • SIGALRM, ,
  • ,

Even if signals are lost due to system overload, this will be fixed the next time the process is scheduled.

0
source

Source: https://habr.com/ru/post/1757944/


All Articles