Getting Started 06 Network Programming

muduo base module is implemented on most of the thread have read, take a look at the key features of the timing

Date.h

(TM) {struct
int tm_sec; / sec * - value interval [0,59] /
int tm_min; /
min - value interval [0,59] /
int tm_hour; /
when - the value interval [0,23 ] /
int tm_mday; /
month date - the value interval [1, 31] /
int tm_mon; /
month (from the beginning of January, 0 for January) - the value range is [0,11] /
int tm_year; /
year, equal to the actual value minus the year 1900 /
int tm_wday; /
week - the value interval [0,6], where 0 represents Sunday, a Monday, and so on /
int tm_yday; /
each year from number of days since January 1 - the value for the interval [0,365], where 0 represents January 1, January 2 represents, so /
int the tm_isdst; /
daylight saving time identifier, when daylight saving time , tm_isdst is positive. When not implement daylight saving time, tm_isdst 0; when uninformed, tm_isdst () is negative.
long int tm_gmtoff; / specifies the number of seconds the negative zone east of the date line in the Eastern time zone UTC positive number of seconds or UTC time zones west/
Const char tm_zone; / name of the current time zone (TZ environment variable and about) * /
};

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 // somewhat similar SSDB in the bytes, leveldb :: slice is essentially a string

A package of the string itself

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) { }

Guess you like

Origin www.cnblogs.com/aiqingyi/p/11316752.html