windows下实现gettimeofday

xingyun86 2019-5-10 1449

// For windows implementation of "gettimeofday"
#if defined(_WIN32) || defined(WIN32)
#include <time.h>
#include <windows.h> //I've ommited this line.

#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
#endif

struct timezone
{
    int  tz_minuteswest; /* minutes W of Greenwich */
    int  tz_dsttime;     /* type of dst correction */
};
__inline static 
int gettimeofday(struct timeval* tv, struct timezone* tz)
{
    FILETIME f = { 0 };
    static char _tz = 0x00;
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
    unsigned long long u = 0Ui64;
#else
    unsigned long long u = 0ULL;
#endif
    if (tv)
    {
#ifdef _WIN32_WCE 
        SYSTEMTIME s = { 0 };
        ::GetSystemTime(&s);
        ::SystemTimeToFileTime(&s, &f);
#else 
        ::GetSystemTimeAsFileTime(&f);
#endif 
        u |= f.dwHighDateTime;
        u <<= 32;
        u |= f.dwLowDateTime;
        //convert into microseconds
        u /= 10;  
        //converting file time to unix epoch
        u -= DELTA_EPOCH_IN_MICROSECS;
        tv->tv_sec = (long)(u / 1000000UL);
        tv->tv_usec = (long)(u % 1000000UL);
    }
    if (tz)
    {
        if (_tz == 0x00)
        {
            _tzset();
            _tz=0x01;
        }
        tz->tz_minuteswest = _timezone / 60;
        tz->tz_dsttime = _daylight;
    }
    return 0;
}

#endif


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回