我是靠谱客的博主 甜美糖豆,这篇文章主要介绍windows gettimeofday,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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; }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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; }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(92)

评论列表共有 0 条评论

立即
投稿
返回
顶部