/src/muduo/muduo/base/Date.h
Line | Count | Source |
1 | | // Use of this source code is governed by a BSD-style license |
2 | | // that can be found in the License file. |
3 | | // |
4 | | // Author: Shuo Chen (chenshuo at chenshuo dot com) |
5 | | |
6 | | #ifndef MUDUO_BASE_DATE_H |
7 | | #define MUDUO_BASE_DATE_H |
8 | | |
9 | | #include "muduo/base/copyable.h" |
10 | | #include "muduo/base/Types.h" |
11 | | |
12 | | struct tm; |
13 | | |
14 | | namespace muduo |
15 | | { |
16 | | |
17 | | /// |
18 | | /// Date in Gregorian calendar. |
19 | | /// |
20 | | /// This class is immutable. |
21 | | /// It's recommended to pass it by value, since it's passed in register on x64. |
22 | | /// |
23 | | class Date : public muduo::copyable |
24 | | // public boost::less_than_comparable<Date>, |
25 | | // public boost::equality_comparable<Date> |
26 | | { |
27 | | public: |
28 | | |
29 | | struct YearMonthDay |
30 | | { |
31 | | int year; // [1900..2500] |
32 | | int month; // [1..12] |
33 | | int day; // [1..31] |
34 | | }; |
35 | | |
36 | | static const int kDaysPerWeek = 7; |
37 | | static const int kJulianDayOf1970_01_01; |
38 | | |
39 | | /// |
40 | | /// Constucts an invalid Date. |
41 | | /// |
42 | | Date() |
43 | | : julianDayNumber_(0) |
44 | 0 | {} |
45 | | |
46 | | /// |
47 | | /// Constucts a yyyy-mm-dd Date. |
48 | | /// |
49 | | /// 1 <= month <= 12 |
50 | | Date(int year, int month, int day); |
51 | | |
52 | | /// |
53 | | /// Constucts a Date from Julian Day Number. |
54 | | /// |
55 | | explicit Date(int julianDayNum) |
56 | 0 | : julianDayNumber_(julianDayNum) |
57 | 0 | {} |
58 | | |
59 | | /// |
60 | | /// Constucts a Date from struct tm |
61 | | /// |
62 | | explicit Date(const struct tm&); |
63 | | |
64 | | // default copy/assignment/dtor are Okay |
65 | | |
66 | | void swap(Date& that) |
67 | 0 | { |
68 | 0 | std::swap(julianDayNumber_, that.julianDayNumber_); |
69 | 0 | } |
70 | | |
71 | 0 | bool valid() const { return julianDayNumber_ > 0; } |
72 | | |
73 | | /// |
74 | | /// Converts to yyyy-mm-dd format. |
75 | | /// |
76 | | string toIsoString() const; |
77 | | |
78 | | struct YearMonthDay yearMonthDay() const; |
79 | | |
80 | | int year() const |
81 | 0 | { |
82 | 0 | return yearMonthDay().year; |
83 | 0 | } |
84 | | |
85 | | int month() const |
86 | 0 | { |
87 | 0 | return yearMonthDay().month; |
88 | 0 | } |
89 | | |
90 | | int day() const |
91 | 0 | { |
92 | 0 | return yearMonthDay().day; |
93 | 0 | } |
94 | | |
95 | | // [0, 1, ..., 6] => [Sunday, Monday, ..., Saturday ] |
96 | | int weekDay() const |
97 | 0 | { |
98 | 0 | return (julianDayNumber_+1) % kDaysPerWeek; |
99 | 0 | } |
100 | | |
101 | 0 | int julianDayNumber() const { return julianDayNumber_; } |
102 | | |
103 | | private: |
104 | | int julianDayNumber_; |
105 | | }; |
106 | | |
107 | | inline bool operator<(Date x, Date y) |
108 | 0 | { |
109 | 0 | return x.julianDayNumber() < y.julianDayNumber(); |
110 | 0 | } |
111 | | |
112 | | inline bool operator==(Date x, Date y) |
113 | 0 | { |
114 | 0 | return x.julianDayNumber() == y.julianDayNumber(); |
115 | 0 | } |
116 | | |
117 | | } // namespace muduo |
118 | | |
119 | | #endif // MUDUO_BASE_DATE_H |