title: c++ is clock_gettime() overflowing?
body: <p>When using the following example code, the tv_nsec value, I presume is looping round because it's <em>only</em> an <code>long</code>;</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  #include &lt;time.h&gt;
  
  int main(int argc, char *argv[]) {
    timespec time1, time2;
    timespec timens = {0};
    timens.tv_sec = 1;
  
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &amp;time1);
    nanosleep(&amp;timens, (struct timespec *)NULL);
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &amp;time2);
  
    cout &lt;&lt; time1.tv_nsec &lt;&lt; " : " &lt;&lt; time2.tv_nsec &lt;&lt; endl;
    cout &lt;&lt; (time2.tv_nsec-time1.tv_nsec) &lt;&lt; endl;
    return 0;
  }
  
  $ ./microtime 
  2754095 : 2766801
  12706
  </code></pre>
  
  <p>The difference between the start and end time is 12706ns (12.7us), it didn't take 12us to sleep for 1 second, I'm <em>reasonably</em> sure of that! So what has happened here, has tv_nsec looped round? Assuming it's an <code>singed long</code> to give a smaller range of positive values (0 to 2147483647) instead of an <code>unsigned long</code> (0 to 4294967295), 2147483647 nanoseconds is still just over 2 full seconds!</p>
  
  <p>If I change the code to the following I get a seemingly invalid output still;</p>
  
  <pre><code>timens.tv_sec = 0;
  timens.tv_nsec = 100000;
  
  $ ./microtime
  2743552 : 2754327
  10775
  </code></pre>
  
  <p>The sleep duration was set for 100,000ns but according to that output the program has slept for 10,000ns. I know that is a small amount of time but I'm trying to show here that whether it is a long time like 1 second or a short time like 100us, there seems to be no accuracy here, or anywhere in between these values. Am I doing something wrong, have I implemented this incorrectly somehow? Why can't I get the actual duration of the event in between the calls to clock_gettime()?</p>
  
  <p>Many thanks for your time.</p>
  
  <p><strong>Update</strong></p>
  
  <p>@Oli;
  I can test your suggestion with the following code but it doesn't seem to work:</p>
  
  <pre><code>int main(int argc, char *argv[]) {
    timespec time1, time2;
    timespec timens = {0};
    timens.tv_sec = 1;
    timens.tv_nsec = 0;
  
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &amp;time1);
    nanosleep(&amp;timens, (struct timespec *)NULL);
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &amp;time2);
  
    cout &lt;&lt;time1.tv_sec&lt;&lt;"s:"&lt;&lt;time1.tv_nsec&lt;&lt;"ns : "&lt;&lt;
           time2.tv_sec&lt;&lt;"s:"&lt;&lt;time2.tv_nsec&lt;&lt;"ns"&lt;&lt;endl;
    cout &lt;&lt;(time2.tv_nsec-time1.tv_nsec)&lt;&lt;endl;
    return 0;
  }
  
  $ ./microtime
  0s:2801478ns : 0s:2813732ns
  12254
  </code></pre>