C timeval vs timespec

Besides the difference in accuracy, what are the differences between struct timeval and struct timespec ? If I need less precision than Ξs (say milliseconds), why should I use one over the other?

In my compiler (gcc for ARM):

 /* POSIX.1b structure for a time value. This is like a `struct timeval' but has nanoseconds instead of microseconds. */ struct timespec { __time_t tv_sec; /* Seconds. */ __syscall_slong_t tv_nsec; /* Nanoseconds. */ }; /* A time value that is accurate to the nearest microsecond but also has a range of years. */ struct timeval { __time_t tv_sec; /* Seconds. */ __suseconds_t tv_usec; /* Microseconds. */ }; 

When using __syscall_slong_t and __suseconds_t defined as "long word".

+7
c linux time
source share
3 answers

I think this is really an API compatibility issue. POSIX-y calls pselect() and clock_gettime() use struct timespec . Various file system calls, such as utimes() , and some various Linux calls, such as gettimeofday() and select() , use struct timeval . In general, from several man pages I suspect that struct timeval has a BSD legacy, whereas struct timespec is POSIX.

If you take interval measurements, there is no reason not to use the extra accuracy from clock_gettime() - although be careful that this is usually hardware and not a header file that limits your measurement accuracy. Dividing by a million for display purposes is hardly better or worse than dividing by a thousand. In addition, Mac OS X does not support clock_gettime() .

But if you do a lot of file manipulation, it may make sense to use the struct timeval used in the API, for example utimes() . struct timeval also has some comparison functions on Linux, BSD, and Mac OS X, for example. timercmp() , timersub() (see the manual pages again).

I would make a decision based on the APIs that you intend to use, and not on the structures themselves. (Or, if necessary, write a wrapper class with conversion methods.)

+13
source share

Both are defined by AFAIK for POSIX.1-2001, so in terms of portability it doesn't matter which one you use. The simplest answer: use what you need for the API that you intend to call.

Here you MAY be platform dependent with size using struct timeval :

The suseconds_t type must be a signed integer type capable of storing values, at least in the range [-1, 1,000,000].

in struct timespec second member is of type long . int would be enough on a 32-bit platform to fulfill suseconds_t requirements. But, on a 64-bit system, time_t usually 64 bits, thereby forcing the structure to fill up to 16 bytes. Thus, the size advantage is incredible.

+2
source share

Personally, I do not use any. I prefer to express time as a simple int64_t , because it makes time calculations dead easy, and if you need to, converting back to struct timeval or struct timespec also not a problem. Even if you need nanosecond precision, int64_t can express a span of almost 585 years. If you just need milliseconds, you have coverage of almost 585 million years.

+1
source share