muduo源码解析9-timezone类

timezone

class timezone:public copyable
{
};

 

作用:

感觉有点看不懂,detail内部实现文件类不明白跟时区有什么关系.
timezone类主要是完成各个时区之间的转换,感觉自己用不到,一个时区就够了.
内部成员变量就一个,是个Data结构体的共享智能指针
提供了3个构造函数,
TimeZone(const char* zonefile)
构造函数,构造函数是explicit的,内置指针不能隐式的转换,只能使用初始化的形式
TimeZone(int eastOfUtc, const char* tzname);
eastOfUtc表示UTC时间。中国内地的时间与UTC的时差为+8,也就是UTC+8
这里根据源代码,eastOfUtc 应该是秒数
TimeZone() = default
默认构造函数

成员变量:

private:
    std::shared_ptr<Data> m_data;

 这个Data是自定义的结构体,里面主要是一些timezone相关的信息,我没怎么看懂,暂时留个坑,不过感觉也

用不到多个时区。

成员函数:

public:
    explicit timezone(const char* zonefile);
    timezone(int eastOfUtc,const char* tzname);
    timezone()=default;

    bool valid() const
    {
        return static_cast<bool>(m_data);
    }

    //UTC时间转换成当地时间
    struct tm toLocalTime(time_t sec) const;
    //当地时间转换成UTC时间
    time_t fromLocalTime(const struct tm&) const;

    //当地时间转换成UTC时间
    static struct tm toUtcTime(time_t sec,bool yday=false);


    //UTC时间转换成当地时间
    static time_t fromUtcTime(const struct tm&);

    //
    static time_t fromUtcTime(int year,int month,int day,
                              int hour,int min,int sec);

测试(略过,没看懂):

 

posted @ 2020-08-23 23:03  WoodInEast  阅读(253)  评论(0编辑  收藏  举报