Timestamp.h的研究
- Timestamp.h的类图如下,参考下即可
- Timestamp中用了一个BOOST_STATIC_ASSERT宏,这是编译时断言,而我们平时用的assert是运行时断言
#include <iostream>
#include <boost/static_assert.hpp>
class Timestamp {
private:
int64_t microSecondsSinceEpoch_;
};
BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));
int main()
{
BOOST_STATIC_ASSERT(3 == 2);
return 0;
}
第一个通过,第二个3=2不会通过,输出test.cpp:13:2: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE’,编译器断言检查。
下面来分析正文,里面都加上了注释:
#ifndef MUDUO_BASE_TIMESTAMP_H
#define MUDUO_BASE_TIMESTAMP_H
#include <muduo/base/copyable.h>
#include <muduo/base/Types.h>
#include <boost/operators.hpp>
namespace muduo
{
///
/// Time stamp in UTC, in microseconds resolution.
///
/// This class is immutable.
/// It's recommended to pass it by value, since it's passed in register on x64.
///
//muduo::copyable空基类,标识类,值类型,凡是继承了它就可以拷贝
//值语义,可以拷贝,拷贝之后与原对象脱离关系
//对象语义,要么不能拷贝,要么可以拷贝,拷贝之后与原对象仍存在一定关系,比如共享一定资源,取决于自己的拷贝构造函数
class Timestamp : public muduo::copyable,
public boost::less_than_comparable<Timestamp>
//less_than_comparable<>是boost的一个模板类,继承该类要求实现<,然后它会自动实现>,<=,>=,有点牛逼。。。
{
public:
///
/// Constucts an invalid Timestamp.
///
Timestamp()
: microSecondsSinceEpoch_(0) //从1970年到当前的时间,单位微秒
{
}
///
/// Constucts a Timestamp at specific time
///
/// @param microSecondsSinceEpoch
explicit Timestamp(int64_t microSecondsSinceEpochArg)
: microSecondsSinceEpoch_(microSecondsSinceEpochArg)
{
}
//交换函数,形参是引用
void swap(Timestamp& that)
{
std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
}
// default copy/assignment/dtor are Okay
string toString() const;
string toFormattedString(bool showMicroseconds = true) const;
bool valid() const { return microSecondsSinceEpoch_ > 0; } //大于0有效
// for internal usage.
int64_t microSecondsSinceEpoch() const { return microSecondsSinceEpoch_; }
//time_t不是结构体,定义为tydef long time_t,一般存放从unxi时间到当前的秒数
time_t secondsSinceEpoch() const //微秒转化为秒
{ return static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); }
///
/// Get time of now.
///
static Timestamp now();
static Timestamp invalid()
{
return Timestamp(); //获取一个无效时间,即时间等于0
}
static Timestamp fromUnixTime(time_t t) //time_t类型时间转化为内部是unix时间的Timestamp结构
{
return fromUnixTime(t, 0); //调用内部函数执行实际操作,微秒忽略
}
static Timestamp fromUnixTime(time_t t, int microseconds)
{
return Timestamp(static_cast<int64_t>(t) * kMicroSecondsPerSecond + microseconds);
}
static const int kMicroSecondsPerSecond = 1000 * 1000; //一百万,一微秒等于百万分之一秒
private:
int64_t microSecondsSinceEpoch_;
};
//只需重载<,> <= >= 这几种less_than_comparable()帮我们实现
inline bool operator<(Timestamp lhs, Timestamp rhs)
{
return lhs.microSecondsSinceEpoch() < rhs.microSecondsSinceEpoch();
}
//判等
inline bool operator==(Timestamp lhs, Timestamp rhs)
{
return lhs.microSecondsSinceEpoch() == rhs.microSecondsSinceEpoch();
}
///
/// Gets time difference of two timestamps, result in seconds.
///
/// @param high, low
/// @return (high-low) in seconds
/// @c double has 52-bit precision, enough for one-microsecond
/// resolution for next 100 years.
inline double timeDifference(Timestamp high, Timestamp low) //实现两个事件的差
{
int64_t diff = high.microSecondsSinceEpoch() - low.microSecondsSinceEpoch();
return static_cast<double>(diff) / Timestamp::kMicroSecondsPerSecond; //将返回时间差的秒数,注意单位!
}
///
/// Add @c seconds to given timestamp.
///
/// @return timestamp+seconds as Timestamp
///
inline Timestamp addTime(Timestamp timestamp, double seconds) //
{
//把秒转化为微秒,构造一个对象,再把它们的时间加起来,构造一个无名临时对象返回
int64_t delta = static_cast<int64_t>(seconds * Timestamp::kMicroSecondsPerSecond);
return Timestamp(timestamp.microSecondsSinceEpoch() + delta);
}
}
#endif // MUDUO_BASE_TIMESTAMP_H
cpp实现:
#include <muduo/base/Timestamp.h>
#include <sys/time.h>
#include <stdio.h>
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#include <boost/static_assert.hpp>
using namespace muduo;
//编译阶段断言
BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));
string Timestamp::toString() const
{
char buf[32] = {0};
int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond;
int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond;
//PRId64跨平台打印64位整数,因为int64_t用来表示64位整数,在32位系统中是long long int,64位系统中是long int
//所以打印64位是%ld或%lld,可移植性较差,不如统一同PRID64来打印。
snprintf(buf, sizeof(buf)-1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds);
return buf;
}
//把它转换成一个格式化字符串
string Timestamp::toFormattedString(bool showMicroseconds) const
{
char buf[32] = {0};
//计算1970到现在的秒数
time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
struct tm tm_time;
gmtime_r(&seconds, &tm_time); //把秒数转换成tm结构体,加_r表示是一个线程安全的函数
if (showMicroseconds) //是否显示微秒标识
{
//取余是微秒
int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);
//年月日时分秒
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
microseconds);
}
else
{
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
}
return buf; //返回string,隐式构造!!!
}
Timestamp Timestamp::now()
{
struct timeval tv;
gettimeofday(&tv, NULL); //获得当前时间,第二个参数是一个时区,当前不需要返回时区,就填空指针
int64_t seconds = tv.tv_sec; //取出秒数
return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); //秒*100万+微秒,就是从1970到现在的微秒数
}