C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- std::chrono::duration
- std::chrono::time_point
- std::chrono::round(std::chrono::time_point)
- std::chrono::time_point_cast
- std::chrono::floor(std::chrono::time_point)
- std::chrono::ceil(std::chrono::time_point)
- operator+, operator-(std::time_point)
- operator==,!=,<,<=,>,>=,<=>(std::chrono::time_point)
- std::common_type(std::chrono::time_point)
- std::chrono::clock_cast
- std::chrono::is_am, std::chrono::is_pm, std::chrono::make12, std::chrono::make24
- std::chrono::system_clock
- std::chrono::steady_clock
- std::chrono::high_resolution_clock
- std::chrono::clock_time_conversion
- C 日期和时间工具
- std::chrono::is_clock
- std::chrono::utc_clock
- std::chrono::tai_clock
- std::chrono::gps_clock
- std::chrono::file_clock
- std::chrono::local_t
- std::chrono::day
- std::chrono::month
- std::chrono::year
- std::chrono::weekday
- std::chrono::operator/ (日历)
- std::chrono::year_month_day
- std::chrono::year_month_day_last
- std::chrono::year_month_weekday
- std::chrono::year_month_weekday_last
- std::chrono::weekday_indexed
- std::chrono::weekday_last
- std::chrono::month_day
- std::chrono::month_day_last
- std::chrono::month_weekday
- std::chrono::month_weekday_last
- std::chrono::year_month
- std::chrono::tzdb
- std::chrono::tzdb_list
- std::chrono::get_tzdb_list, std::chrono::get_tzdb, std::chrono::remote_version, std::chrono::reload_tzdb
- std::chrono::time_zone
- std::chrono::locate_zone
- std::chrono::current_zone
- std::chrono::zoned_time
- std::chrono::time_zone_link
- std::chrono::leap_second
- std::chrono::time_of_day
- std::chrono::last_spec
- std::chrono::treat_as_floating_point
- std::literals::chrono_literals::operator""ns
- std::literals::chrono_literals::operator""y
- std::literals::chrono_literals::operator""d
- std::literals::chrono_literals::operator""s
- std::literals::chrono_literals::operator""ms
- std::literals::chrono_literals::operator""us
- std::literals::chrono_literals::operator""h
- std::literals::chrono_literals::operator""min
- std::chrono::duration_values
- std::chrono::nonexistent_local_time
- std::chrono::ambiguous_local_time
- 注释
- 字符串库
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >日期和时间工具 >std::chrono::time_point > std::chrono::floor(std::chrono::time_point)
std::chrono::floor(std::chrono::time_point)
定义于头文件 <chrono>
|
||
template <class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> floor(const time_point<Clock, Duration>& tp); |
(C++17 起) | |
返回能以 ToDuration
表示的小于或等于 tp
的最大时间点 t
。
函数不参与重载决议,除非 ToDuration
是 std::chrono::duration 的特化。
参数
tp | - | 要向下舍入的 time_point |
返回值
向下舍入到使用 ToDuration
类型时长的下一个时间点的 tp
。
可能的实现
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>{}>> constexpr std::chrono::time_point<Clock, To> floor(const std::chrono::time_point<Clock, FromDuration>& tp) { return std::chrono::time_point<Clock, To>{ std::chrono::floor<To>(tp.time_since_epoch())}; } |
示例
运行此代码
#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 到另一个,就近取整,偶数优先 (函数模板) | |
(C++17) |
以向下取整的方式,将一个时长转换为另一个时长 (函数模板) |
(C++11)(C++11) |
不大于给定值的最接近整数 (函数) |