Chrono.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  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. #pragma once
  17. #include <chrono>
  18. #include <stdexcept>
  19. #include <type_traits>
  20. #include <folly/Portability.h>
  21. #include <folly/lang/Exception.h>
  22. #include <folly/portability/Time.h>
  23. /***
  24. * include or backport:
  25. * * std::chrono::ceil
  26. * * std::chrono::floor
  27. * * std::chrono::round
  28. */
  29. #if __cpp_lib_chrono >= 201510 || _MSC_VER
  30. namespace folly {
  31. namespace chrono {
  32. /* using override */ using std::chrono::ceil;
  33. /* using override */ using std::chrono::floor;
  34. /* using override */ using std::chrono::round;
  35. } // namespace chrono
  36. } // namespace folly
  37. #else
  38. namespace folly {
  39. namespace chrono {
  40. namespace detail {
  41. // from: http://en.cppreference.com/w/cpp/chrono/duration/ceil, CC-BY-SA
  42. template <typename T>
  43. struct is_duration : std::false_type {};
  44. template <typename Rep, typename Period>
  45. struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {};
  46. template <typename To, typename Duration>
  47. constexpr To ceil_impl(Duration const& d, To const& t) {
  48. return t < d ? t + To{1} : t;
  49. }
  50. template <typename To, typename Duration>
  51. constexpr To floor_impl(Duration const& d, To const& t) {
  52. return t > d ? t - To{1} : t;
  53. }
  54. template <typename To, typename Diff>
  55. constexpr To round_impl(To const& t0, To const& t1, Diff diff0, Diff diff1) {
  56. return diff0 < diff1 ? t0 : diff1 < diff0 ? t1 : t0.count() & 1 ? t1 : t0;
  57. }
  58. template <typename To, typename Duration>
  59. constexpr To round_impl(Duration const& d, To const& t0, To const& t1) {
  60. return round_impl(t0, t1, d - t0, t1 - d);
  61. }
  62. template <typename To, typename Duration>
  63. constexpr To round_impl(Duration const& d, To const& t0) {
  64. return round_impl(d, t0, t0 + To{1});
  65. }
  66. } // namespace detail
  67. // mimic: std::chrono::ceil, C++17
  68. // from: http://en.cppreference.com/w/cpp/chrono/duration/ceil, CC-BY-SA
  69. template <
  70. typename To,
  71. typename Rep,
  72. typename Period,
  73. typename = typename std::enable_if<detail::is_duration<To>::value>::type>
  74. constexpr To ceil(std::chrono::duration<Rep, Period> const& d) {
  75. return detail::ceil_impl(d, std::chrono::duration_cast<To>(d));
  76. }
  77. // mimic: std::chrono::ceil, C++17
  78. // from: http://en.cppreference.com/w/cpp/chrono/time_point/ceil, CC-BY-SA
  79. template <
  80. typename To,
  81. typename Clock,
  82. typename Duration,
  83. typename = typename std::enable_if<detail::is_duration<To>::value>::type>
  84. constexpr std::chrono::time_point<Clock, To> ceil(
  85. std::chrono::time_point<Clock, Duration> const& tp) {
  86. return std::chrono::time_point<Clock, To>{ceil<To>(tp.time_since_epoch())};
  87. }
  88. // mimic: std::chrono::floor, C++17
  89. // from: http://en.cppreference.com/w/cpp/chrono/duration/floor, CC-BY-SA
  90. template <
  91. typename To,
  92. typename Rep,
  93. typename Period,
  94. typename = typename std::enable_if<detail::is_duration<To>::value>::type>
  95. constexpr To floor(std::chrono::duration<Rep, Period> const& d) {
  96. return detail::floor_impl(d, std::chrono::duration_cast<To>(d));
  97. }
  98. // mimic: std::chrono::floor, C++17
  99. // from: http://en.cppreference.com/w/cpp/chrono/time_point/floor, CC-BY-SA
  100. template <
  101. typename To,
  102. typename Clock,
  103. typename Duration,
  104. typename = typename std::enable_if<detail::is_duration<To>::value>::type>
  105. constexpr std::chrono::time_point<Clock, To> floor(
  106. std::chrono::time_point<Clock, Duration> const& tp) {
  107. return std::chrono::time_point<Clock, To>{floor<To>(tp.time_since_epoch())};
  108. }
  109. // mimic: std::chrono::round, C++17
  110. // from: http://en.cppreference.com/w/cpp/chrono/duration/round, CC-BY-SA
  111. template <
  112. typename To,
  113. typename Rep,
  114. typename Period,
  115. typename = typename std::enable_if<
  116. detail::is_duration<To>::value &&
  117. !std::chrono::treat_as_floating_point<typename To::rep>::value>::type>
  118. constexpr To round(std::chrono::duration<Rep, Period> const& d) {
  119. return detail::round_impl(d, floor<To>(d));
  120. }
  121. // mimic: std::chrono::round, C++17
  122. // from: http://en.cppreference.com/w/cpp/chrono/time_point/round, CC-BY-SA
  123. template <
  124. typename To,
  125. typename Clock,
  126. typename Duration,
  127. typename = typename std::enable_if<
  128. detail::is_duration<To>::value &&
  129. !std::chrono::treat_as_floating_point<typename To::rep>::value>::type>
  130. constexpr std::chrono::time_point<Clock, To> round(
  131. std::chrono::time_point<Clock, Duration> const& tp) {
  132. return std::chrono::time_point<Clock, To>{round<To>(tp.time_since_epoch())};
  133. }
  134. } // namespace chrono
  135. } // namespace folly
  136. #endif
  137. namespace folly {
  138. namespace chrono {
  139. struct coarse_steady_clock {
  140. using rep = std::chrono::milliseconds::rep;
  141. using period = std::chrono::milliseconds::period;
  142. using duration = std::chrono::duration<rep, period>;
  143. using time_point = std::chrono::time_point<coarse_steady_clock, duration>;
  144. constexpr static bool is_steady = true;
  145. static time_point now() {
  146. #ifndef CLOCK_MONOTONIC_COARSE
  147. return time_point(std::chrono::duration_cast<duration>(
  148. std::chrono::steady_clock::now().time_since_epoch()));
  149. #else
  150. timespec ts;
  151. auto ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
  152. if (ret != 0) {
  153. throw_exception<std::runtime_error>(
  154. "Error using CLOCK_MONOTONIC_COARSE.");
  155. }
  156. return time_point(std::chrono::duration_cast<duration>(
  157. std::chrono::seconds(ts.tv_sec) +
  158. std::chrono::nanoseconds(ts.tv_nsec)));
  159. #endif
  160. }
  161. };
  162. } // namespace chrono
  163. } // namespace folly