windows gettimeofday

#include 
#include 
#include 

#ifdef _WIN32
    #include 
#else
    #include 
#endif


void gettimeofday(struct timeval *tp,struct timezone *tz)
{
    uint64_t  intervals;
    FILETIME  ft;

    GetSystemTimeAsFileTime(&ft);

    /*
     * A file time is a 64-bit value that represents the number
     * of 100-nanosecond intervals that have elapsed since
     * January 1, 1601 12:00 A.M. UTC.
     *
     * Between January 1, 1970 (Epoch) and January 1, 1601 there were
     * 134744 days,
     * 11644473600 seconds or
     * 11644473600,000,000,0 100-nanosecond intervals.
     *
     * See also MSKB Q167296.
     */

    intervals = ((uint64_t) ft.dwHighDateTime << 32) | ft.dwLowDateTime;
    intervals -= 116444736000000000;

    tp->tv_sec = (long) (intervals / 10000000);
    tp->tv_usec = (long) ((intervals % 10000000) / 10);
}


static long _getTime(void)
{
    struct timeval  now;
#ifdef _WIN32
    gettimeofday(&now);
#else
    gettimeofday(&now,NULL);
#endif
    return (long)(now.tv_sec*1000 + now.tv_usec/1000);
}


int main()
{

    //printf("%d\n",sizeof(CTest));
    printf("%lld\n",_getTime());

    return 0;
}
#include 
#include 
#include 
 
#ifdef _WIN32
    #include 
	//#include "compat.h"
#else
    #include 
#endif

#ifdef _WIN32
//https://github.com/google/conscrypt/blob/master/common/src/jni/main/include/conscrypt/compat.h
#include 
#include 
#include 
//#include 
#include 

typedef long ssize_t;
#define strerror_r(errnum, buf, buflen) strerror_s(buf, buflen, errnum)
#define strcasecmp _stricmp

// Windows doesn't define this either *sigh*...
inline int vasprintf(char **ret, const char *format, va_list args) {
    va_list copy;
    va_copy(copy, args);
    *ret = nullptr;

    int count = vsnprintf(nullptr, 0, format, args);
    if (count >= 0) {
        char *buffer = static_cast(malloc((std::size_t)count + 1));
        if (buffer == nullptr) {
            count = -1;
        } else if ((count = vsnprintf(buffer, static_cast(count + 1), format, copy)) <
                   0) {
            free(buffer);
        } else {
            *ret = buffer;
        }
    }
    va_end(copy);  // Each va_start() or va_copy() needs a va_end()

    return count;
}

inline int asprintf(char **strp, const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    int r = vasprintf(strp, fmt, ap);
    va_end(ap);
    return r;
}

inline int gettimeofday(struct timeval *tp, struct timezone *) {
    // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing
    // zero's
    // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
    // until 00:00:00 January 1, 1970
    static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);

    SYSTEMTIME system_time;
    FILETIME file_time;
    uint64_t time;

    GetSystemTime(&system_time);
    SystemTimeToFileTime(&system_time, &file_time);
    time = ((uint64_t)file_time.dwLowDateTime);
    time += ((uint64_t)file_time.dwHighDateTime) << 32;

    tp->tv_sec = (long)((time - EPOCH) / 10000000L);
    tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
    return 0;
}
#endif  // _WIN32

 
static long _getTime_old(void)
{
    struct timeval  now;
#ifdef _WIN32
    gettimeofday(&now,NULL);
#else
    gettimeofday(&now,NULL);
#endif
    return (long)(now.tv_sec*1000 + now.tv_usec/1000);
}

static long _getTimestamp(void)
{
    struct timeval  now;
    gettimeofday(&now,NULL);
    return now.tv_sec;
}
static long _getTimestamp2(void)
{
    struct timeval  now;
    gettimeofday(&now,NULL);
    return now.tv_usec;
}
 
 
 
int main()
{
 
    //printf("%d\n",sizeof(CTest));
    printf("%lld\n",_getTime_old());
	printf("%lld\n",_getTimestamp());//1545616102 正确的时间戳
	printf("%lld\n",_getTimestamp());
	printf("%lld\n",_getTimestamp());
	printf("%lld\n",_getTimestamp2());
#ifdef _WIN32
	Sleep(1000*3);
#endif
	
	printf("%lld\n",_getTimestamp());//1545616102 正确的时间戳
	printf("%lld\n",_getTimestamp());
	printf("%lld\n",_getTimestamp());
 
    return 0;
}
#include 
#include 
#include 
 
#ifdef _WIN32
    #include 
#else
    #include 
#endif

#ifdef _WIN32
//https://blog.csdn.net/shan165310175/article/details/48933585

#define ngx_gettimeofday gettimeofday
void ngx_gettimeofday(struct timeval *tp, struct timezone *)
{
    uint64_t  intervals;
    FILETIME  ft;

    GetSystemTimeAsFileTime(&ft);
 
    /*
     * A file time is a 64-bit value that represents the number
     * of 100-nanosecond intervals that have elapsed since
     * January 1, 1601 12:00 A.M. UTC.
     *
     * Between January 1, 1970 (Epoch) and January 1, 1601 there were
     * 134744 days,
     * 11644473600 seconds or
     * 11644473600,000,000,0 100-nanosecond intervals.
     *
     * See also MSKB Q167296.
     */
 
    intervals = ((uint64_t) ft.dwHighDateTime << 32) | ft.dwLowDateTime;
    intervals -= 116444736000000000;
 
    tp->tv_sec = (long) (intervals / 10000000);
    tp->tv_usec = (long) ((intervals % 10000000) / 10);
}
#endif  // _WIN32

 
static long _getTimestamp(void)
{
    struct timeval  now;
    gettimeofday(&now,NULL);
    return now.tv_sec;
}
static long _getTimestamp2(void)
{
    struct timeval  now;
    gettimeofday(&now,NULL);
    return now.tv_usec;
}
 
 
int main()
{
	printf("%lld\n",_getTimestamp());//1545616102 正确的时间戳
	printf("%lld\n",_getTimestamp());
	printf("%lld\n",_getTimestamp());
	printf("%lld\n",_getTimestamp2());
#ifdef _WIN32
	Sleep(1000*2);
#endif
	
	printf("%lld\n",_getTimestamp());//1545616102 正确的时间戳
	printf("%lld\n",_getTimestamp());
	printf("%lld\n",_getTimestamp());
 
    return 0;
}

 

你可能感兴趣的:(c/c++)