1.源码
#ifndef _NGX_CLOCK_HPP
#define _NGX_CLOCK_HPP
//#include "Nginx.hpp"
class NgxClock final
{
public:
NgxClock() = default;
~NgxClock() = default;
public:
static const ngx_time_t& now()
{
ngx_time_update();
return *ngx_timeofday();
}
public:
void reset()
{
m_time = now();
}
ngx_time_t delta() const
{
auto t = now();
t.sec -= m_time.sec;
t.msec -= m_time.msec;
return t;
}
double elapsed() const
{
auto t = delta();
return t.sec + t.msec * 1.0 / 1000;
}
public:
static decltype((ngx_current_msec)) msec()
{
return ngx_current_msec;
}
public:
static void sleep(ngx_uint_t sec)
{
ngx_msleep(sec * 1000);
}
static void msleep(ngx_uint_t msec)
{
ngx_msleep(msec);
}
private:
ngx_time_t m_time = now();
};
#endif //_NGX_CLOCK_HPP
1.1类定义
class NgxClock final
{
public:
NgxClock() = default;
~NgxClock() = default;
private:
ngx_time_t m_time = now();
};
Ngxclock使用一个成员变量m_time来保存时间值,在构造时调用成员函数 now ()获取当前时间,表示计时的起点。
1.2操作函数
static const ngx_time_t& now ()
static const ngx_time_t& now()
{
ngx_time_update();
return *ngx_timeofday();
}
静态成员函数now ()首先调用ngx_time_update ( ),然后获取更新后的缓存时间:;
ngx_time_t delta () const
ngx_time_t delta() const
{
auto t = now();
t.sec -= m_time.sec;
t.msec -= m_time.msec;
return t;
}
double elapsed() const
{
auto t = delta();
return t.sec + t.msec * 1.0 / 1000;
}
两个函数都是计算时间流逝
static decltype((ngx_current_msec)) msec()
声明类型:也是运行时间 封装ngx_current_msec
2.日期
class NgxDatetime final
{
public:
NgxDatetime() = default;
~NgxDatetime() = default;
public:
static std::time_t since()
{
return ngx_time();
}
static ngx_str_t today()
{
ngx_tm_t tm;
//ngx_gmtime(since(), &tm);
ngx_localtime(since(), &tm);
static u_char buf[20] = {};
auto p = ngx_snprintf(buf, 20,
"%d-%02d-%02d",
tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday);
return ngx_str_t{static_cast<std::size_t>(p - buf), buf};
}
public:
static ngx_str_t http(std::time_t t = since())
{
static u_char buf[50] = {};
auto p = ngx_http_time(buf, t);
return ngx_str_t{static_cast<std::size_t>(p - buf), buf};
}
static std::time_t http(ngx_str_t& str)
{
return ngx_parse_http_time(str.data, str.len);
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/129635.html