muduo網路庫Timestamp類
1 Timestamp類主要的檔案
muduo/base/Timestamp.cc
muduo/base/Timestamp.h
muduo/base/copyable.h
muduo/base/Types.h
其中Timestamp.cc和Timestamp.h 是類的檔案。
copyable.h檔案中包括的空類是一個標識類,表示繼承該類的所有類都是可複製的,是值語義。
Types.h檔案主要使用了該檔案中對string類的包含。
2 Timestamp.h
#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.
///
class Timestamp : public muduo::copyable,
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 microSecondsSinceEpoch);
void swap(Timestamp& that)
{
std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
}
// default copy/assignment/dtor are Okay
string toString() const;
string toFormattedString() 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();
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-microseciond
/// 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
3 繼承boost::less_than_comparable
boost::less_than_comparable<>是模版類,繼承這個類,表明要求實現<,然後就自動實現>,<=,>=這些比較運算子。這種程式設計是一種模版元的程式設計思想。
可以減少實現的程式碼。
4 Timestamp.cpp
#include <muduo/base/Timestamp.h>
#include <sys/time.h>
#include <stdio.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#undef __STDC_FORMAT_MACROS
#include <boost/static_assert.hpp>
using namespace muduo;
BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));
Timestamp::Timestamp(int64_t microseconds)
: microSecondsSinceEpoch_(microseconds)
{
}
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() const
{
char buf[32] = {0};
time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);
struct tm tm_time;
gmtime_r(&seconds, &tm_time);//thread safe function
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);
return buf;
}
Timestamp Timestamp::now()
{
struct timeval tv;
gettimeofday(&tv, NULL);
int64_t seconds = tv.tv_sec;
return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);
}
Timestamp Timestamp::invalid()
{
return Timestamp();
}
5 PRId64 是什麼東東
是標頭檔案/usr/include/inttypes.h中定義的一個巨集。可以自動根據作業系統的版本,來表示是"ld"還是"lld"。
6 gmtime_r函式
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
gmtime(執行緒不安全的)是把日期和時間轉換為格林威治(GMT)時間的函式。將引數timep 所指的time_t 結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。使用gmtime後要立即處理結果,否則返回的指標指向的內容可能會被覆蓋。
一個好的方法是使用gmtime_r(執行緒安全的),gmtime_r()函式功能與此相同,但是它可以將資料儲存到使用者提供的結構體中,由於使用了使用者分配的記憶體,是不會出錯的。
7 測試用例
muduo/base/tests/Timestamp_unittest.cc檔案是該類的測試檔案。
#include <muduo/base/Timestamp.h>
#include <vector>
#include <stdio.h>
using muduo::Timestamp;
void passByConstReference(const Timestamp& x)
{
printf("%s\n", x.toString().c_str());
}
void passByValue(Timestamp x)
{
printf("%s\n", x.toString().c_str());
}
void benchmark()
{
const int kNumber = 1000*1000;
std::vector<Timestamp> stamps;
stamps.reserve(kNumber);
for (int i = 0; i < kNumber; ++i)
{
stamps.push_back(Timestamp::now());
}
printf("front: %s\n", stamps.front().toString().c_str());
printf("back: %s\n", stamps.back().toString().c_str());
printf("back-front: %f\n", timeDifference(stamps.back(), stamps.front()));
int increments[100] = { 0 };
int64_t start = stamps.front().microSecondsSinceEpoch();
for (int i = 1; i < kNumber; ++i)
{
int64_t next = stamps[i].microSecondsSinceEpoch();
int64_t inc = next - start;
start = next;
if (inc < 0)
{
printf("reverse!\n");
}
else if (inc < 100)
{
++increments[inc];
}
else
{
printf("big gap %d\n", static_cast<int>(inc));
}
}
for (int i = 0; i < 100; ++i)
{
printf("%2d: %d\n", i, increments[i]);
}
}
int main()
{
Timestamp now(Timestamp::now());
printf("%s\n", now.toString().c_str());
passByValue(now);
passByConstReference(now);
benchmark();
}
相關文章
- muduo網路庫Exception異常類Exception
- muduo網路庫AtomicIntegerT原子整數類
- muduo網路庫編譯安裝編譯
- muduo原始碼解析11-logger類原始碼
- muduo
- [網際網路]網際網路公司的種類
- mysql資料庫date 、datetime、time、timestamp區別MySql資料庫
- Oracle TimestampOracle
- B類網路快速 子網劃分
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated.explicit_defaults_for_timestamp
- TIMESTAMP和TIMESTAMP WITH TIME ZONE之間的總結
- partitioned by timestamp datatype
- Linux網路-Yum倉庫Linux
- 網路:IP地址分類和分段
- 網路流量抓包庫 gopacketGo
- JavaScript event.timeStampJavaScript
- MySQL: datetime vs timestampMySql
- 7.71 CURRENT_TIMESTAMP
- 網路組網七類水晶頭製作方法,七類網線製作過程
- 計算機網路(們)和 Internet (計算機網路的子類)計算機網路
- 「影像分類」 實戰影像分類網路的視覺化視覺化
- MySQL 資料庫之網際網路常用分庫分表方案MySql資料庫
- DeepMind圖網路庫開源了!
- 圖神經網路GNN 庫,液體神經網路LNN/LFM神經網路GNN
- [20190531]Timestamp Oddity.txt
- 網際網路資料庫架構設計資料庫架構
- 神經網路實現鳶尾花分類神經網路
- Web類別(小迪網路安全筆記~Web筆記
- 網路安全滲透測試分為哪幾類?網路安全好就業嗎?就業
- 網路安全漏洞的種類分為哪些?
- Debian、kali類虛擬機器網路設定虛擬機
- 計算機網路 兩類密碼體制計算機網路密碼
- ×tamp變成×tamp問題
- MySQL中的CURRENT_TIMESTAMPMySql
- mysql timestamp比較查詢MySql
- ON UPDATE CURRENT_TIMESTAMP請慎用
- mysql 資料型別TIMESTAMPMySQL 資料型別
- mysql資料庫時間型別datetime、bigint、timestamp的查詢效率比較MySql資料庫型別