本帖最后由 古月不傲 于 2021-1-13 15:50 编辑
[C++] 纯文本查看 复制代码 // Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
#ifndef MUDUO_BASE_TIMESTAMP_H
#define MUDUO_BASE_TIMESTAMP_H
//#include "muduo/base/copyable.h"
//#include "muduo/base/Types.h"
//#include <boost/operators.hpp>
#include <iostream>
#include <string>
#include <stdint.h>
#include <inttypes.h>
#include <sys/time.h>
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.
/// 基于UTC的时间戳,使用微妙
// 该类是不可改变的
// 建议通过值传递,因为是在64位机器上嘛
class Timestamp // :
//public muduo::copyable, // 显示该对象可以被拷贝、赋值
//public boost::equality_comparable<Timestamp>, // 只要重载== 自动重载 !=
//public boost::less_than_comparable<Timestamp> // 只要重载< 自动重载 > <= >=
{
public:
///
/// Constucts an invalid Timestamp.
///
Timestamp()
: microSecondsSinceEpoch_(0)
{
}
///
/// Constucts a Timestamp at specific time
///
/// @param microSecondsSinceEpoch
// 在特定时间内构造时间戳
explicit Timestamp(int64_t microSecondsSinceEpochArg)
: microSecondsSinceEpoch_(microSecondsSinceEpochArg)
{
}
public:
// 交换时间
void swap(Timestamp& that)
{
std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
}
// default copy/assignment/dtor are Okay
// 检测时间是否有效
bool valid() const
{
return microSecondsSinceEpoch_ > 0;
}
// for internal usage.
// 返回微妙,用于重载比较
int64_t microSecondsSinceEpoch() const
{
return microSecondsSinceEpoch_;
}
// 返回妙 用微妙 / 1000000即可
time_t secondsSinceEpoch() const
{
return static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
}
// 编译时断言
// static_assert(sizeof(Timestamp) == sizeof(int64_t),
// "Timestamp should be same size as int64_t");
// 将 秒 + 微妙 格式成字符串
std::string toString() const
{
char buf[32] {};
int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond;
int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond;
snprintf(buf, sizeof(buf), "%" PRId64 ".%06" PRId64 "", seconds, microseconds);
return buf;
}
// 输出当前时间格式
std::string toFormattedString(bool showMicroseconds) const
{
char buf[64] {};
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;
}
public: // 注意以下是静态成员函数
// 获取当前的时间
static Timestamp now()
{
struct timeval tv;
gettimeofday(&tv, nullptr);
int64_t seconds = tv.tv_sec;
// 秒 * 微妙 = 微妙 + 当前的微妙
return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);
}
// 获取无效的时间,返回0即可
static Timestamp invalid()
{
return Timestamp();
}
static Timestamp fromUnixTime(time_t t)
{
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_; // 微妙
};
// 重载 < 由于继承了 public boost::less_than_comparable<Timestamp>
// 所以会默认重载 > <= >=
inline bool operator<(Timestamp lhs, Timestamp rhs)
{
return lhs.microSecondsSinceEpoch() < rhs.microSecondsSinceEpoch();
}
// 重载 == 由于继承了 public boost::equality_comparable<Timestamp>
// 所以会默认重载 !=
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
///
// 对象 + seconds > microsenconds
inline Timestamp addTime(Timestamp timestamp, double seconds)
{
// 转换成微妙
int64_t delta = static_cast<int64_t>(seconds * Timestamp::kMicroSecondsPerSecond);
// 当前对象 + 微妙即可
return Timestamp(timestamp.microSecondsSinceEpoch() + delta);
}
} // namespace muduo
#endif // MUDUO_BASE_TIMESTAMP_H
|