How to convert from FILETIME to Unix epoch time in milliseconds

Shane 46 Reputation points
2023-01-11T16:29:11.5233333+00:00

I would like to know how to convert from a FILETIME to time in milliseconds since Unix epoch in UTC (I want to store this timestamp in an int64_t).

I considered using the FileTimeToSystemTime function and then computing the milliseconds since the Unix epoch, but maybe there is a better way to compute the timestamp I want - maybe there is a Windows API function that performs the conversion for me.

How can I perform this conversion using the Windows API?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,710 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,832 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Michael Taylor 56,271 Reputation points
    2023-01-11T16:43:38.0066667+00:00

    Raymond answered this very question in his blog last year. The answer is to use chrono He mentions the helper methods in WinRT but I don't think that is an issue if you're using pure C++.

    Alternatively somebody posted a pure C++ answer using chrono on StackOverflow. It is really just a matter of some basic math.

    1 person found this answer helpful.

  2. Castorix31 86,496 Reputation points
    2023-01-11T17:59:32.9933333+00:00

    Old macro from MS :

    #define FILETIME_TO_UNIXTIME(ft) (UINT)((*(LONGLONG*)&(ft)-116444736000000000)/10000000)

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.