NebulaGraph CPP Client  release-3.4
Date.h
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 #pragma once
7 
8 #include <string>
9 
10 namespace nebula {
11 
12 // In nebula only store UTC time, and the interpretion of time value based on
13 // the timezone configuration in current system.
14 
15 extern const int64_t kDaysSoFar[];
16 extern const int64_t kLeapDaysSoFar[];
17 
18 struct Date {
19  int16_t year; // Any integer
20  int8_t month; // 1 - 12
21  int8_t day; // 1 - 31
22 
23  Date() : year{0}, month{1}, day{1} {}
24  Date(int16_t y, int8_t m, int8_t d) : year{y}, month{m}, day{d} {}
25  // Tak the number of days since -32768/1/1, and convert to the real date
26  explicit Date(uint64_t days);
27 
28  void clear() {
29  year = 0;
30  month = 1;
31  day = 1;
32  }
33 
34  void __clear() {
35  clear();
36  }
37 
38  void reset(int16_t y, int8_t m, int8_t d) {
39  year = y;
40  month = m;
41  day = d;
42  }
43 
44  bool operator==(const Date& rhs) const {
45  return year == rhs.year && month == rhs.month && day == rhs.day;
46  }
47 
48  bool operator<(const Date& rhs) const {
49  if (!(year == rhs.year)) {
50  return year < rhs.year;
51  }
52  if (!(month == rhs.month)) {
53  return month < rhs.month;
54  }
55  if (!(day == rhs.day)) {
56  return day < rhs.day;
57  }
58  return false;
59  }
60 
61  Date operator+(int64_t days) const;
62  Date operator-(int64_t days) const;
63 
64  std::string toString() const;
65 
66  // Return the number of days since -32768/1/1
67  int64_t toInt() const;
68  // Convert the number of days since -32768/1/1 to the real date
69  void fromInt(int64_t days);
70 };
71 
72 inline std::ostream& operator<<(std::ostream& os, const Date& d) {
73  os << d.toString();
74  return os;
75 }
76 
77 struct Time {
78  int8_t hour;
79  int8_t minute;
80  int8_t sec;
81  int32_t microsec;
82 
83  Time() : hour{0}, minute{0}, sec{0}, microsec{0} {}
84  Time(int8_t h, int8_t min, int8_t s, int32_t us) : hour{h}, minute{min}, sec{s}, microsec{us} {}
85 
86  void clear() {
87  hour = 0;
88  minute = 0;
89  sec = 0;
90  microsec = 0;
91  }
92 
93  void __clear() {
94  clear();
95  }
96 
97  bool operator==(const Time& rhs) const {
98  return hour == rhs.hour && minute == rhs.minute && sec == rhs.sec && microsec == rhs.microsec;
99  }
100 
101  bool operator<(const Time& rhs) const {
102  if (!(hour == rhs.hour)) {
103  return hour < rhs.hour;
104  }
105  if (!(minute == rhs.minute)) {
106  return minute < rhs.minute;
107  }
108  if (!(sec == rhs.sec)) {
109  return sec < rhs.sec;
110  }
111  if (!(microsec == rhs.microsec)) {
112  return microsec < rhs.microsec;
113  }
114  return false;
115  }
116 
117  std::string toString() const;
118 };
119 
120 inline std::ostream& operator<<(std::ostream& os, const Time& d) {
121  os << d.toString();
122  return os;
123 }
124 
125 struct DateTime {
126 #if defined(__GNUC__)
127 #pragma GCC diagnostic push
128 #pragma GCC diagnostic ignored "-Wpedantic"
129 #endif // defined(__GNUC__)
130  union {
131  struct {
132  int64_t year : 16;
133  uint64_t month : 4;
134  uint64_t day : 5;
135  uint64_t hour : 5;
136  uint64_t minute : 6;
137  uint64_t sec : 6;
138  uint64_t microsec : 22;
139  };
140  uint64_t qword;
141  };
142 #if defined(__GNUC__)
143 #pragma GCC diagnostic pop
144 #endif // defined(__GNUC__)
145 
146  DateTime() : year{0}, month{1}, day{1}, hour{0}, minute{0}, sec{0}, microsec{0} {}
147  DateTime(int16_t y, int8_t m, int8_t d, int8_t h, int8_t min, int8_t s, int32_t us) {
148  year = y;
149  month = m;
150  day = d;
151  hour = h;
152  minute = min;
153  sec = s;
154  microsec = us;
155  }
156  explicit DateTime(const Date& date) {
157  year = date.year;
158  month = date.month;
159  day = date.day;
160  hour = 0;
161  minute = 0;
162  sec = 0;
163  microsec = 0;
164  }
165 
166  void clear() {
167  year = 0;
168  month = 1;
169  day = 1;
170  hour = 0;
171  minute = 0;
172  sec = 0;
173  microsec = 0;
174  }
175 
176  void __clear() {
177  clear();
178  }
179 
180  bool operator==(const DateTime& rhs) const {
181  return year == rhs.year && month == rhs.month && day == rhs.day && hour == rhs.hour &&
182  minute == rhs.minute && sec == rhs.sec && microsec == rhs.microsec;
183  }
184  bool operator<(const DateTime& rhs) const {
185  if (!(year == rhs.year)) {
186  return year < rhs.year;
187  }
188  if (!(month == rhs.month)) {
189  return month < rhs.month;
190  }
191  if (!(day == rhs.day)) {
192  return day < rhs.day;
193  }
194  if (!(hour == rhs.hour)) {
195  return hour < rhs.hour;
196  }
197  if (!(minute == rhs.minute)) {
198  return minute < rhs.minute;
199  }
200  if (!(sec == rhs.sec)) {
201  return sec < rhs.sec;
202  }
203  if (!(microsec == rhs.microsec)) {
204  return microsec < rhs.microsec;
205  }
206  return false;
207  }
208 
209  std::string toString() const;
210 };
211 
212 inline std::ostream& operator<<(std::ostream& os, const DateTime& d) {
213  os << d.toString();
214  return os;
215 }
216 
217 } // namespace nebula
218 
219 namespace std {
220 
221 // Inject a customized hash function
222 template <>
223 struct hash<nebula::Date> {
224  std::size_t operator()(const nebula::Date& h) const noexcept;
225 };
226 
227 template <>
228 struct hash<nebula::Time> {
229  std::size_t operator()(const nebula::Time& h) const noexcept;
230 };
231 
232 template <>
233 struct hash<nebula::DateTime> {
234  std::size_t operator()(const nebula::DateTime& h) const noexcept;
235 };
236 
237 } // namespace std