Kudu C++ client API
 All Classes Namespaces 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 #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  explicit MonoDelta(int64_t delta);
135  int64_t nano_delta_;
136 };
137 
145 class KUDU_EXPORT MonoTime {
146  public:
150  static const int64_t kNanosecondsPerSecond = 1000000000L;
151  static const int64_t kNanosecondsPerMillisecond = 1000000L;
152  static const int64_t kNanosecondsPerMicrosecond = 1000L;
153 
154  static const int64_t kMicrosecondsPerSecond = 1000000L;
156 
160  static MonoTime Now();
161 
163  static MonoTime Max();
164 
166  static MonoTime Min();
167 
175  static const MonoTime& Earliest(const MonoTime& a, const MonoTime& b);
176 
179  MonoTime();
180 
182  bool Initialized() const;
183 
191  MonoDelta GetDeltaSince(const MonoTime &rhs) const;
192 
197  void AddDelta(const MonoDelta &delta);
198 
206  bool ComesBefore(const MonoTime &rhs) const;
207 
209  std::string ToString() const;
210 
217  bool Equals(const MonoTime& other) const;
218 
219  private:
220  friend class MonoDelta;
221  FRIEND_TEST(TestMonoTime, TestTimeSpec);
222  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
223 
224  explicit MonoTime(const struct timespec &ts);
225  explicit MonoTime(int64_t nanos);
226  double ToSeconds() const;
227  int64_t nanos_;
228 };
229 
240 void KUDU_EXPORT SleepFor(const MonoDelta& delta);
241 
242 } // namespace kudu
243 
244 #endif
void KUDU_EXPORT SleepFor(const MonoDelta &delta)
Representation of a particular point in time.
Definition: monotime.h:145
A representation of a time interval.
Definition: monotime.h:43