Here is a ceil
function that will take any time_point
and round it up to the next Duration m
, while giving the result a precision of Duration{1}
:
template <class Clock, class Duration1, class Duration2>
constexpr
auto
ceil(std::chrono::time_point<Clock, Duration1> t, Duration2 m) noexcept
{
using R = std::chrono::time_point<Clock, Duration2>;
auto r = std::chrono::time_point_cast<Duration2>(R{} + (t - R{})/m*m);
if (r < t)
r += m;
return r;
}
It can be used like:
const auto begin = ceil(system_clock::now(), 5min);
const auto begin = ceil(system_clock::now(), 10min);
const auto begin = ceil(system_clock::now(), 10ms);
- First the return type is computed and given the alias
R
.
- Then the
time_point
t
is truncated to the duration
m
(towards zero).
- If the truncated result is less than the input, the result is rounded up to the next
m
.
If 24 hours is not evenly divisible by m
, the result may be counterintuitive.