blob: 64e164a020cabc99bf65d19bc303d5abc202db5e [file] [log] [blame]
Yijie Maf99b8b52022-12-23 07:01:531//
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//
Nicolas Nobleb7ebd3b2014-11-27 00:33:0318
Craig Tillerf40df232016-03-25 20:38:1419#include <grpc/support/time.h>
Muxi Yan0e00c432018-01-26 23:39:3220#include <grpcpp/support/time.h>
Nicolas Nobleb7ebd3b2014-11-27 00:33:0321
Craig Tillerdbb51642024-10-04 16:41:5622#include <chrono>
23#include <cstdint>
24
Craig Tiller0f9d0242022-05-19 14:34:4825// IWYU pragma: no_include <ratio>
26
Nicolas Nobleb7ebd3b2014-11-27 00:33:0327using std::chrono::duration_cast;
Craig Tillerbaa14a92017-11-03 16:09:3628using std::chrono::high_resolution_clock;
Nicolas Nobleb7ebd3b2014-11-27 00:33:0329using std::chrono::nanoseconds;
30using std::chrono::seconds;
31using std::chrono::system_clock;
32
33namespace grpc {
34
Yang Gao6baa9b62015-03-17 17:49:3935void Timepoint2Timespec(const system_clock::time_point& from,
36 gpr_timespec* to) {
Nicolas Nobleb7ebd3b2014-11-27 00:33:0337 system_clock::duration deadline = from.time_since_epoch();
38 seconds secs = duration_cast<seconds>(deadline);
Yang Gaocdb2a6e2015-03-21 06:55:0439 if (from == system_clock::time_point::max() ||
Craig Tiller143e7bf2015-07-13 15:41:4940 secs.count() >= gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec ||
41 secs.count() < 0) {
42 *to = gpr_inf_future(GPR_CLOCK_REALTIME);
Yang Gaocdb2a6e2015-03-21 06:55:0443 return;
44 }
Nicolas Nobleb7ebd3b2014-11-27 00:33:0345 nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
Noah Eisenbe82e642018-02-09 17:16:5546 to->tv_sec = static_cast<int64_t>(secs.count());
47 to->tv_nsec = static_cast<int32_t>(nsecs.count());
Craig Tiller354398f2015-07-13 16:16:0348 to->clock_type = GPR_CLOCK_REALTIME;
Nicolas Nobleb7ebd3b2014-11-27 00:33:0349}
50
Vijay Pai372fd872015-06-08 20:30:0851void TimepointHR2Timespec(const high_resolution_clock::time_point& from,
52 gpr_timespec* to) {
53 high_resolution_clock::duration deadline = from.time_since_epoch();
54 seconds secs = duration_cast<seconds>(deadline);
55 if (from == high_resolution_clock::time_point::max() ||
Craig Tiller143e7bf2015-07-13 15:41:4956 secs.count() >= gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec ||
57 secs.count() < 0) {
58 *to = gpr_inf_future(GPR_CLOCK_REALTIME);
Vijay Pai372fd872015-06-08 20:30:0859 return;
60 }
61 nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
Noah Eisenbe82e642018-02-09 17:16:5562 to->tv_sec = static_cast<int64_t>(secs.count());
63 to->tv_nsec = static_cast<int32_t>(nsecs.count());
Craig Tiller354398f2015-07-13 16:16:0364 to->clock_type = GPR_CLOCK_REALTIME;
Vijay Pai372fd872015-06-08 20:30:0865}
66
yangg87da1b92014-12-11 23:57:3767system_clock::time_point Timespec2Timepoint(gpr_timespec t) {
Craig Tiller94329d02015-07-23 16:52:1168 if (gpr_time_cmp(t, gpr_inf_future(t.clock_type)) == 0) {
yangged5e7e02015-01-06 18:16:1569 return system_clock::time_point::max();
70 }
Craig Tiller94329d02015-07-23 16:52:1171 t = gpr_convert_clock_type(t, GPR_CLOCK_REALTIME);
Nicolas Nobleb7ebd3b2014-11-27 00:33:0372 system_clock::time_point tp;
rsilvera081e3202014-12-16 23:57:3573 tp += duration_cast<system_clock::time_point::duration>(seconds(t.tv_sec));
74 tp +=
75 duration_cast<system_clock::time_point::duration>(nanoseconds(t.tv_nsec));
Nicolas Nobleb7ebd3b2014-11-27 00:33:0376 return tp;
77}
78
Craig Tiller190d3602015-02-18 17:23:3879} // namespace grpc