Kudu C++ client API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 struct timeval; // IWYU pragma: keep
39 
40 namespace kudu {
41 
46 class KUDU_EXPORT MonoDelta {
47  public:
56  static MonoDelta FromSeconds(double seconds);
57  static MonoDelta FromMilliseconds(int64_t ms);
58  static MonoDelta FromMicroseconds(int64_t us);
59  static MonoDelta FromNanoseconds(int64_t ns);
61 
66  MonoDelta();
67 
69  bool Initialized() const;
70 
77  bool LessThan(const MonoDelta &rhs) const;
78 
85  bool MoreThan(const MonoDelta &rhs) const;
86 
94  bool Equals(const MonoDelta &rhs) const;
95 
97  std::string ToString() const;
98 
104  double ToSeconds() const;
105  int64_t ToMilliseconds() const;
106  int64_t ToMicroseconds() const;
107  int64_t ToNanoseconds() const;
109 
115  void ToTimeVal(struct timeval *tv) const;
116 
122  void ToTimeSpec(struct timespec *ts) const;
123 
130  static void NanosToTimeSpec(int64_t nanos, struct timespec* ts);
131 
132  private:
133  static const int64_t kUninitialized;
134 
135  friend class MonoTime;
136  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
137 
138  explicit MonoDelta(int64_t delta);
139  int64_t nano_delta_;
140 };
141 
149 class KUDU_EXPORT MonoTime {
150  public:
154  static const int64_t kNanosecondsPerSecond = 1000000000L;
155  static const int64_t kNanosecondsPerMillisecond = 1000000L;
156  static const int64_t kNanosecondsPerMicrosecond = 1000L;
157 
158  static const int64_t kMicrosecondsPerSecond = 1000000L;
160 
164  static MonoTime Now();
165 
167  static MonoTime Max();
168 
170  static MonoTime Min();
171 
179  static const MonoTime& Earliest(const MonoTime& a, const MonoTime& b)
180  ATTRIBUTE_DEPRECATED("use std::min() instead");
181 
184  MonoTime();
185 
187  bool Initialized() const;
188 
196  MonoDelta GetDeltaSince(const MonoTime &rhs) const;
197 
202  void AddDelta(const MonoDelta &delta);
203 
211  bool ComesBefore(const MonoTime &rhs) const;
212 
214  std::string ToString() const;
215 
222  bool Equals(const MonoTime& other) const;
223 
232  MonoTime& operator+=(const MonoDelta& delta);
233 
239  MonoTime& operator-=(const MonoDelta& delta);
241 
242  private:
243  friend class MonoDelta;
244  FRIEND_TEST(TestMonoTime, TestTimeSpec);
245  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
246 
247  explicit MonoTime(const struct timespec &ts);
248  explicit MonoTime(int64_t nanos);
249  double ToSeconds() const;
250  int64_t nanos_;
251 };
252 
263 void KUDU_EXPORT SleepFor(const MonoDelta& delta);
264 
274 bool KUDU_EXPORT operator==(const MonoDelta &lhs, const MonoDelta &rhs);
275 
282 bool KUDU_EXPORT operator!=(const MonoDelta &lhs, const MonoDelta &rhs);
283 
290 bool KUDU_EXPORT operator<(const MonoDelta &lhs, const MonoDelta &rhs);
291 
298 bool KUDU_EXPORT operator<=(const MonoDelta &lhs, const MonoDelta &rhs);
299 
306 bool KUDU_EXPORT operator>(const MonoDelta &lhs, const MonoDelta &rhs);
307 
314 bool KUDU_EXPORT operator>=(const MonoDelta &lhs, const MonoDelta &rhs);
316 
330 bool KUDU_EXPORT operator==(const MonoTime& lhs, const MonoTime& rhs);
331 
343 bool KUDU_EXPORT operator!=(const MonoTime& lhs, const MonoTime& rhs);
344 
351 bool KUDU_EXPORT operator<(const MonoTime& lhs, const MonoTime& rhs);
352 
359 bool KUDU_EXPORT operator<=(const MonoTime& lhs, const MonoTime& rhs);
360 
367 bool KUDU_EXPORT operator>(const MonoTime& lhs, const MonoTime& rhs);
368 
375 bool KUDU_EXPORT operator>=(const MonoTime& lhs, const MonoTime& rhs);
377 
388 MonoTime KUDU_EXPORT operator+(const MonoTime& t, const MonoDelta& delta);
389 
397 MonoTime KUDU_EXPORT operator-(const MonoTime& t, const MonoDelta& delta);
398 
411 MonoDelta KUDU_EXPORT operator-(const MonoTime& t_end, const MonoTime& t_begin);
413 
414 } // namespace kudu
415 
416 #endif
Representation of a particular point in time.
Definition: monotime.h:149
A representation of a time interval.
Definition: monotime.h:46