Kudu C++ client API
monotime.h
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 #ifndef KUDU_UTIL_MONOTIME_H
18 #define KUDU_UTIL_MONOTIME_H
19 
20 #include <stdint.h>
21 #include <string>
22 
23 #ifdef KUDU_HEADERS_NO_STUBS
24 #include <gtest/gtest_prod.h>
25 #else
26 // This is a poor module interdependency, but the stubs are header-only and
27 // it's only for exported header builds, so we'll make an exception.
28 #include "kudu/client/stubs.h"
29 #endif
30 
31 #include "kudu/util/kudu_export.h"
32 
33 struct timeval;
34 struct timespec;
35 
36 namespace kudu {
37 class MonoTime;
38 
43 class KUDU_EXPORT MonoDelta {
44  public:
53  static MonoDelta FromSeconds(double seconds);
54  static MonoDelta FromMilliseconds(int64_t ms);
55  static MonoDelta FromMicroseconds(int64_t us);
56  static MonoDelta FromNanoseconds(int64_t ns);
58 
63  MonoDelta();
64 
66  bool Initialized() const;
67 
74  bool LessThan(const MonoDelta &rhs) const;
75 
82  bool MoreThan(const MonoDelta &rhs) const;
83 
91  bool Equals(const MonoDelta &rhs) const;
92 
94  std::string ToString() const;
95 
101  double ToSeconds() const;
102  int64_t ToMilliseconds() const;
103  int64_t ToMicroseconds() const;
104  int64_t ToNanoseconds() const;
106 
112  void ToTimeVal(struct timeval *tv) const;
113 
119  void ToTimeSpec(struct timespec *ts) const;
120 
127  static void NanosToTimeSpec(int64_t nanos, struct timespec* ts);
128 
129  private:
130  static const int64_t kUninitialized;
131 
132  friend class MonoTime;
133  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
134 
135  explicit MonoDelta(int64_t delta);
136  int64_t nano_delta_;
137 };
138 
146 class KUDU_EXPORT MonoTime {
147  public:
151  static const int64_t kNanosecondsPerSecond = 1000000000L;
152  static const int64_t kNanosecondsPerMillisecond = 1000000L;
153  static const int64_t kNanosecondsPerMicrosecond = 1000L;
154 
155  static const int64_t kMicrosecondsPerSecond = 1000000L;
157 
161  static MonoTime Now();
162 
164  static MonoTime Max();
165 
167  static MonoTime Min();
168 
176  static const MonoTime& Earliest(const MonoTime& a, const MonoTime& b);
177 
180  MonoTime();
181 
183  bool Initialized() const;
184 
192  MonoDelta GetDeltaSince(const MonoTime &rhs) const;
193 
198  void AddDelta(const MonoDelta &delta);
199 
207  bool ComesBefore(const MonoTime &rhs) const;
208 
210  std::string ToString() const;
211 
218  bool Equals(const MonoTime& other) const;
219 
228  MonoTime& operator+=(const MonoDelta& delta);
229 
235  MonoTime& operator-=(const MonoDelta& delta);
237 
238  private:
239  friend class MonoDelta;
240  FRIEND_TEST(TestMonoTime, TestTimeSpec);
241  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
242 
243  explicit MonoTime(const struct timespec &ts);
244  explicit MonoTime(int64_t nanos);
245  double ToSeconds() const;
246  int64_t nanos_;
247 };
248 
259 void KUDU_EXPORT SleepFor(const MonoDelta& delta);
260 
270 bool KUDU_EXPORT operator==(const MonoDelta &lhs, const MonoDelta &rhs);
271 
278 bool KUDU_EXPORT operator!=(const MonoDelta &lhs, const MonoDelta &rhs);
279 
286 bool KUDU_EXPORT operator<(const MonoDelta &lhs, const MonoDelta &rhs);
287 
294 bool KUDU_EXPORT operator<=(const MonoDelta &lhs, const MonoDelta &rhs);
295 
302 bool KUDU_EXPORT operator>(const MonoDelta &lhs, const MonoDelta &rhs);
303 
310 bool KUDU_EXPORT operator>=(const MonoDelta &lhs, const MonoDelta &rhs);
312 
326 bool KUDU_EXPORT operator==(const MonoTime& lhs, const MonoTime& rhs);
327 
339 bool KUDU_EXPORT operator!=(const MonoTime& lhs, const MonoTime& rhs);
340 
347 bool KUDU_EXPORT operator<(const MonoTime& lhs, const MonoTime& rhs);
348 
355 bool KUDU_EXPORT operator<=(const MonoTime& lhs, const MonoTime& rhs);
356 
363 bool KUDU_EXPORT operator>(const MonoTime& lhs, const MonoTime& rhs);
364 
371 bool KUDU_EXPORT operator>=(const MonoTime& lhs, const MonoTime& rhs);
373 
384 MonoTime KUDU_EXPORT operator+(const MonoTime& t, const MonoDelta& delta);
385 
393 MonoTime KUDU_EXPORT operator-(const MonoTime& t, const MonoDelta& delta);
394 
407 MonoDelta KUDU_EXPORT operator-(const MonoTime& t_end, const MonoTime& t_begin);
409 
410 } // namespace kudu
411 
412 #endif
Definition: callbacks.h:28
Representation of a particular point in time.
Definition: monotime.h:146
A representation of a time interval.
Definition: monotime.h:43