std::chrono::floor(std::chrono::time_point)
来自cppreference.com
| 在标头 <chrono> 定义
|
||
| |
(C++17 起) | |
返回能以 ToDuration 表示的小于或等于 tp 的最大时间点 t。
除非 ToDuration 是 std::chrono::duration 的特化,否则此函数不参与重载决议。
参数
| tp | - | 要向下取整的时间点 |
返回值
tp 向下取整到使用 ToDuration 类型时长的下一个时间点。
可能的实现
|
示例
运行此代码
#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
参阅
(C++11) |
转换时间点为同一时钟上拥有不同时长的另一时间点 (函数模板) |
| 转换 time_point 到另一个,向上取整 (函数模板) | |
| 转换 time_point 到另一个,就近取整,偶数优先 (函数模板) | |
(C++17) |
以向下取整的方式,将一个时长转换为另一个时长 (函数模板) |
(C++11)(C++11) |
不大于给定值的最接近整数 (函数) |