time_cc.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include <grpc/support/time.h>
  19. #include <grpcpp/support/config.h>
  20. #include <grpcpp/support/time.h>
  21. using std::chrono::duration_cast;
  22. using std::chrono::high_resolution_clock;
  23. using std::chrono::nanoseconds;
  24. using std::chrono::seconds;
  25. using std::chrono::system_clock;
  26. namespace grpc {
  27. void Timepoint2Timespec(const system_clock::time_point& from,
  28. gpr_timespec* to) {
  29. system_clock::duration deadline = from.time_since_epoch();
  30. seconds secs = duration_cast<seconds>(deadline);
  31. if (from == system_clock::time_point::max() ||
  32. secs.count() >= gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec ||
  33. secs.count() < 0) {
  34. *to = gpr_inf_future(GPR_CLOCK_REALTIME);
  35. return;
  36. }
  37. nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
  38. to->tv_sec = static_cast<int64_t>(secs.count());
  39. to->tv_nsec = static_cast<int32_t>(nsecs.count());
  40. to->clock_type = GPR_CLOCK_REALTIME;
  41. }
  42. void TimepointHR2Timespec(const high_resolution_clock::time_point& from,
  43. gpr_timespec* to) {
  44. high_resolution_clock::duration deadline = from.time_since_epoch();
  45. seconds secs = duration_cast<seconds>(deadline);
  46. if (from == high_resolution_clock::time_point::max() ||
  47. secs.count() >= gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec ||
  48. secs.count() < 0) {
  49. *to = gpr_inf_future(GPR_CLOCK_REALTIME);
  50. return;
  51. }
  52. nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
  53. to->tv_sec = static_cast<int64_t>(secs.count());
  54. to->tv_nsec = static_cast<int32_t>(nsecs.count());
  55. to->clock_type = GPR_CLOCK_REALTIME;
  56. }
  57. system_clock::time_point Timespec2Timepoint(gpr_timespec t) {
  58. if (gpr_time_cmp(t, gpr_inf_future(t.clock_type)) == 0) {
  59. return system_clock::time_point::max();
  60. }
  61. t = gpr_convert_clock_type(t, GPR_CLOCK_REALTIME);
  62. system_clock::time_point tp;
  63. tp += duration_cast<system_clock::time_point::duration>(seconds(t.tv_sec));
  64. tp +=
  65. duration_cast<system_clock::time_point::duration>(nanoseconds(t.tv_nsec));
  66. return tp;
  67. }
  68. } // namespace grpc