概述
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#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("%dn",sizeof(CTest));
printf("%lldn",_getTime());
return 0;
}
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#ifdef _WIN32
#include <Windows.h>
//#include "compat.h"
#else
#include <unistd.h>
#endif
#ifdef _WIN32
//https://github.com/google/conscrypt/blob/master/common/src/jni/main/include/conscrypt/compat.h
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
//#include <winsock2.h>
#include <cstddef>
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<char *>(malloc((std::size_t)count + 1));
if (buffer == nullptr) {
count = -1;
} else if ((count = vsnprintf(buffer, static_cast<std::size_t>(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("%dn",sizeof(CTest));
printf("%lldn",_getTime_old());
printf("%lldn",_getTimestamp());//1545616102 正确的时间戳
printf("%lldn",_getTimestamp());
printf("%lldn",_getTimestamp());
printf("%lldn",_getTimestamp2());
#ifdef _WIN32
Sleep(1000*3);
#endif
printf("%lldn",_getTimestamp());//1545616102 正确的时间戳
printf("%lldn",_getTimestamp());
printf("%lldn",_getTimestamp());
return 0;
}
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#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("%lldn",_getTimestamp());//1545616102 正确的时间戳
printf("%lldn",_getTimestamp());
printf("%lldn",_getTimestamp());
printf("%lldn",_getTimestamp2());
#ifdef _WIN32
Sleep(1000*2);
#endif
printf("%lldn",_getTimestamp());//1545616102 正确的时间戳
printf("%lldn",_getTimestamp());
printf("%lldn",_getTimestamp());
return 0;
}
最后
以上就是甜美糖豆为你收集整理的windows gettimeofday的全部内容,希望文章能够帮你解决windows gettimeofday所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复