您的位置:首页 > 其它

ngnix 时钟 ngx_gettimeofday 更新时间

2012-11-02 09:51 260 查看
ngxin 项目,有 windows 版本,之前为了优化性能,用 timeGetTime(); 获取时间。

这样导致了时钟不稳定,时钟正常跑一段时间后就不跑了,或者超时。

用 timeGetTime(); 获取返回的数值不定时出现问题。按 msdn 说,这个函数不能单独直接使用于代码运算。

http://msdn.microsoft.com/en-us/library/ms713418.aspx

timeGetTime

This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution. You should always use the difference between two timeGetTime return values
in computations.

void
ngx_gettimeofday(struct timeval *tp)
{
#ifdef NGX_WIN32
    DWORD dt = timeGetTime(); // 错误代码项
    tp->tv_sec = (long) (dt / 1000);
    tp->tv_usec = (long) (dt*1000);
#else
    ULONGLONG   usec;
    FILETIME    ft;
    SYSTEMTIME  st;

    GetSystemTime(&st);
    SystemTimeToFileTime(&st, &ft);

    usec = ft.dwHighDateTime;
    usec <<= 32;
    usec |= ft.dwLowDateTime;
    usec /= 10;
    usec -= 11644473600000000LL;

    tp->tv_sec = (long) (usec / 1000000);
    tp->tv_usec = (long) (usec % 1000000);
#endif // NGX_WIN32
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: