ngnix 时钟 ngx_gettimeofday 更新时间

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
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值