std::chrono::round(std::chrono::time_point)
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <chrono> で定義
|
||
template <class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> round(const time_point<Clock, Duration>& tp); |
(C++17以上) | |
ToDuration で表現可能な tp に最も近い time point を返します。 ちょうど中間の場合は偶数に丸めます。
この関数は、 ToDuration が std::chrono::duration の特殊化でないか、 std::chrono::treat_as_floating_point<typename ToDuration::rep>::value が false でなければ、オーバーロード解決に参加しません。
引数
| tp | - | 最も近い値に丸める time point |
戻り値
ToDuration 型の duration を使用する最も近い time point に丸めた d。 ちょうど中間の場合は偶数に丸めます。
実装例
template <class T> struct is_duration : std::false_type {};
template <class Rep, class Period> struct is_duration<
std::chrono::duration<Rep, Period>> : std::true_type {};
template <class To, class Clock, class FromDuration,
class = std::enable_if_t<is_duration<To>{}
&& !std::chrono::treat_as_floating_point<typename To::rep>{}>>
constexpr std::chrono::time_point<Clock, To> round(
const std::chrono::time_point<Clock, FromDuration>& tp)
{
return std::chrono::time_point<Clock, To>{
std::chrono::round<To>(tp.time_since_epoch())};
}
|
例
Run this code
#include <iostream>
#include <chrono>
#include <string>
template <typename TimePoint>
std::string to_string(const TimePoint& time_point)
{
return std::to_string(time_point.time_since_epoch().count());
}
int main()
{
using namespace std::literals::chrono_literals;
using Sec = std::chrono::seconds;
std::cout << "Time point\t" "Cast\t" "Floor\t" "Round\t" "Ceil\n";
std::cout << "(ms)\t\t" "(s)\t" "(s)\t" "(s)\t" "(s)\n";
for (const auto value_ms: {5432ms, 5678ms}) {
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>
time_point_ms(value_ms);
std::cout
<< to_string(time_point_ms) << "\t\t"
<< to_string(std::chrono::time_point_cast<Sec>(time_point_ms)) << "\t"
<< to_string(std::chrono::floor <Sec>(time_point_ms)) << "\t"
<< to_string(std::chrono::round <Sec>(time_point_ms)) << "\t"
<< to_string(std::chrono::ceil <Sec>(time_point_ms)) << "\n";
}
}
出力:
Time point Cast Floor Round Ceil
(ms) (s) (s) (s) (s)
5432 5 5 5 6
5678 5 5 6 6
関連項目
| time point を同じ時計の異なる時間単位の time point に変換します (関数テンプレート) | |
| time_point を別の time_point に切り上げ変換します (関数テンプレート) | |
| time_point を別の time_point に切り捨て変換します (関数テンプレート) | |
(C++17) |
時間を別の時間の最も近い値に丸めて変換します (関数テンプレート) |
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11) |
最も近い整数を返します。 ちょうど中間の場合はゼロから離れる方向に丸めます (関数) |