Nginx源码重作–时间 日期

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 Nginx源码重作–时间 日期,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

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

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!