2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <type_traits>
23 * include or backport:
25 * * std::chrono::floor
26 * * std::chrono::round
29 #if __cpp_lib_chrono >= 201510 || _MSC_VER
34 /* using override */ using std::chrono::ceil;
35 /* using override */ using std::chrono::floor;
36 /* using override */ using std::chrono::round;
47 // from: http://en.cppreference.com/w/cpp/chrono/duration/ceil, CC-BY-SA
49 struct is_duration : std::false_type {};
50 template <typename Rep, typename Period>
51 struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {};
53 template <typename To, typename Duration>
54 constexpr To ceil_impl(Duration const& d, To const& t) {
55 return t < d ? t + To{1} : t;
58 template <typename To, typename Duration>
59 constexpr To floor_impl(Duration const& d, To const& t) {
60 return t > d ? t - To{1} : t;
63 template <typename To, typename Diff>
64 constexpr To round_impl(To const& t0, To const& t1, Diff diff0, Diff diff1) {
65 return diff0 < diff1 ? t0 : diff1 < diff0 ? t1 : t0.count() & 1 ? t1 : t0;
68 template <typename To, typename Duration>
69 constexpr To round_impl(Duration const& d, To const& t0, To const& t1) {
70 return round_impl(t0, t1, d - t0, t1 - d);
73 template <typename To, typename Duration>
74 constexpr To round_impl(Duration const& d, To const& t0) {
75 return round_impl(d, t0, t0 + To{1});
79 // mimic: std::chrono::ceil, C++17
80 // from: http://en.cppreference.com/w/cpp/chrono/duration/ceil, CC-BY-SA
85 typename = typename std::enable_if<detail::is_duration<To>::value>::type>
86 constexpr To ceil(std::chrono::duration<Rep, Period> const& d) {
87 return detail::ceil_impl(d, std::chrono::duration_cast<To>(d));
90 // mimic: std::chrono::ceil, C++17
91 // from: http://en.cppreference.com/w/cpp/chrono/time_point/ceil, CC-BY-SA
96 typename = typename std::enable_if<detail::is_duration<To>::value>::type>
97 constexpr std::chrono::time_point<Clock, To> ceil(
98 std::chrono::time_point<Clock, Duration> const& tp) {
99 return std::chrono::time_point<Clock, To>{ceil<To>(tp.time_since_epoch())};
102 // mimic: std::chrono::floor, C++17
103 // from: http://en.cppreference.com/w/cpp/chrono/duration/floor, CC-BY-SA
108 typename = typename std::enable_if<detail::is_duration<To>::value>::type>
109 constexpr To floor(std::chrono::duration<Rep, Period> const& d) {
110 return detail::floor_impl(d, std::chrono::duration_cast<To>(d));
113 // mimic: std::chrono::floor, C++17
114 // from: http://en.cppreference.com/w/cpp/chrono/time_point/floor, CC-BY-SA
119 typename = typename std::enable_if<detail::is_duration<To>::value>::type>
120 constexpr std::chrono::time_point<Clock, To> floor(
121 std::chrono::time_point<Clock, Duration> const& tp) {
122 return std::chrono::time_point<Clock, To>{floor<To>(tp.time_since_epoch())};
125 // mimic: std::chrono::round, C++17
126 // from: http://en.cppreference.com/w/cpp/chrono/duration/round, CC-BY-SA
131 typename = typename std::enable_if<
132 detail::is_duration<To>::value &&
133 !std::chrono::treat_as_floating_point<typename To::rep>::value>::type>
134 constexpr To round(std::chrono::duration<Rep, Period> const& d) {
135 return detail::round_impl(d, floor<To>(d));
138 // mimic: std::chrono::round, C++17
139 // from: http://en.cppreference.com/w/cpp/chrono/time_point/round, CC-BY-SA
144 typename = typename std::enable_if<
145 detail::is_duration<To>::value &&
146 !std::chrono::treat_as_floating_point<typename To::rep>::value>::type>
147 constexpr std::chrono::time_point<Clock, To> round(
148 std::chrono::time_point<Clock, Duration> const& tp) {
149 return std::chrono::time_point<Clock, To>{round<To>(tp.time_since_epoch())};