NebulaGraph CPP Client  release-3.8
Duration.h
1 /* Copyright (c) 2021 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 <sstream>
9 
10 #include "common/time/Constants.h"
11 
12 namespace nebula {
13 
14 // Duration equals to months + seconds + microseconds
15 // The base between months and days is not fixed, so we store years and months
16 // separately.
17 struct Duration {
18  // day + hours + minutes + seconds + microseconds
19  int64_t seconds;
20  int32_t microseconds;
21  // years + months
22  int32_t months;
23 
24  Duration() : seconds(0), microseconds(0), months(0) {}
25  Duration(int32_t m, int64_t s, int32_t us) : seconds(s), microseconds(us), months(m) {}
26 
27  int64_t years() const {
28  return months / 12;
29  }
30 
31  int64_t monthsInYear() const {
32  return months % 12;
33  }
34 
35  int64_t days() const {
36  return seconds / time::kSecondsOfDay;
37  }
38 
39  int64_t hours() const {
40  return seconds % time::kSecondsOfDay / time::kSecondsOfHour;
41  }
42 
43  int64_t minutes() const {
44  return seconds % time::kSecondsOfHour / time::kSecondsOfMinute;
45  }
46 
47  int64_t secondsInMinute() const {
48  return seconds % time::kSecondsOfMinute;
49  }
50 
51  int64_t microsecondsInSecond() const {
52  return microseconds;
53  }
54 
55  Duration operator-() const {
56  return Duration(-months, -seconds, -microseconds);
57  }
58 
59  Duration operator+(const Duration& rhs) const {
60  return Duration(months + rhs.months, seconds + rhs.seconds, microseconds + rhs.microseconds);
61  }
62 
63  Duration operator-(const Duration& rhs) const {
64  return Duration(months - rhs.months, seconds - rhs.seconds, microseconds - rhs.microseconds);
65  }
66 
67  Duration& addYears(int32_t y) {
68  months += y * 12;
69  return *this;
70  }
71 
72  Duration& addQuarters(int32_t q) {
73  months += q * 3;
74  return *this;
75  }
76 
77  Duration& addMonths(int32_t m) {
78  months += m;
79  return *this;
80  }
81 
82  Duration& addWeeks(int32_t w) {
83  seconds += (w * 7 * time::kSecondsOfDay);
84  return *this;
85  }
86 
87  Duration& addDays(int64_t d) {
88  seconds += d * time::kSecondsOfDay;
89  return *this;
90  }
91 
92  Duration& addHours(int64_t h) {
93  seconds += h * time::kSecondsOfHour;
94  return *this;
95  }
96 
97  Duration& addMinutes(int64_t minutes) {
98  seconds += minutes * time::kSecondsOfMinute;
99  return *this;
100  }
101 
102  Duration& addSeconds(int64_t s) {
103  seconds += s;
104  return *this;
105  }
106 
107  Duration& addMilliseconds(int64_t ms) {
108  seconds += ms / 1000;
109  microseconds += ((ms % 1000) * 1000);
110  return *this;
111  }
112 
113  Duration& addMicroseconds(int32_t us) {
114  microseconds += us;
115  return *this;
116  }
117 
118  // can't compare
119  bool operator<(const Duration& rhs) const = delete;
120 
121  bool operator==(const Duration& rhs) const {
122  return months == rhs.months && seconds == rhs.seconds && microseconds == rhs.microseconds;
123  }
124 
125  std::string toString() const;
126 };
127 
128 } // namespace nebula
129 
130 namespace std {
131 
132 // Inject a customized hash function
133 template <>
134 struct hash<nebula::Duration> {
135  std::size_t operator()(const nebula::Duration& d) const noexcept;
136 };
137 
138 } // namespace std