tags:

views:

30

answers:

1

Hi,

Is it possible to pass settimeofday() my time_t/epoch time value, in C? Could someone give me an example of how I could do it ... my C skills are a little rusty :S

Would it be:

time_t time = somevalue;
settimeofday(somevalue, NULL);

I don't have admin access where I'm working and so can't test it out.

Thanks in advance!

+1  A: 

settimeofday() takes a struct timeval * as first argument, so you should do

struct timeval tv;

tv.tv_sec = somevalue;
tv.tv_usec = 0;

settimeofday(&tv,NULL);

followup edit gettimeofday() is the counterpart:

struct timeval tv;

if ( !gettimeofday(&tv,NULL) ) // *always* check return values ;-)
{
    long long microsince1970;
    microsince1970 = tv.tv_sec*1000000 + tv.tv_usec;
    printf("it's been %lld µs ago\n",microsince1970);
}
mvds
Oh, sweet, that worked. Thanks so much! Quick follow up question, is it possible to use gettimeofday() to get a time_t object in milliseconds kinda like how time(Null) returns a time_t object in seconds.
iman453
here you go, time() with µs. divide by 1000 to get ms.
mvds