8

How do I convert timeval to time_t? I'm trying to convert: umtp->ut_tv to a time_t so I can use a difftime(a,b).

struct {
   int32_t tv_sec;         /* Seconds */
   int32_t tv_usec;        /* Microseconds */
           } ut_tv;                    /* Time entry was made */
 struct timeval ut_tv;      /* Time entry was made */
4
  • Are you sure the microseconds don't matter to you? If they do, then you need to write your own struct timeval difftimeval(const struct timeval *t1, const struct timeval *t2); function that returns t2 - t1. Or you can define the return type to be a double if you prefer, so it is more nearly plug-compatible with difftime(). Feb 12, 2012 at 21:42
  • Incidentally, the code shown won't compile; you define a variable ut_tv twice with two different types (as an anonymous struct and as a struct timeval). Feb 12, 2012 at 21:43
  • Where did you get this definition of timeval? The type of tv_sec is supposed to be time_t and there is no conversion to be done, only truncation. Feb 13, 2012 at 0:54
  • @R this is coming from utmp.h which is part of Linux. @Jonathan, you're right about declared twice, in utmp.h, I actually took out an else statement so it's really one or the other. I was able to just use (time_t) utmp->ut_tv.tv.sec
    – user994165
    Feb 13, 2012 at 1:44

2 Answers 2

12

time_t just stores seconds, so

 time_t time = (time_t)ut_tv.tv_sec;

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

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

This lets you keep all the precision you had before.

5
  • I was using "->" instead of "." before ".tv_sec"
    – user994165
    Feb 12, 2012 at 21:41
  • 6
    The subtraction is not properly normalized; if a.tv_usec = 123456 and b.tv_usec = 654321, then you end up with a negative fractional time, which should be normalized to a positive value and the tv_sec component adjusted appropriately. Of course, if a comes before b, maybe both parts of the result should be negative? This is 'segmented arithmetic', and it is typically easiest to keep the sign in a separate field. Where there isn't a separate sign field, then both value segments should have the same sign (or one or the other can be zero). Feb 12, 2012 at 21:48
  • I would say all but the most-significant field should always be non-negative. Feb 13, 2012 at 0:55
  • @R..: that works fine until you print out a negative value. Printing -1.900000 when you mean -0.100000 is the sort of thing that gives users the heebie-jeebies. Or, at least, at the point of printing out, you have to normalize the numbers. Of course, you have to do that, regardless; amongst other things, you must print the fractional part with %06d or %.6d, and make sure it is positive; again, if you omit the width specification, you can mislead people by printing 1 µs as 0.1, which is also not good!. Handling signed segmented numbers w/o a sign bit requires non-negligible effort. Feb 13, 2012 at 2:31
  • @JonathanLeffler you both wrong. the time value calculates as (double)(t2.tv_sec - t1.tv_sec) * 1000 + (double)(t2.tv_usec - t1.tv_usec) / 1000, so it never goes negative nor tv_usec diff whatever value it takes will never be above the decimal point, and tv_sec diff never can be below decimal point. overall, time is just two digits - sec, msec: "0.0" that is why ts_usec being introduced - and never being used alone. to calculate time diff, you have to convert to either secs or msecs and then subtract, then you always get the correct result. Mar 4 at 3:03
2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.