class Timestamp : public muduo::copyable,
public boost::equality_comparable<Timestamp>,
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_);
}
//判断时间是否有效
bool valid() const { return microSecondsSinceEpoch_ > 0; }
//返回一个无效的时间
static Timestamp invalid()
{
return Timestamp();
}
//从1970-01-01 00:00:00到现在的的微秒数
int64_t microSecondsSinceEpoch() const { return microSecondsSinceEpoch_; }
//从1970-01-01 00:00:00到现在的的秒数
time_t secondsSinceEpoch() const
{
//microSecondsSinceEpoch_:从1970-01-01 00:00:00到现在的的微秒数
//kMicroSecondsPerSecond:每秒所对应的微秒数1000 * 1000
return static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
}
static Timestamp now()
{
struct timeval tv;
gettimeofday(&tv, NULL); //获取当前的时间
int64_t seconds = tv.tv_sec;
// 秒数 * 每秒对应的微秒数 + 微秒数
//用微秒数构造Timestamp对象,并返回
return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);
}
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);
}
//转换成字符串格式:秒.微秒
string 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;
}
//转换成指定格式
// 年月日 时:分:秒 20180905 08:45:56
string toFormattedString(bool showMicroseconds = true) const
{
char buf[64] = {0};
time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); //秒
struct tm tm_time;
gmtime_r(&seconds, &tm_time); //将日历时间seconds转换为用UTC时间表示的时间struct tm 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;
}
static const int kMicroSecondsPerSecond = 1000 * 1000;//每秒所对应的微秒数
private:
//唯一的成员变量:表示1970年01月01日00时00分00秒起至现在的总微秒数
int64_t microSecondsSinceEpoch_;
};
两个全局函数:timeDifference、addTime
//计算时间差:high-low,单位:秒
inline double timeDifference(Timestamp high, Timestamp low)
{
int64_t diff = high.microSecondsSinceEpoch() - low.microSecondsSinceEpoch();
return static_cast<double>(diff) / Timestamp::kMicroSecondsPerSecond;
}
//将Timestamp类型的变量,加上seconds秒
inline Timestamp addTime(Timestamp timestamp, double seconds)//此处的timestamp是值传递
{
// 总微秒数 = 秒数 * 每秒所对应的微秒数,即1000*1000
int64_t delta = static_cast<int64_t>(seconds * Timestamp::kMicroSecondsPerSecond);
//先转换成微秒:timestamp.microSecondsSinceEpoch() + delta
//再用微妙构建时间对象Timestamp
return Timestamp(timestamp.microSecondsSinceEpoch() + delta);
}
#include
class Timestamp
{
private:
int64_t microSecondsSinceEpoch_;
};
// 编译时断言通过
BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));
// 编译时断言失败
BOOST_STATIC_ASSERT(sizeof(int) == sizeof(short));
int main(void)
{
return 0;
}
printf("%ld",value); //64bit OS
printf("%lld",value); //32bit OS
上面的方法是不可以移植的,跨平台的做法:
// C++使用PRID64,需要两步:
// 包含头文件:
// 定义宏:__STDC_FORMAT_MACROS,可以通过编译时加-D__STDC_FORMAT_MACROS,或者在包含文件之前定义这个宏。
#define __STDC_FORMAT_MACROS
#include
#undef __STDC_FORMAT_MACROS
printf("%" PRId64 "\n", value);
原子自增操作 *ptr+value
type __sync_fetch_and_add(type* ptr,type value)
返回值:没有加value之前的值
原子比较和(设置)操作
type __sync_val_compare_and_swap(type* ptr,type oldval, type newval)
先比较,再设置:if (*ptr == oldval),则*ptr = newval
返回值:返回原来的值oldval
bool __sync_bool_compare_and_swap(type* ptr,type oldval, type newval)
返回值:if (*ptr == oldval){*ptr=newval; 返回true}
else {不进行设置; 返回false;}
原子赋值操作 *ptr=value
type __sync_lock_test_and_set(type* ptr,type value)