blob: cd59a1970320c0248ef45e5839f5fea786ed19b1 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
yang-g9e2f90c2015-08-21 15:35:03 -070034#include <grpc++/support/config.h>
yang-g9e2f90c2015-08-21 15:35:03 -070035#include <grpc++/support/time.h>
Craig Tillerf40df232016-03-25 13:38:14 -070036#include <grpc/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037
38using std::chrono::duration_cast;
39using std::chrono::nanoseconds;
40using std::chrono::seconds;
41using std::chrono::system_clock;
Vijay Pai372fd872015-06-08 13:30:08 -070042using std::chrono::high_resolution_clock;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
44namespace grpc {
45
Yang Gao6baa9b62015-03-17 10:49:39 -070046void Timepoint2Timespec(const system_clock::time_point& from,
47 gpr_timespec* to) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048 system_clock::duration deadline = from.time_since_epoch();
49 seconds secs = duration_cast<seconds>(deadline);
Yang Gaocdb2a6e2015-03-20 23:55:04 -070050 if (from == system_clock::time_point::max() ||
Craig Tiller143e7bf2015-07-13 08:41:49 -070051 secs.count() >= gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec ||
52 secs.count() < 0) {
53 *to = gpr_inf_future(GPR_CLOCK_REALTIME);
Yang Gaocdb2a6e2015-03-20 23:55:04 -070054 return;
55 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056 nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
Craig Tiller7536af02015-12-22 13:49:30 -080057 to->tv_sec = (int64_t)secs.count();
58 to->tv_nsec = (int32_t)nsecs.count();
Craig Tiller354398f2015-07-13 09:16:03 -070059 to->clock_type = GPR_CLOCK_REALTIME;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060}
61
Vijay Pai372fd872015-06-08 13:30:08 -070062void TimepointHR2Timespec(const high_resolution_clock::time_point& from,
63 gpr_timespec* to) {
64 high_resolution_clock::duration deadline = from.time_since_epoch();
65 seconds secs = duration_cast<seconds>(deadline);
66 if (from == high_resolution_clock::time_point::max() ||
Craig Tiller143e7bf2015-07-13 08:41:49 -070067 secs.count() >= gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec ||
68 secs.count() < 0) {
69 *to = gpr_inf_future(GPR_CLOCK_REALTIME);
Vijay Pai372fd872015-06-08 13:30:08 -070070 return;
71 }
72 nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
Craig Tiller7536af02015-12-22 13:49:30 -080073 to->tv_sec = (int64_t)secs.count();
74 to->tv_nsec = (int32_t)nsecs.count();
Craig Tiller354398f2015-07-13 09:16:03 -070075 to->clock_type = GPR_CLOCK_REALTIME;
Vijay Pai372fd872015-06-08 13:30:08 -070076}
77
yangg87da1b92014-12-11 15:57:37 -080078system_clock::time_point Timespec2Timepoint(gpr_timespec t) {
Craig Tiller94329d02015-07-23 09:52:11 -070079 if (gpr_time_cmp(t, gpr_inf_future(t.clock_type)) == 0) {
yangged5e7e02015-01-06 10:16:15 -080080 return system_clock::time_point::max();
81 }
Craig Tiller94329d02015-07-23 09:52:11 -070082 t = gpr_convert_clock_type(t, GPR_CLOCK_REALTIME);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 system_clock::time_point tp;
rsilvera081e3202014-12-16 15:57:35 -080084 tp += duration_cast<system_clock::time_point::duration>(seconds(t.tv_sec));
85 tp +=
86 duration_cast<system_clock::time_point::duration>(nanoseconds(t.tv_nsec));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087 return tp;
88}
89
Craig Tiller190d3602015-02-18 09:23:38 -080090} // namespace grpc