ngnix 时钟 ngx_gettimeofday 更新时间

来源:互联网 发布:大疆无人机 人工智能 编辑:程序博客网 时间:2024/04/28 10:40

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. 

voidngx_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}


原创粉丝点击