muduo base模块中关于线程大部分都看完了,看看关键计时功能的实现把
Date.h
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] /
int tm_min; / 分 - 取值区间为[0,59] /
int tm_hour; / 时 - 取值区间为[0,23] /
int tm_mday; / 一个月中的日期 - 取值区间为[1,31] /
int tm_mon; / 月份(从一月开始,0代表一月) - 取值区间为[0,11] /
int tm_year; / 年份,其值等于实际年份减去1900 /
int tm_wday; / 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 /
int tm_yday; / 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 /
int tm_isdst; / 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。
long int tm_gmtoff; /指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数/
const char tm_zone; /当前时区的名字(与环境变量TZ有关)*/
};
class Date : noncopyable { //创建了一个date类型,记录的是从1970 01 01到今天的天数
private:
int julianDayNumber_;
public:
struct YearMothDay { //类中的结构
int year;
int month;
int day;
}
static const int kDaysPerWeek = 7;
static const int kJulianDayOf1970_01_01;
Date()
: julianDayNumber_(0)
{}
Date(int year, int month, int day) {
}
explicit Date(int julianDayNum)
: julianDayNumber_(julianDayNum)
{}
explicit Date(const struct tm&) {
}
void swap(Date& that)
{
std::swap(julianDayNumber_, that.julianDayNumber_);
}
bool valid() const { return julianDayNumber_ > 0; }
string toIsoString() const;
int year() const
{
return yearMonthDay().year;
}
int month() const
{
return yearMonthDay().month;
}
int day() const
{
return yearMonthDay().day;
}
int weekDay() const
{
return (julianDayNumber_+1) % kDaysPerWeek;
}
}
inline bool operator<(Date x, Date y)
{
return x.julianDayNumber() < y.julianDayNumber();
}
inline bool operator==(Date x, Date y)
{
return x.julianDayNumber() == y.julianDayNumber();
}
int getJulianDayNumber(int year, int month, int day) //计算天数 认为是一种哈希算法把,不是天数 从日期转换为一个int
{
(void) require_32_bit_integer_at_least; // no warning please
int a = (14 - month) / 12;
int y = year + 4800 - a;
int m = month + 12 * a - 3;
return day + (153*m + 2) / 5 + y*365 + y/4 - y/100 + y/400 - 32045;
}
struct Date::YearMonthDay getYearMonthDay(int julianDayNumber) 从int转换为一个日期
{
int a = julianDayNumber + 32044;
int b = (4 * a + 3) / 146097;
int c = a - ((b * 146097) / 4);
int d = (4 * c + 3) / 1461;
int e = c - ((1461 * d) / 4);
int m = (5 * e + 2) / 153;
Date::YearMonthDay ymd;
ymd.day = e - ((153 * m + 2) / 5) + 1;
ymd.month = m + 3 - 12 * (m / 10);
ymd.year = b * 100 + d - 4800 + (m / 10);
return ymd;
}
Date::Date(const struct tm& t)
: julianDayNumber_(getJulianDayNumber(
t.tm_year+1900,
t.tm_mon+1,
t.tm_mday))
{
}
StringPiece.h //有点类似于SSDB 里的 bytes, leveldb::slice 本质就是字符串
对字符串本身的的一个封装
class StringArg {
private:
const char* str_;
public:
StringArg(const char* str) : str_(str) {
}
StringArg(const string& str) : str_(str.c_str()) {}
const char* c_str() const { return str_;}
}
class StringPiece {
private:
const char* ptr_;
int length_;
public:
StringPiece()
: ptr_(NULL), length_(0) { }
StringPiece(const char* str)
: ptr_(str), length_(static_cast<int>(strlen(ptr_))) { }
StringPiece(const unsigned char* str)
: ptr_(reinterpret_cast<const char*>(str)),
length_(static_cast<int>(strlen(ptr_))) { }
StringPiece(const string& str)
: ptr_(str.data()), length_(static_cast<int>(str.size())) { }
StringPiece(const char* offset, int len)
: ptr_(offset), length_(len) { }