views:

109

answers:

7

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.

A: 

The simplest is

#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));
Akusete
+1  A: 

It really depends on what you mean by "many" :-)

I think you'll probably find that using the ISO standard time() and localtime() functions will be more than fast enough.

The time() function gives you the number of seconds since the epoch, while localtime() both converts it to local time (from UTC) and splits it into a more usable form, the struct tm structure.

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

Any attempt to cache the date/time and use other ways of finding out a delta to apply to it, such as with clock(), will almost invariably:

  • be slower; and
  • suffer from the fact you won't pick up external time changes.
paxdiablo
Of course if many means 100s of times/second - then you could just cache the response and check if time() has changed every 100calls
Martin Beckett
@Martin, that's fine right up until the point where his process is switched out for a second because of massive system loads :-)
paxdiablo
@Martin: the only problem with that is that unless you're *sure* it's going to be called many times a second, this could easily end up returning stale data.
Jerry Coffin
time() only has a resolution of a second so it's unlikey that a process running 100Hz would care if 10% of the values were tagged with the previous second
Martin Beckett
A: 

Just about the only way (that's standard, anyway) is to call time followed by localtime or gmtime.

Jerry Coffin
I don't see what's wrong with the most straightforward response. The fact he's already working in C is of great benefit, the only hit here is the syscall for time?
Matt Joiner
A: 

well, in general, directly accessing the OS's API to get the time is probably the most efficient, but not so portable.....

the C time functions are ok.

But really depends on your platform

Keith Nicholas
+1  A: 
/* 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.

Alex Marcotte
+2  A: 

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 by definition, 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.

caf
Thanks much. I am on freeBSD.
hari
If you're worried about `gmtime` or `localtime` being slow, you could simply call them once and then adjust the results based on a delta value given in seconds obtained from `time`. This is portable as far as POSIX is concerned, but not plain C, as plain C does not specify the format of the `time_t` type except that it's an arithmetic type.
R..
A: 

You can use gettimeofday() function to get time in seconds & microseconds which is (I think) very fast (as there is a similar function in Linux kernel do_gettimeofday()) and then you can convert it to your required format (might possible to use functions mentioned above for conversion.

I hope this helps.

Fawad Lateef