muduo库阅读(20)——时间戳类

来源:互联网 发布:网络市场调研报告范文 编辑:程序博客网 时间:2024/05/18 00:53
/* * 时间戳类 */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.///class Timestamp : public muduo::copyable,public boost::less_than_comparable<Timestamp>{public:Timestamp(): microSecondsSinceEpoch_(0){}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; }// for internal usage.// 自纪元以来的微妙数int64_t microSecondsSinceEpoch() const { return microSecondsSinceEpoch_; }// 自纪元以来的秒数time_t secondsSinceEpoch() const{ return static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); }////// Get time of now.///// 当前时间戳static Timestamp now();// 判断时间戳是否有效static Timestamp invalid(){return Timestamp();}// 把unix时间转换成时间戳static Timestamp fromUnixTime(time_t t){return fromUnixTime(t, 0);}// 把unix时间转换成时间戳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_;};// 时间戳比较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);}}

using namespace muduo;// 断言Timestamp的空间占用和int64_t相同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;snprintf(buf, sizeof(buf)-1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds);return buf;}// 把时间戳格式化为字符串string Timestamp::toFormattedString(bool showMicroseconds) const{char buf[32] = {0};time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);struct tm tm_time;gmtime_r(&seconds, &tm_time);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;}// 获取当前时间戳Timestamp Timestamp::now(){struct timeval tv;gettimeofday(&tv, NULL);int64_t seconds = tv.tv_sec;return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);}


0 0