Convert Windows Filetime to second in Unix/Linux Convert Windows Filetime to second in Unix/Linux c c

Convert Windows Filetime to second in Unix/Linux


it's quite simple: the windows epoch starts 1601-01-01T00:00:00Z. It's 11644473600 seconds before the UNIX/Linux epoch (1970-01-01T00:00:00Z). The Windows ticks are in 100 nanoseconds. Thus, a function to get seconds from the UNIX epoch will be as follows:

#define WINDOWS_TICK 10000000#define SEC_TO_UNIX_EPOCH 11644473600LLunsigned WindowsTickToUnixSeconds(long long windowsTicks){     return (unsigned)(windowsTicks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);}


FILETIME type is is the number 100 ns increments since January 1 1601.

To convert this into a unix time_t you can use the following.

#define TICKS_PER_SECOND 10000000#define EPOCH_DIFFERENCE 11644473600LLtime_t convertWindowsTimeToUnixTime(long long int input){    long long int temp;    temp = input / TICKS_PER_SECOND; //convert from 100ns intervals to seconds;    temp = temp - EPOCH_DIFFERENCE;  //subtract number of seconds between epochs    return (time_t) temp;}

you may then use the ctime functions to manipulate it.


(I discovered I can't enter readable code in a comment, so...)

Note that Windows can represent times outside the range of POSIX epoch times, and thus a conversion routine should return an "out-of-range" indication as appropriate. The simplest method is:

   ... (as above)   long long secs;   time_t t;   secs = (windowsTicks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);   t = (time_t) secs;   if (secs != (long long) t)    // checks for truncation/overflow/underflow      return (time_t) -1;   // value not representable as a POSIX time   return t;