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 // NOTE: using stdint.h instead of cstdint because this file is supposed
21 // to be processed by a compiler lacking C++11 support.
22 #include <stdint.h>
23 
24 #include <string>
25 
26 #ifdef KUDU_HEADERS_NO_STUBS
27 #include <gtest/gtest_prod.h>
28 
29 #include "kudu/gutil/port.h"
30 #else
31 // This is a poor module interdependency, but the stubs are header-only and
32 // it's only for exported header builds, so we'll make an exception.
33 #include "kudu/client/stubs.h"
34 #endif
35 
36 #include "kudu/util/kudu_export.h"
37 
38 namespace kudu {
39 
44 class KUDU_EXPORT MonoDelta {
45  public:
54  static MonoDelta FromSeconds(double seconds);
55  static MonoDelta FromMilliseconds(int64_t ms);
56  static MonoDelta FromMicroseconds(int64_t us);
57  static MonoDelta FromNanoseconds(int64_t ns);
59 
64  MonoDelta();
65 
67  bool Initialized() const;
68 
75  bool LessThan(const MonoDelta &rhs) const;
76 
83  bool MoreThan(const MonoDelta &rhs) const;
84 
92  bool Equals(const MonoDelta &rhs) const;
93 
95  std::string ToString() const;
96 
102  double ToSeconds() const;
103  int64_t ToMilliseconds() const;
104  int64_t ToMicroseconds() const;
105  int64_t ToNanoseconds() const;
107 
113  void ToTimeVal(struct timeval *tv) const;
114 
120  void ToTimeSpec(struct timespec *ts) const;
121 
128  static void NanosToTimeSpec(int64_t nanos, struct timespec* ts);
129 
130  private:
131  static const int64_t kUninitialized;
132 
133  friend class MonoTime;
134  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
135 
136  explicit MonoDelta(int64_t delta);
137  int64_t nano_delta_;
138 };
139 
147 class KUDU_EXPORT MonoTime {
148  public:
152  static const int64_t kNanosecondsPerSecond = 1000000000L;
153  static const int64_t kNanosecondsPerMillisecond = 1000000L;
154  static const int64_t kNanosecondsPerMicrosecond = 1000L;
155 
156  static const int64_t kMicrosecondsPerSecond = 1000000L;
158 
162  static MonoTime Now();
163 
165  static MonoTime Max();
166 
168  static MonoTime Min();
169 
177  static const MonoTime& Earliest(const MonoTime& a, const MonoTime& b)
178  ATTRIBUTE_DEPRECATED("use std::min() instead");
179 
182  MonoTime();
183 
185  bool Initialized() const;
186 
194  MonoDelta GetDeltaSince(const MonoTime &rhs) const;
195 
200  void AddDelta(const MonoDelta &delta);
201 
209  bool ComesBefore(const MonoTime &rhs) const;
210 
212  std::string ToString() const;
213 
219  void ToTimeSpec(struct timespec* ts) const;
220 
227  bool Equals(const MonoTime& other) const;
228 
237  MonoTime& operator+=(const MonoDelta& delta);
238 
244  MonoTime& operator-=(const MonoDelta& delta);
246 
247  private:
248  friend class MonoDelta;
249  FRIEND_TEST(TestMonoTime, TestTimeSpec);
250  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
251 
252  explicit MonoTime(const struct timespec &ts);
253  explicit MonoTime(int64_t nanos);
254  double ToSeconds() const;
255  int64_t nanos_;
256 };
257 
268 void KUDU_EXPORT SleepFor(const MonoDelta& delta);
269 
279 bool KUDU_EXPORT operator==(const MonoDelta &lhs, const MonoDelta &rhs);
280 
287 bool KUDU_EXPORT operator!=(const MonoDelta &lhs, const MonoDelta &rhs);
288 
295 bool KUDU_EXPORT operator<(const MonoDelta &lhs, const MonoDelta &rhs);
296 
303 bool KUDU_EXPORT operator<=(const MonoDelta &lhs, const MonoDelta &rhs);
304 
311 bool KUDU_EXPORT operator>(const MonoDelta &lhs, const MonoDelta &rhs);
312 
319 bool KUDU_EXPORT operator>=(const MonoDelta &lhs, const MonoDelta &rhs);
321 
335 bool KUDU_EXPORT operator==(const MonoTime& lhs, const MonoTime& rhs);
336 
348 bool KUDU_EXPORT operator!=(const MonoTime& lhs, const MonoTime& rhs);
349 
356 bool KUDU_EXPORT operator<(const MonoTime& lhs, const MonoTime& rhs);
357 
364 bool KUDU_EXPORT operator<=(const MonoTime& lhs, const MonoTime& rhs);
365 
372 bool KUDU_EXPORT operator>(const MonoTime& lhs, const MonoTime& rhs);
373 
380 bool KUDU_EXPORT operator>=(const MonoTime& lhs, const MonoTime& rhs);
382 
393 MonoTime KUDU_EXPORT operator+(const MonoTime& t, const MonoDelta& delta);
394 
402 MonoTime KUDU_EXPORT operator-(const MonoTime& t, const MonoDelta& delta);
403 
416 MonoDelta KUDU_EXPORT operator-(const MonoTime& t_end, const MonoTime& t_begin);
418 
419 } // namespace kudu
420 
421 #endif
Definition: callbacks.h:28
Representation of a particular point in time.
Definition: monotime.h:147
A representation of a time interval.
Definition: monotime.h:44