title: How to correctly convert timespec to timeval?
body: <p>I have a <code>struct timespec</code> object I need to convert to <code>struct timeval</code> for use with <code>lutimes(...)</code>.</p>
  
  <p>I've attempted the following, but <code>lutimes()</code> complains.</p>
  
  <pre><code>const struct timespec ts; // originally provided as function parameter from FUSE
  struct timeval tv;
  tv.tv_sec = ts.tv_sec;
  tv.tv_usec = ts.tv_nsec / 1000;
  lutimes(path, tv); // returns -1; errno=EINVAL
  </code></pre>
  
  <p>Now <code>EINVAL</code> from <code>lutimes</code> means the usec component was outside of <code>0 &lt;= tv_usec &lt; 1000000</code>, meaning the conversion from <code>timespec</code> went wrong. <a href="https://docs.oracle.com/cd/E19109-01/tsolaris8/835-8003/6ruu1b0re/index.html" rel="nofollow noreferrer">[source]</a></p>
  
  <p>How do I properly convert from <code>timespec</code> to <code>timeval</code>?</p>
  
  <hr>
  
  <p>More thorough debugging with the <code>touch</code> command, reveals that <code>timespec</code> contains <code>tv_sec = 0</code> and <code>tv_nsec &gt; 1000000000</code>, when no specific date was specified and the current time should be used.</p>
  
  <p>Why is this? What's the proper way to handle this?</p>