Wednesday, March 5, 2008

FILETIME to time_t

Microsoft kindly provides the code to convert from time_t (Unix time) to Win32's FILETIME.
Here's the code to do the reverse:

time_t FileTimeToUnixTime(const FILETIME& ft)
{
// the reverse of http://support.microsoft.com/kb/167296/en-us
ULONGLONG ull = reinterpret_cast<const ULONGLONG&>(ft);
ull -= 116444736000000000;
ull /= 10000000;
assert(ull < ULONG_MAX);
return static_cast<time_t>(ull);
}