Convert Unix/Linux time to Windows FILETIME

后端 未结 3 856
悲哀的现实
悲哀的现实 2020-12-18 07:23

I am once again going from Windows to Linux, I have to port a function from Windows to Linux that calculates NTP time. Seems simple but the format is in Windows FILETI

3条回答
  •  甜味超标
    2020-12-18 08:11

    The Microsoft documentation for the FILETIME structure explains what it is. The basic idea is that a Windows FILETIME counts by steps of 10-7 seconds (100-nanosecond intervals) from 1 Jan 1601 (why 1601? no idea...). In Linux you can obtain time in microseconds (10-6) from 1 Jan 1970 using gettimeofday(). Thus the following C function does the job:

    #include 
    /**
     * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
     */
    #define EPOCH_DIFF 11644473600LL
    
    unsigned long long
    getfiletime() {
        struct timeval tv;
        unsigned long long result = EPOCH_DIFF;
        gettimeofday(&tv, NULL);
        result += tv.tv_sec;
        result *= 10000000LL;
        result += tv.tv_usec * 10;
        return result;
    }
    

提交回复
热议问题