[ACCEPTED]-Convert timeval to time_t-c

Accepted answer
Score: 12

time_t just stores seconds, so

 time_t time = (time_t)ut_tv.tv_sec;

Should work, but 3 since you're just looking for a difference, there's 2 always the magic of subtraction.

struct timeval diff = {a.tv_sec-b.tv_sec, a.tv_usec-b.tv_usec};

This lets you keep 1 all the precision you had before.

Score: 2

Without using c-cast, you can simply do 1 this :

struct timeval rawtime_s_us;
gettimeofday( &rawtime_s_us, NULL );
time_t rawtime_s = rawtime_s_us.tv_sec;

More Related questions