| 1 |
// |
| 2 |
// Copyright 2021 the gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include "src/core/lib/gprpp/time_util.h" |
| 20 |
|
| 21 |
#include <stdint.h> |
| 22 |
#include <time.h> |
| 23 |
|
| 24 |
#include <grpc/support/log.h> |
| 25 |
#include <grpc/support/time.h> |
| 26 |
|
| 27 |
namespace grpc_core { |
| 28 |
|
| 29 |
gpr_timespec ToGprTimeSpec(absl::Duration duration) { |
| 30 |
if (duration == absl::InfiniteDuration()) { |
| 31 |
return gpr_inf_future(GPR_TIMESPAN); |
| 32 |
} else if (duration == -absl::InfiniteDuration()) { |
| 33 |
return gpr_inf_past(GPR_TIMESPAN); |
| 34 |
} else { |
| 35 |
int64_t s = absl::IDivDuration(duration, absl::Seconds(1), &duration); |
| 36 |
int64_t n = absl::IDivDuration(duration, absl::Nanoseconds(1), &duration); |
| 37 |
return gpr_time_add(gpr_time_from_seconds(s, GPR_TIMESPAN), |
| 38 |
gpr_time_from_nanos(n, GPR_TIMESPAN)); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
gpr_timespec ToGprTimeSpec(absl::Time time) { |
| 43 |
if (time == absl::InfiniteFuture()) { |
| 44 |
return gpr_inf_future(GPR_CLOCK_REALTIME); |
| 45 |
} else if (time == absl::InfinitePast()) { |
| 46 |
return gpr_inf_past(GPR_CLOCK_REALTIME); |
| 47 |
} else { |
| 48 |
timespec ts = absl::ToTimespec(time); |
| 49 |
gpr_timespec out; |
| 50 |
out.tv_sec = static_cast<decltype(out.tv_sec)>(ts.tv_sec); |
| 51 |
out.tv_nsec = static_cast<decltype(out.tv_nsec)>(ts.tv_nsec); |
| 52 |
out.clock_type = GPR_CLOCK_REALTIME; |
| 53 |
return out; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
absl::Duration ToAbslDuration(gpr_timespec ts) { |
| 58 |
GPR_ASSERT(ts.clock_type == GPR_TIMESPAN); |
| 59 |
if (gpr_time_cmp(ts, gpr_inf_future(GPR_TIMESPAN)) == 0) { |
| 60 |
return absl::InfiniteDuration(); |
| 61 |
} else if (gpr_time_cmp(ts, gpr_inf_past(GPR_TIMESPAN)) == 0) { |
| 62 |
return -absl::InfiniteDuration(); |
| 63 |
} else { |
| 64 |
return absl::Seconds(ts.tv_sec) + absl::Nanoseconds(ts.tv_nsec); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
absl::Time ToAbslTime(gpr_timespec ts) { |
| 69 |
GPR_ASSERT(ts.clock_type != GPR_TIMESPAN); |
| 70 |
gpr_timespec rts = gpr_convert_clock_type(ts, GPR_CLOCK_REALTIME); |
| 71 |
if (gpr_time_cmp(rts, gpr_inf_future(GPR_CLOCK_REALTIME)) == 0) { |
| 72 |
return absl::InfiniteFuture(); |
| 73 |
} else if (gpr_time_cmp(rts, gpr_inf_past(GPR_CLOCK_REALTIME)) == 0) { |
| 74 |
return absl::InfinitePast(); |
| 75 |
} else { |
| 76 |
return absl::UnixEpoch() + absl::Seconds(rts.tv_sec) + |
| 77 |
absl::Nanoseconds(rts.tv_nsec); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
} // namespace grpc_core |