linux 下的gettimeofday 函数在windows上的替换方案

方案一:

#include
#ifdef WIN32
# include
#else
# include
#endif
#ifdef WIN32
int
gettimeofday(struct timeval *tp, void *tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm. tm_isdst = -1;
clock = mktime(&tm);
tp->tv_sec = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;
return (0);
}
#endif

方案二:

gettimeofday的使用

1
2
3
4
5
6
7
8
//copy from muduo
Timestamp Timestamp::now()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);//返回1970年至今的秒+微秒
  int64_t seconds = tv.tv_sec;
  return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);
}

gettimeofday要求传入一个timeval和一个时区。因为存在微秒数,显然它比 time_t now = ::time(NULL)更精确。

但是这个函数是linux下的。所以我们需要一个跨平台的实现。

以下是一个实现,使用c++的chrono库。

1
2
3
4
5
6
7
8
9
#include <chrono>
int gettimeofday(struct timeval *__restrict __tv, __timezone_ptr_t __tz)
{
auto now = std::chrono::system_clock::now();
auto now_ticks = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());//
__tv->tv_sec = (long)now_ticks.count() / 1000000;
__tv->tv_usec = (long)now_ticks.count() % 1000000;
return 0;
}

1
2
3
now.time_since_epoch()返回的duration比较奇怪,需要转化成微秒。

其实主要是windows没有这个函数,那么我们实现它。
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
//copy from evpp
#ifdef WIN32
int gettimeofday(struct timeval* tp, void* tzp) {
    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 / 10,000,000);
    tp->tv_usec = (long)((intervals % 10000000) / 10);


    return (0);
}