title: How do I create monotonic clock on Windows which doesn't tick during suspend?
body: <p>I'm looking for a way to obtain a guaranteed-monotonic clock which excludes time spent during suspend, just like POSIX <code>CLOCK_MONOTONIC</code>.</p>
  
  <p>Solutions requiring Windows 7 (or later) are acceptable.</p>
  
  <p>Here's an example of something that doesn't work:</p>
  
  <pre><code>LONGLONG suspendTime, uiTime1, uiTime2;
  do {
    QueryUnbiasedInterruptTime((ULONGLONG*)&amp;uiTime1);
    suspendTime = GetTickCount64()*10000 - uiTime1;
    QueryUnbiasedInterruptTime((ULONGLONG*)&amp;uiTime2);
  } while (uiTime1 != uiTime2);
  static LARGE_INTEGER firstSuspend = suspendTime;
  static LARGE_INTERER lastSuspend = suspendTime;
  assert(suspendTime &gt; lastSuspend);
  lastSuspend = suspendTime;
  
  LARGE_INTEGER now;
  QueryPerformanceCounter(&amp;now);
  static LONGLONG firstQpc = now.QuadPart;
  
  return (now.QuadPart - firstQpc)*qpcFreqNumer/qpcFreqDenom -
      (suspendTime - firstSuspend);
  </code></pre>
  
  <p>The problem with this (my first attempt) is that GetTickCount only ticks every 15ms, wheras QueryUnbiasedInterruptTime seems to tick a little more often, so every now and then my method observes the suspend time go back by a little.</p>
  
  <p>I've also tried using CallNtPowerInformation, but it's not clear how to use those values either to get a nice, race-free measure of suspend time.</p>