muduo库Timestamp、Date、TimeZone分析

先了解关于时间的一个结构体和获取时间的一个函数。

#include <time.h>
struct timeval  
{  
    __time_t tv_sec;        /* Seconds. */  
    __suseconds_t tv_usec;  /* Microseconds. */  
}; 

tv_sec指从Epoc到现在的秒数。
tv_usec指从Epoch到现在的微秒数。
Epoch指的是一个特定的时间:1970-01-01 00:00:00 UTC。

获取时间的函数

#include <sys/time.h>
int gettimeofday(struct timeval* tv,struct timezone× tz )

把当前时间信息放到tv中,把当地时区信息放到tz中(如果不需要可以为NULL)。这个函数是C库提供的函数,在封装实现中调用了sys_gettimeofday系统调用,所以归根到底还是调用了系统调用。

更多关于时间的内容,参考这里

Timestamp类

类结构代码如下:

class Timestamp : public muduo::copyable,
                  public boost::less_than_comparable<Timestamp>
{
 public:
  Timestamp();
  explicit Timestamp(int64_t microSecondsSinceEpochArg);
  void swap(Timestamp& that);
  string toString() const;
  string toFormattedString(bool showMicroseconds = true) const;
  bool valid() const { return microSecondsSinceEpoch_ > 0; }

  int64_t microSecondsSinceEpoch() const ;
  time_t secondsSinceEpoch() const;
  static Timestamp now();
  static Timestamp invalid();
  static Timestamp fromUnixTime(time_t t)
  static Timestamp fromUnixTime(time_t t, int microseconds);
  static const int kMicroSecondsPerSecond = 1000 * 1000;
 private:
  int64_t microSecondsSinceEpoch_;
};

继承public muduo::copyable表示可以复制,即值传递。继承public boost::less_than_comparable表示可以作大小比较(!=,>,<=,>=);在实现时,只需实现<和==即可,其他可以自动生成;比较运算符的重载是以友元的形式实现的。

变量microSecondsSinceEpoch_保持着Epoch到现在的微秒数,为了方便微秒和秒之间的转换,定义了const static变量kMicroSecondsPerSecond。microSecondsSinceEpoch_是int64_t类型,在32位和64位计算机有不同的类型表示,如下定义所示:

# if __WORDSIZE == 64
typedef long int              int64_t;
# else
__extension__
typedef long long int      int64_t;
#endif

里面定义的一下函数,有些是为了获取一定格式时间;与string相关的是为了把时间转换为string格式。可以用来测试一下,获取当前时间,转换为string.

#include <muduo/base/Timestamp.h>
#include<stdio.h>
using namespace muduo;

int main()
{
    Timestamp timeStamp=Timestamp::now();
    printf("it's %lld microseconds from Epoch\n", timeStamp.microSecondsSinceEpoch());
    string str= timeStamp.toString();
    printf("sting format is %s\n", str.c_str());
    return 0;
}

Date类

类代码如下:

class Date : public muduo::copyable
{
 public:
  struct YearMonthDay
  {
    int year; // [1900..2500]
    int month;  // [1..12]
    int day;  // [1..31]
  };
  static const int kDaysPerWeek = 7;
  static const int kJulianDayOf1970_01_01;
  Date();
  Date(int year, int month, int day);
  explicit Date(int julianDayNum);
  explicit Date(const struct tm&);
  void swap(Date& that);
  bool valid() const { return julianDayNumber_ > 0; }
  string toIsoString() const;
  struct YearMonthDay yearMonthDay() const;
  int year() const;
  int month() const;
  int day() const;
  // [0, 1, ..., 6] => [Sunday, Monday, ..., Saturday ]
  int weekDay() const;
  int julianDayNumber() const { return julianDayNumber_; }
 private:
  int julianDayNumber_;
};

日期是采用Julian day来表示的,什么是Julian day,大概来说就是现在到公元前4713年1月1日的天数,具体可以参考维基百科这里或百度百科这里

私有变量julianDayNumber_存储着这个天数,定义了结构体YearMonthDay方便两者之间的转换,toIosString把日期转换为string。

可以测试一下:

#include <muduo/base/Date.h>
#include <iostream>
using namespace muduo;

int main()
{
    Date date(2015,7,27);
    int julianDayNum=date.julianDayNumber();
    std::cout<<"Julian Day Num is"<<julianDayNum<<std::endl;
    Date date2(julianDayNum);
    struct Date::YearMonthDay yearMonthDay=date2.yearMonthDay();
    std::cout<<yearMonthDay.year<<"--"<<yearMonthDay.month<<"--"<<yearMonthDay.day<<std::endl;

    return 0;
}

TimeZone类

先看个数据结构

#include <time.h>
struct tm {
    int tm_sec;       /* 秒 – 取值区间为[0,59] */
    int tm_min;       /* 分 - 取值区间为[0,59] */
    int tm_hour;      /* 时 - 取值区间为[0,23] */
    int tm_mday;      /* 一个月中的日期 - 取值区间为[1,31] */
    int tm_mon;     /* 月份(从一月开始,0代表一月) 取值区间为[0,11] */
    int tm_year;      /* 年份,其值等于实际年份减去1900 */
    int tm_wday;      /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
          int tm_yday;      /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
          int tm_isdst;     /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
          };

还有一个类型time_t。time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数,它是一个长整数,
日历时间(Calendar Time)是通过time_t数据类型来表示的,用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。在time.h中,我们也可以看到time_t是一个长整型数,表示的时间不能晚于2038年1月18日19时14分07秒

TimeZone类是为了方便时区之间的转换,以及时令之间的转换。其定义如下:

// TimeZone for 1970~2030
class TimeZone : public muduo::copyable
{
 public:
  explicit TimeZone(const char* zonefile);
  TimeZone(int eastOfUtc, const char* tzname);  // a fixed timezone
  TimeZone() {}  // an invalid timezone
  bool valid() const;

  struct tm toLocalTime(time_t secondsSinceEpoch) const;
  time_t fromLocalTime(const struct tm&) const;
  static struct tm toUtcTime(time_t secondsSinceEpoch, bool yday = false);
  static time_t fromUtcTime(const struct tm&);
  static time_t fromUtcTime(int year, int month, int day,
                            int hour, int minute, int seconds);
  struct Data;//forward declaration
 private:
  boost::shared_ptr<Data> data_;
};

TimeZone(int eastOfUtc, const char* tzname)构造函数中,eastOfUtc表示UTC时间。中国内地的时间与UTC的时差为+8,也就是UTC+8。美国是UTC-5。

可以写个测试代码:

#include <muduo/base/TimeZone.h>
#include <muduo/base/Timestamp.h>

#include <time.h>

#include <iostream>
using namespace muduo;
void PrintTm(struct tm& T)
{
    std::cout<<T.tm_year<<"-"<<T.tm_mon<<"-"<<T.tm_mday<<std::endl;
    std::cout<<T.tm_hour<<"-"<<T.tm_min<<"-"<<T.tm_sec<<std::endl;
    std::cout<<T.tm_wday<<std::endl;
}
int main()
{

    Timestamp timeStamp=Timestamp::now();
    struct tm T=TimeZone::toUtcTime(timeStamp.secondsSinceEpoch());
    PrintTm(T);

    TimeZone timeZone(8, "China");

    struct  tm T2=timeZone.toLocalTime(timeStamp.secondsSinceEpoch());
    PrintTm(T2);

    return 0;
}

运行结果:

115-6-27
11-51-34
1
115-6-27
11-51-42
1

年的时间是从1900算起,所以115代表2015。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值