src/core/lib/gprpp/time.h
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_CORE_LIB_GPRPP_TIME_H
16 #define GRPC_CORE_LIB_GPRPP_TIME_H
17 
19 
20 #include <stdint.h>
21 
22 #include <limits>
23 #include <ostream>
24 #include <string>
25 
28 #include <grpc/support/time.h>
29 
32 
33 namespace grpc_core {
34 
35 namespace time_detail {
36 
41  }
45  }
46  return SaturatingAdd(a, b);
47 }
48 
49 constexpr inline int64_t MillisMul(int64_t millis, int64_t mul) {
50  return millis >= std::numeric_limits<int64_t>::max() / mul
54  : millis * mul;
55 }
56 
57 } // namespace time_detail
58 
59 class Duration;
60 
61 // Timestamp represents a discrete point in time.
62 class Timestamp {
63  public:
64  constexpr Timestamp() = default;
65  // Constructs a Timestamp from a gpr_timespec.
68 
69  // Construct a Timestamp from a gpr_cycle_counter.
70  static Timestamp FromCycleCounterRoundUp(gpr_cycle_counter c);
71  static Timestamp FromCycleCounterRoundDown(gpr_cycle_counter c);
72 
74  return Timestamp(millis);
75  }
76 
77  static constexpr Timestamp ProcessEpoch() { return Timestamp(0); }
78 
79  static constexpr Timestamp InfFuture() {
81  }
82 
83  static constexpr Timestamp InfPast() {
85  }
86 
87  constexpr bool operator==(Timestamp other) const {
88  return millis_ == other.millis_;
89  }
90  constexpr bool operator!=(Timestamp other) const {
91  return millis_ != other.millis_;
92  }
93  constexpr bool operator<(Timestamp other) const {
94  return millis_ < other.millis_;
95  }
96  constexpr bool operator<=(Timestamp other) const {
97  return millis_ <= other.millis_;
98  }
99  constexpr bool operator>(Timestamp other) const {
100  return millis_ > other.millis_;
101  }
102  constexpr bool operator>=(Timestamp other) const {
103  return millis_ >= other.millis_;
104  }
105  Timestamp& operator+=(Duration duration);
106 
107  bool is_process_epoch() const { return millis_ == 0; }
108 
110 
112 
113  std::string ToString() const;
114 
115  private:
116  explicit constexpr Timestamp(int64_t millis) : millis_(millis) {}
117 
119 };
120 
121 // Duration represents a span of time.
122 class Duration {
123  public:
124  constexpr Duration() noexcept : millis_(0) {}
125 
128  static Duration FromSecondsAsDouble(double seconds);
129 
130  static constexpr Duration Zero() { return Duration(0); }
131 
132  // Smallest representatable positive duration.
133  static constexpr Duration Epsilon() { return Duration(1); }
134 
135  static constexpr Duration NegativeInfinity() {
137  }
138 
139  static constexpr Duration Infinity() {
141  }
142 
143  static constexpr Duration Hours(int64_t hours) {
144  return Minutes(time_detail::MillisMul(hours, 60));
145  }
146 
147  static constexpr Duration Minutes(int64_t minutes) {
148  return Seconds(time_detail::MillisMul(minutes, 60));
149  }
150 
151  static constexpr Duration Seconds(int64_t seconds) {
153  }
154 
155  static constexpr Duration Milliseconds(int64_t millis) {
156  return Duration(millis);
157  }
158 
159  static constexpr Duration MicrosecondsRoundDown(int64_t micros) {
160  return Duration(micros / GPR_US_PER_MS);
161  }
162 
163  static constexpr Duration NanosecondsRoundDown(int64_t nanos) {
164  return Duration(nanos / GPR_NS_PER_MS);
165  }
166 
167  static constexpr Duration MicrosecondsRoundUp(int64_t micros) {
168  return Duration(micros / GPR_US_PER_MS + (micros % GPR_US_PER_MS != 0));
169  }
170 
171  static constexpr Duration NanosecondsRoundUp(int64_t nanos) {
172  return Duration(nanos / GPR_NS_PER_MS + (nanos % GPR_NS_PER_MS != 0));
173  }
174 
175  constexpr bool operator==(Duration other) const {
176  return millis_ == other.millis_;
177  }
178  constexpr bool operator!=(Duration other) const {
179  return millis_ != other.millis_;
180  }
181  constexpr bool operator<(Duration other) const {
182  return millis_ < other.millis_;
183  }
184  constexpr bool operator<=(Duration other) const {
185  return millis_ <= other.millis_;
186  }
187  constexpr bool operator>(Duration other) const {
188  return millis_ > other.millis_;
189  }
190  constexpr bool operator>=(Duration other) const {
191  return millis_ >= other.millis_;
192  }
195  *this = divisor < 0 ? NegativeInfinity() : Infinity();
196  } else if (millis_ == std::numeric_limits<int64_t>::min()) {
197  *this = divisor < 0 ? Infinity() : NegativeInfinity();
198  } else {
199  millis_ /= divisor;
200  }
201  return *this;
202  }
204  millis_ += other.millis_;
205  return *this;
206  }
207 
208  constexpr int64_t millis() const { return millis_; }
209  double seconds() const { return static_cast<double>(millis_) / 1000.0; }
210 
211  // NOLINTNEXTLINE: google-explicit-constructor
213 
214  gpr_timespec as_timespec() const;
215 
216  std::string ToString() const;
217 
218  // Returns the duration in the JSON form corresponding to a
219  // google.protobuf.Duration proto, as defined here:
220  // https://developers.google.com/protocol-buffers/docs/proto3#json
221  std::string ToJsonString() const;
222 
223  private:
224  explicit constexpr Duration(int64_t millis) : millis_(millis) {}
225 
227 };
228 
230  return Duration::Milliseconds(
231  time_detail::MillisAdd(lhs.millis(), rhs.millis()));
232 }
233 
235  return Duration::Milliseconds(
236  time_detail::MillisAdd(lhs.millis(), -rhs.millis()));
237 }
238 
242 }
243 
247 }
248 
249 inline Timestamp operator+(Duration lhs, Timestamp rhs) { return rhs + lhs; }
250 
252  return Duration::Milliseconds(
255 }
256 
257 inline Duration operator*(Duration lhs, double rhs) {
258  if (lhs == Duration::Infinity()) {
259  return rhs < 0 ? Duration::NegativeInfinity() : Duration::Infinity();
260  }
261  if (lhs == Duration::NegativeInfinity()) {
262  return rhs < 0 ? Duration::Infinity() : Duration::NegativeInfinity();
263  }
264  return Duration::FromSecondsAsDouble(lhs.millis() * rhs / 1000.0);
265 }
266 
267 inline Duration operator*(double lhs, Duration rhs) { return rhs * lhs; }
268 
269 inline Duration operator/(Duration lhs, int64_t rhs) {
270  lhs /= rhs;
271  return lhs;
272 }
273 
275  int32_t nanos) {
276  return Seconds(seconds) + NanosecondsRoundDown(nanos);
277 }
278 
280  double millis = seconds * 1000.0;
281  if (millis >= static_cast<double>(std::numeric_limits<int64_t>::max())) {
282  return Infinity();
283  }
284  if (millis <= static_cast<double>(std::numeric_limits<int64_t>::min())) {
285  return NegativeInfinity();
286  }
287  return Milliseconds(static_cast<int64_t>(millis));
288 }
289 
291  return *this = (*this + duration);
292 }
293 
295 
296 std::ostream& operator<<(std::ostream& out, Timestamp timestamp);
297 std::ostream& operator<<(std::ostream& out, Duration duration);
298 
299 } // namespace grpc_core
300 
301 #endif // GRPC_CORE_LIB_GPRPP_TIME_H
grpc_core::Duration::seconds
double seconds() const
Definition: src/core/lib/gprpp/time.h:209
mul
#define mul(r, a, w, c)
Definition: generic.c:110
grpc_core::Duration::Hours
static constexpr Duration Hours(int64_t hours)
Definition: src/core/lib/gprpp/time.h:143
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
grpc_core::Timestamp::operator==
constexpr bool operator==(Timestamp other) const
Definition: src/core/lib/gprpp/time.h:87
grpc_core::Duration::FromSecondsAsDouble
static Duration FromSecondsAsDouble(double seconds)
Definition: src/core/lib/gprpp/time.h:279
grpc_core::operator+
Duration operator+(Duration lhs, Duration rhs)
Definition: src/core/lib/gprpp/time.h:229
grpc_core::TestOnlySetProcessEpoch
void TestOnlySetProcessEpoch(gpr_timespec epoch)
Definition: src/core/lib/gprpp/time.cc:201
grpc_core
Definition: call_metric_recorder.h:31
event_engine.h
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
useful.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_event_engine::experimental::EventEngine::Duration
std::chrono::duration< int64_t, std::nano > Duration
Definition: event_engine.h:80
grpc_core::Duration::NanosecondsRoundDown
static constexpr Duration NanosecondsRoundDown(int64_t nanos)
Definition: src/core/lib/gprpp/time.h:163
grpc_core::Timestamp::InfPast
static constexpr Timestamp InfPast()
Definition: src/core/lib/gprpp/time.h:83
time.h
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc_core::Duration::millis_
int64_t millis_
Definition: src/core/lib/gprpp/time.h:226
grpc_core::Timestamp::operator>
constexpr bool operator>(Timestamp other) const
Definition: src/core/lib/gprpp/time.h:99
grpc_core::Duration::MicrosecondsRoundDown
static constexpr Duration MicrosecondsRoundDown(int64_t micros)
Definition: src/core/lib/gprpp/time.h:159
grpc_core::Duration::operator>
constexpr bool operator>(Duration other) const
Definition: src/core/lib/gprpp/time.h:187
grpc_core::Duration::operator==
constexpr bool operator==(Duration other) const
Definition: src/core/lib/gprpp/time.h:175
grpc_core::Duration::operator<
constexpr bool operator<(Duration other) const
Definition: src/core/lib/gprpp/time.h:181
grpc_core::Timestamp::operator<=
constexpr bool operator<=(Timestamp other) const
Definition: src/core/lib/gprpp/time.h:96
grpc_core::operator-
Duration operator-(Duration lhs, Duration rhs)
Definition: src/core/lib/gprpp/time.h:234
grpc_core::operator<<
std::ostream & operator<<(std::ostream &out, Timestamp timestamp)
Definition: src/core/lib/gprpp/time.cc:206
grpc_core::Duration::ToString
std::string ToString() const
Definition: src/core/lib/gprpp/time.cc:179
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc_core::Timestamp::operator>=
constexpr bool operator>=(Timestamp other) const
Definition: src/core/lib/gprpp/time.h:102
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_core::Duration::Duration
constexpr Duration(int64_t millis)
Definition: src/core/lib/gprpp/time.h:224
grpc_core::Timestamp::milliseconds_after_process_epoch
uint64_t milliseconds_after_process_epoch() const
Definition: src/core/lib/gprpp/time.h:109
grpc_core::Duration::FromSecondsAndNanoseconds
static Duration FromSecondsAndNanoseconds(int64_t seconds, int32_t nanos)
Definition: src/core/lib/gprpp/time.h:274
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
grpc_core::Duration::operator/=
Duration & operator/=(int64_t divisor)
Definition: src/core/lib/gprpp/time.h:193
grpc_core::time_detail::MillisMul
constexpr int64_t MillisMul(int64_t millis, int64_t mul)
Definition: src/core/lib/gprpp/time.h:49
grpc_core::Timestamp::ToString
std::string ToString() const
Definition: src/core/lib/gprpp/time.cc:161
grpc_core::Timestamp::operator+=
Timestamp & operator+=(Duration duration)
Definition: src/core/lib/gprpp/time.h:290
epoch
int64_t epoch(int year, int yday, int hour, int min, int sec)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:10172
grpc_core::Duration::FromTimespec
static Duration FromTimespec(gpr_timespec t)
Definition: src/core/lib/gprpp/time.cc:175
grpc_core::SaturatingAdd
int64_t SaturatingAdd(int64_t a, int64_t b)
Definition: useful.h:112
grpc_core::Duration::operator+=
Duration & operator+=(Duration other)
Definition: src/core/lib/gprpp/time.h:203
grpc_core::Duration::Epsilon
static constexpr Duration Epsilon()
Definition: src/core/lib/gprpp/time.h:133
grpc_core::Duration::ToJsonString
std::string ToJsonString() const
Definition: src/core/lib/gprpp/time.cc:189
min
#define min(a, b)
Definition: qsort.h:83
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_core::Timestamp::ProcessEpoch
static constexpr Timestamp ProcessEpoch()
Definition: src/core/lib/gprpp/time.h:77
grpc_core::Duration::operator!=
constexpr bool operator!=(Duration other) const
Definition: src/core/lib/gprpp/time.h:178
stdint.h
grpc_core::Duration::NegativeInfinity
static constexpr Duration NegativeInfinity()
Definition: src/core/lib/gprpp/time.h:135
grpc_core::Duration::MicrosecondsRoundUp
static constexpr Duration MicrosecondsRoundUp(int64_t micros)
Definition: src/core/lib/gprpp/time.h:167
grpc_core::Timestamp::operator<
constexpr bool operator<(Timestamp other) const
Definition: src/core/lib/gprpp/time.h:93
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch
static constexpr Timestamp FromMillisecondsAfterProcessEpoch(int64_t millis)
Definition: src/core/lib/gprpp/time.h:73
gpr_types.h
GPR_NS_PER_MS
#define GPR_NS_PER_MS
Definition: include/grpc/support/time.h:42
grpc_core::Duration::Zero
static constexpr Duration Zero()
Definition: src/core/lib/gprpp/time.h:130
grpc_core::Duration::operator<=
constexpr bool operator<=(Duration other) const
Definition: src/core/lib/gprpp/time.h:184
grpc_core::Duration::Milliseconds
static constexpr Duration Milliseconds(int64_t millis)
Definition: src/core/lib/gprpp/time.h:155
grpc_core::Duration::millis
constexpr int64_t millis() const
Definition: src/core/lib/gprpp/time.h:208
grpc_core::Duration::operator>=
constexpr bool operator>=(Duration other) const
Definition: src/core/lib/gprpp/time.h:190
grpc_core::operator*
Duration operator*(Duration lhs, double rhs)
Definition: src/core/lib/gprpp/time.h:257
GPR_US_PER_MS
#define GPR_US_PER_MS
Definition: include/grpc/support/time.h:44
grpc_core::Timestamp::Timestamp
constexpr Timestamp()=default
grpc_core::Timestamp::millis_
int64_t millis_
Definition: src/core/lib/gprpp/time.h:118
grpc_core::Timestamp::FromCycleCounterRoundDown
static Timestamp FromCycleCounterRoundDown(gpr_cycle_counter c)
Definition: src/core/lib/gprpp/time.cc:152
grpc_core::Duration::as_timespec
gpr_timespec as_timespec() const
Definition: src/core/lib/gprpp/time.cc:171
grpc_core::Timestamp::FromTimespecRoundDown
static Timestamp FromTimespecRoundDown(gpr_timespec t)
Definition: src/core/lib/gprpp/time.cc:141
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition: src/core/lib/gprpp/time.h:151
grpc_core::Timestamp::operator!=
constexpr bool operator!=(Timestamp other) const
Definition: src/core/lib/gprpp/time.h:90
grpc_core::Timestamp::Timestamp
constexpr Timestamp(int64_t millis)
Definition: src/core/lib/gprpp/time.h:116
grpc_core::Timestamp::FromCycleCounterRoundUp
static Timestamp FromCycleCounterRoundUp(gpr_cycle_counter c)
Definition: src/core/lib/gprpp/time.cc:147
grpc_core::Duration::NanosecondsRoundUp
static constexpr Duration NanosecondsRoundUp(int64_t nanos)
Definition: src/core/lib/gprpp/time.h:171
GPR_MS_PER_SEC
#define GPR_MS_PER_SEC
Definition: include/grpc/support/time.h:39
Duration
Definition: bloaty/third_party/protobuf/src/google/protobuf/duration.pb.h:69
grpc_core::Timestamp::InfFuture
static constexpr Timestamp InfFuture()
Definition: src/core/lib/gprpp/time.h:79
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
gpr_timespec
Definition: gpr_types.h:50
grpc_core::Timestamp::is_process_epoch
bool is_process_epoch() const
Definition: src/core/lib/gprpp/time.h:107
grpc_core::Timestamp::FromTimespecRoundUp
static Timestamp FromTimespecRoundUp(gpr_timespec t)
Definition: src/core/lib/gprpp/time.cc:136
grpc_core::time_detail::MillisAdd
int64_t MillisAdd(int64_t a, int64_t b)
Definition: src/core/lib/gprpp/time.h:37
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
grpc_core::Duration::Duration
constexpr Duration() noexcept
Definition: src/core/lib/gprpp/time.h:124
grpc_core::Timestamp::as_timespec
gpr_timespec as_timespec(gpr_clock_type type) const
Definition: src/core/lib/gprpp/time.cc:157
grpc_core::Duration::Infinity
static constexpr Duration Infinity()
Definition: src/core/lib/gprpp/time.h:139
gpr_clock_type
gpr_clock_type
Definition: gpr_types.h:34
grpc_core::Duration::Minutes
static constexpr Duration Minutes(int64_t minutes)
Definition: src/core/lib/gprpp/time.h:147
time_precise.h
port_platform.h
grpc_core::operator/
Duration operator/(Duration lhs, int64_t rhs)
Definition: src/core/lib/gprpp/time.h:269


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:36