Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When invoking clock_gettime() may the returned tv_nsec field actually exceed a second?

Tags:

c

linux

glibc

libc

When you invoke clock_gettime() it returns a timespec structure.

struct timespec {
    time_t tv_sec; /* seconds */
    long tv_nsec; /* nanoseconds */
};

I don't find in the man page a garantee that the tv_nsec won't exceed one second. Does the garantee exist actually ? Could it be dependant on the library (glibc?) implementation for linux?

The key idea is : do I need to 'normalize' any result coming from the clock_gettime() function?

like image 505
yves Baumes Avatar asked Jan 19 '15 16:01

yves Baumes


1 Answers

According to opengroup

The tv_nsec member is only valid if greater than or equal to zero, and less than the number of nanoseconds in a second (1000 million). The time interval described by this structure is (tv_sec * 10'-.4m'9'.4m' + tv_nsec) nanoseconds.

So according to opengroup, it looks official that it must be less than 1 second.

like image 114
user590028 Avatar answered Nov 02 '22 22:11

user590028