GRPC C++  1.81.0
time.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // 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, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPCPP_SUPPORT_TIME_H
20 #define GRPCPP_SUPPORT_TIME_H
21 
23 #include <grpc/impl/grpc_types.h>
24 #include <grpc/support/time.h>
25 #include <grpcpp/support/config.h>
26 
27 #include <chrono>
28 
29 #include "absl/time/clock.h"
30 #include "absl/time/time.h"
31 
32 namespace grpc {
33 
39 
44 template <typename T>
45 class TimePoint {
46  public:
47  // If you see the error with methods below, you may need either
48  // i) using the existing types having a conversion class such as
49  // gpr_timespec and std::chrono::system_clock::time_point or
50  // ii) writing a new TimePoint<YourType> to address your case.
51  TimePoint(const T& /*time*/) = delete;
52  gpr_timespec raw_time() = delete;
53 };
54 
55 template <>
57  public:
58  // NOLINTNEXTLINE(google-explicit-constructor)
59  TimePoint(const gpr_timespec& time) : time_(time) {}
60  gpr_timespec raw_time() { return time_; }
61 
62  private:
63  gpr_timespec time_;
64 };
65 
66 template <>
67 class TimePoint<absl::Time> {
68  public:
69  explicit TimePoint(absl::Time time) : time_(TimeToGprTimespec(time)) {}
70 
71  gpr_timespec raw_time() const { return time_; }
72 
73  private:
74  static gpr_timespec TimeToGprTimespec(absl::Time time) {
75  if (time == absl::InfiniteFuture()) {
77  }
78  if (time == absl::InfinitePast()) {
80  }
81 
82  gpr_timespec spec;
83  timespec t = absl::ToTimespec(time);
84  spec.tv_sec = t.tv_sec;
85  spec.tv_nsec = static_cast<int32_t>(t.tv_nsec);
87  return spec;
88  }
89 
90  const gpr_timespec time_;
91 };
92 
93 } // namespace grpc
94 
95 namespace grpc {
96 
97 // from and to should be absolute time.
98 void Timepoint2Timespec(const std::chrono::system_clock::time_point& from,
99  gpr_timespec* to);
101  const std::chrono::high_resolution_clock::time_point& from,
102  gpr_timespec* to);
103 
104 std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t);
105 
106 template <>
107 class TimePoint<std::chrono::system_clock::time_point> {
108  public:
109  // NOLINTNEXTLINE(google-explicit-constructor)
110  TimePoint(const std::chrono::system_clock::time_point& time) {
111  Timepoint2Timespec(time, &time_);
112  }
113  gpr_timespec raw_time() const { return time_; }
114 
115  private:
116  gpr_timespec time_;
117 };
118 
119 // Converts gpr timespec to absl::Time
120 absl::Time TimeFromGprTimespec(gpr_timespec time);
121 
122 // Converts absl::Time to gpr timespec with clock type set to REALTIME
124 
125 // Converts gpr timespec to absl::Duration. The gpr timespec clock type
126 // should be TIMESPAN.
127 absl::Duration DurationFromGprTimespec(gpr_timespec time);
128 
129 // Converts absl::Duration to gpr timespec with clock type set to TIMESPAM
130 gpr_timespec GprTimeSpecFromDuration(absl::Duration);
131 
132 // Creates an absolute-time deadline from now + dur.
133 inline gpr_timespec DeadlineFromDuration(absl::Duration dur) {
134  if (absl::time_internal::IsInfiniteDuration(dur)) {
135  if (dur > absl::ZeroDuration()) {
137  } else {
139  }
140  }
141 
142  timespec t = absl::ToTimespec(dur);
143  gpr_timespec span;
144  span.tv_sec = t.tv_sec;
145  span.tv_nsec = static_cast<int32_t>(t.tv_nsec);
146  span.clock_type = GPR_TIMESPAN;
148 }
149 
150 } // namespace grpc
151 
152 #endif // GRPCPP_SUPPORT_TIME_H
gpr_timespec::tv_nsec
int32_t tv_nsec
Definition: time.h:49
gpr_timespec::tv_sec
int64_t tv_sec
Definition: time.h:48
grpc::TimePoint< absl::Time >::TimePoint
TimePoint(absl::Time time)
Definition: time.h:69
gpr_inf_past
GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type)
The far past.
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Monotonic clock.
Definition: time.h:33
grpc::TimePoint< std::chrono::system_clock::time_point >::TimePoint
TimePoint(const std::chrono::system_clock::time_point &time)
Definition: time.h:110
grpc::DeadlineFromDuration
gpr_timespec DeadlineFromDuration(absl::Duration dur)
Definition: time.h:133
grpc::TimePoint< std::chrono::system_clock::time_point >::raw_time
gpr_timespec raw_time() const
Definition: time.h:113
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
The far future.
GPR_TIMESPAN
@ GPR_TIMESPAN
Unmeasurable clock type: no base, created by taking the difference between two times.
Definition: time.h:42
grpc_types.h
grpc::TimePoint< gpr_timespec >::TimePoint
TimePoint(const gpr_timespec &time)
Definition: time.h:59
grpc::GprTimeSpecFromTime
gpr_timespec GprTimeSpecFromTime(absl::Time)
grpc::TimePoint::raw_time
gpr_timespec raw_time()=delete
grpc::TimePoint< absl::Time >::raw_time
gpr_timespec raw_time() const
Definition: time.h:71
grpc::TimeFromGprTimespec
absl::Time TimeFromGprTimespec(gpr_timespec time)
gpr_types.h
gpr_timespec::clock_type
gpr_clock_type clock_type
Against which clock was this time measured? (or GPR_TIMESPAN if this is a relative time measure)
Definition: time.h:52
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Add and subtract times.
config.h
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
Return the current time measured from the given clocks epoch.
std
Definition: async_unary_call.h:410
grpc::Timepoint2Timespec
void Timepoint2Timespec(const std::chrono::system_clock::time_point &from, gpr_timespec *to)
grpc::TimepointHR2Timespec
void TimepointHR2Timespec(const std::chrono::high_resolution_clock::time_point &from, gpr_timespec *to)
grpc::TimePoint< gpr_timespec >::raw_time
gpr_timespec raw_time()
Definition: time.h:60
grpc::GprTimeSpecFromDuration
gpr_timespec GprTimeSpecFromDuration(absl::Duration)
grpc::TimePoint::TimePoint
TimePoint(const T &)=delete
gpr_timespec
Analogous to struct timespec.
Definition: time.h:47
grpc::DurationFromGprTimespec
absl::Duration DurationFromGprTimespec(gpr_timespec time)
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Realtime clock.
Definition: time.h:36
grpc::Timespec2Timepoint
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
grpc::TimePoint
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:45
time.h