Search code examples
windowsunixtimerepoch

How to convert QueryPerformanceCounter to Unix nanoseconds (epoch)


I am using QueryPerformanceCounter windows syscall in order to get high-precision timestamp.

I need to convert it to unix epoch (in nanoseconds) as I am going to pass the value to an API that needs it in this format

Could anybody help me understanding how to accomplish this?


Solution

  • QueryPerformanceCounter does not return a timestamp with a fixed offset to the current time (as in UTC, or, the time shown by a wall clock), so you cannot convert it to UNIX time.

    However, time differences measured using QueryPerformanceCounter can be converted to nanoseconds (or any time unit) by dividing by the result of QueryPerformanceFrequency (to get seconds) and multiplying by, e.g., 10^9 to get nanoseconds.