我应该用什么来替换Windows上的gettimeofday()?

前端之家收集整理的这篇文章主要介绍了我应该用什么来替换Windows上的gettimeofday()?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个便携式Socket类,它支持发送和接收的超时…为了实现这些超时,我正在使用select()….但是,我有时需要知道我在select()中阻塞了多长时间当然在Linux上我会通过在调用select()之前和之后调用gettimeofday()然后使用timersub()来计算delta来实现…

鉴于Windows上的select()接受struct timeval的超时,我应该用什么方法替换Windows上的gettimeofday()?

我最终找到了这个页面:windows上的gettimeofday()[编辑:删除链接,因为它现在指向一个广告网站].在Windows上有一个方便,花花公子的gettimeofday()实现.它使用GetSystemTimeAsFileTime()方法获得准确的时钟.

更新:这是一个活动链接[编辑:删除链接,因为它现在指向一个广告网站]指向OP所指的实现.另请注意链接实现中存在拼写错误

#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64 // WRONG
#else
  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL // WRONG
#endif

显示的值在结尾处缺少额外的0(它们假定为微秒,而不是100纳秒间隔的数量).这个拼写错误是在Google代码项目页面上通过this comment找到的.要使用的正确值如下所示:

#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
  #define DELTA_EPOCH_IN_MICROSECS  116444736000000000Ui64 // CORRECT
#else
  #define DELTA_EPOCH_IN_MICROSECS  116444736000000000ULL // CORRECT
#endif

Postgresql为windows实现gettimeofday:

/*
 * gettimeofday.c
 *    Win32 gettimeofday() replacement
 *
 * src/port/gettimeofday.c
 *
 * Copyright (c) 2003 SRA,Inc.
 * Copyright (c) 2003 SKC,Inc.
 *
 * Permission to use,copy,modify,and distribute this software and
 * its documentation for any purpose,without fee,and without a
 * written agreement is hereby granted,provided that the above
 * copyright notice and this paragraph and the following two
 * paragraphs appear in all copies.
 *
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,* INDIRECT,SPECIAL,INCIDENTAL,OR CONSEQUENTIAL DAMAGES,INCLUDING
 * LOST PROFITS,ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
 * DOCUMENTATION,EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES,INCLUDING,BUT NOT
 * LIMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
 * IS" BASIS,AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,* SUPPORT,UPDATES,ENHANCEMENTS,OR MODIFICATIONS.
 */

#include "c.h"

#include <sys/time.h>


/* FILETIME of Jan 1 1970 00:00:00. */
static const unsigned __int64 epoch = ((unsigned __int64) 116444736000000000ULL);

/*
 * timezone information is stored outside the kernel so tzp isn't used anymore.
 *
 * Note: this function is not for Win32 high precision timing purpose. See
 * elapsed_time().
 */
int
gettimeofday(struct timeval * tp,struct timezone * tzp)
{
    FILETIME    file_time;
    SYSTEMTIME  system_time;
    ULARGE_INTEGER ularge;

    GetSystemTime(&system_time);
    SystemTimeToFileTime(&system_time,&file_time);
    ularge.LowPart = file_time.dwLowDateTime;
    ularge.HighPart = file_time.dwHighDateTime;

    tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L);
    tp->tv_usec = (long) (system_time.wMilliseconds * 1000);

    return 0;
}
原文链接:https://www.f2er.com/windows/371944.html

猜你在找的Windows相关文章