Convert current time from windows to unix timestamp in C or C++

Viewed 28617

First off, I know that this question was asked quite some times (although it seems that 90% are about converting Unix ts -> Windows). Secondly, I would add a comment to another accepted question where my problem would fit in instead of adding another one but I don't have enough reputation.

I saw the accepted solution in Convert Windows Filetime to second in Unix/Linux but am stuck at what I should pass to the function WindowsTickToUnixSeconds. Judging by the parameter name windowsTicks I tried GetTickCount but saw shortly after that this returns the ms since the system started but I need any reasonable count since the start of the Windows time (which seems to was in 1601?).

I saw that windows has a function for retrieving this time: GetSystemTime. I cannot pass the resulting struct to the proposed function in 1 as it is not a long long value.

Can't someone please just give a full working example for C or C++ without omitting such mad-driving details?

3 Answers

With the SYSTEMTIME structure set by GetSystemTime, it's easy to create a a struct tm (see asctime for a reference of the structure) and convert it to a "UNIX time stamp" using the mktime function.

Related