我是靠谱客的博主 体贴冰淇淋,这篇文章主要介绍gettimeofday在VS上的实现,现在分享给大家,希望可以做个参考。

gettimeofday是Linux上的函数,使用方法如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <sys/time.h> const char* srs_human_format_time() { struct timeval tv; static char buf[23]; memset(buf, 0, sizeof(buf)); // clock time if (gettimeofday(&tv, NULL) == -1) { return buf; } // to calendar time struct tm* tm; if ((tm = localtime((const time_t*)&tv.tv_sec)) == NULL) { return buf; } snprintf(buf, sizeof(buf), "%d-%02d-%02d %02d:%02d:%02d.%03d", 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000)); buf[sizeof(buf) - 1] = 0; return buf; }

该函数返回如 2018-02-25 09:32:11 的格式化时间字符串

而 sys/time.h 只是在linux系统上可以调用,在vs2010中:

复制代码
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
#include <WinSock.h> #define SECS_TO_FT_MULT 10000000 static LARGE_INTEGER base_time; typedef struct win_time_val { /** The seconds part of the time. */ long sec; /** The miliseconds fraction of the time. */ long msec; } win_time_val_t; typedef struct win_time { /** This represents day of week where value zero means Sunday */ int wday; /** This represents day of month: 1-31 */ int day; /** This represents month, with the value is 0 - 11 (zero is January) */ int mon; /** This represent the actual year (unlike in ANSI libc where * the value must be added by 1900). */ int year; /** This represents the second part, with the value is 0-59 */ int sec; /** This represents the minute part, with the value is: 0-59 */ int min; /** This represents the hour part, with the value is 0-23 */ int hour; /** This represents the milisecond part, with the value is 0-999 */ int msec; }win_time_t; // Find 1st Jan 1970 as a FILETIME static void get_base_time(LARGE_INTEGER *base_time) { SYSTEMTIME st; FILETIME ft; memset(&st,0,sizeof(st)); st.wYear=1970; st.wMonth=1; st.wDay=1; SystemTimeToFileTime(&st, &ft); base_time->LowPart = ft.dwLowDateTime; base_time->HighPart = ft.dwHighDateTime; base_time->QuadPart /= SECS_TO_FT_MULT; } int win_gettimeofday(win_time_val_t *tv) { SYSTEMTIME st; FILETIME ft; LARGE_INTEGER li; static char get_base_time_flag=0; if (get_base_time_flag == 0) { get_base_time(&base_time); } /* Standard Win32 GetLocalTime */ GetLocalTime(&st); SystemTimeToFileTime(&st, &ft); li.LowPart = ft.dwLowDateTime; li.HighPart = ft.dwHighDateTime; li.QuadPart /= SECS_TO_FT_MULT; li.QuadPart -= base_time.QuadPart; tv->sec = li.LowPart; tv->msec = st.wMilliseconds; return 0; } int win_time(const win_time_val_t *tv, win_time_t *time) { LARGE_INTEGER li; FILETIME ft; SYSTEMTIME st; li.QuadPart = tv->sec; li.QuadPart += base_time.QuadPart; li.QuadPart *= SECS_TO_FT_MULT; ft.dwLowDateTime = li.LowPart; ft.dwHighDateTime = li.HighPart; FileTimeToSystemTime(&ft, &st); time->year = st.wYear; time->mon = st.wMonth-1; time->day = st.wDay; time->wday = st.wDayOfWeek; time->hour = st.wHour; time->min = st.wMinute; time->sec = st.wSecond; time->msec = tv->msec; return 0; } const char* srs_human_format_time() { static char buf[23]; memset(buf, 0, sizeof(buf)); win_time_val_t wintv; win_time_t wintime; win_gettimeofday(&wintv); win_time(&wintv,&wintime); _snprintf(buf,sizeof(buf),"%d-%d-%d %d:%d:%d", wintime.year,wintime.mon+1,wintime.day, wintime.hour,wintime.min,wintime.sec); buf[sizeof(buf) - 1] = 0; return buf; }

最后在main方法里:

复制代码
1
printf("%s", srs_human_format_time());


最后

以上就是体贴冰淇淋最近收集整理的关于gettimeofday在VS上的实现的全部内容,更多相关gettimeofday在VS上内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部