Yijie Ma | f99b8b5 | 2022-12-23 07:01:53 | [diff] [blame] | 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 | // |
Nicolas Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 18 | |
Craig Tiller | f40df23 | 2016-03-25 20:38:14 | [diff] [blame] | 19 | #include <grpc/support/time.h> |
Muxi Yan | 0e00c43 | 2018-01-26 23:39:32 | [diff] [blame] | 20 | #include <grpcpp/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 21 | |
Craig Tiller | dbb5164 | 2024-10-04 16:41:56 | [diff] [blame] | 22 | #include <chrono> |
| 23 | #include <cstdint> |
| 24 | |
Craig Tiller | 0f9d024 | 2022-05-19 14:34:48 | [diff] [blame] | 25 | // IWYU pragma: no_include <ratio> |
| 26 | |
Nicolas Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 27 | using std::chrono::duration_cast; |
Craig Tiller | baa14a9 | 2017-11-03 16:09:36 | [diff] [blame] | 28 | using std::chrono::high_resolution_clock; |
Nicolas Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 29 | using std::chrono::nanoseconds; |
| 30 | using std::chrono::seconds; |
| 31 | using std::chrono::system_clock; |
| 32 | |
| 33 | namespace grpc { |
| 34 | |
Yang Gao | 6baa9b6 | 2015-03-17 17:49:39 | [diff] [blame] | 35 | void Timepoint2Timespec(const system_clock::time_point& from, |
| 36 | gpr_timespec* to) { |
Nicolas Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 37 | system_clock::duration deadline = from.time_since_epoch(); |
| 38 | seconds secs = duration_cast<seconds>(deadline); |
Yang Gao | cdb2a6e | 2015-03-21 06:55:04 | [diff] [blame] | 39 | if (from == system_clock::time_point::max() || |
Craig Tiller | 143e7bf | 2015-07-13 15:41:49 | [diff] [blame] | 40 | secs.count() >= gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec || |
| 41 | secs.count() < 0) { |
| 42 | *to = gpr_inf_future(GPR_CLOCK_REALTIME); |
Yang Gao | cdb2a6e | 2015-03-21 06:55:04 | [diff] [blame] | 43 | return; |
| 44 | } |
Nicolas Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 45 | nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs); |
Noah Eisen | be82e64 | 2018-02-09 17:16:55 | [diff] [blame] | 46 | to->tv_sec = static_cast<int64_t>(secs.count()); |
| 47 | to->tv_nsec = static_cast<int32_t>(nsecs.count()); |
Craig Tiller | 354398f | 2015-07-13 16:16:03 | [diff] [blame] | 48 | to->clock_type = GPR_CLOCK_REALTIME; |
Nicolas Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 49 | } |
| 50 | |
Vijay Pai | 372fd87 | 2015-06-08 20:30:08 | [diff] [blame] | 51 | void 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 Tiller | 143e7bf | 2015-07-13 15:41:49 | [diff] [blame] | 56 | secs.count() >= gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec || |
| 57 | secs.count() < 0) { |
| 58 | *to = gpr_inf_future(GPR_CLOCK_REALTIME); |
Vijay Pai | 372fd87 | 2015-06-08 20:30:08 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs); |
Noah Eisen | be82e64 | 2018-02-09 17:16:55 | [diff] [blame] | 62 | to->tv_sec = static_cast<int64_t>(secs.count()); |
| 63 | to->tv_nsec = static_cast<int32_t>(nsecs.count()); |
Craig Tiller | 354398f | 2015-07-13 16:16:03 | [diff] [blame] | 64 | to->clock_type = GPR_CLOCK_REALTIME; |
Vijay Pai | 372fd87 | 2015-06-08 20:30:08 | [diff] [blame] | 65 | } |
| 66 | |
yangg | 87da1b9 | 2014-12-11 23:57:37 | [diff] [blame] | 67 | system_clock::time_point Timespec2Timepoint(gpr_timespec t) { |
Craig Tiller | 94329d0 | 2015-07-23 16:52:11 | [diff] [blame] | 68 | if (gpr_time_cmp(t, gpr_inf_future(t.clock_type)) == 0) { |
yangg | ed5e7e0 | 2015-01-06 18:16:15 | [diff] [blame] | 69 | return system_clock::time_point::max(); |
| 70 | } |
Craig Tiller | 94329d0 | 2015-07-23 16:52:11 | [diff] [blame] | 71 | t = gpr_convert_clock_type(t, GPR_CLOCK_REALTIME); |
Nicolas Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 72 | system_clock::time_point tp; |
rsilvera | 081e320 | 2014-12-16 23:57:35 | [diff] [blame] | 73 | 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 Noble | b7ebd3b | 2014-11-27 00:33:03 | [diff] [blame] | 76 | return tp; |
| 77 | } |
| 78 | |
Craig Tiller | 190d360 | 2015-02-18 17:23:38 | [diff] [blame] | 79 | } // namespace grpc |