std::chrono::year_month_day_last
来自 cppreference.cn
定义于头文件 <chrono> |
||
class year_month_day_last; |
(自 C++20 起) | |
类 year_month_day_last
表示特定年份和月份的最后一天。它是一个基于字段的时间点,分辨率为 std::chrono::days,但受限于它只能表示一个月的最后一天。
直接支持面向 std::chrono::years 和 std::chrono::months 的算术运算。隐式转换为 std::chrono::sys_days 允许高效地执行面向 std::chrono::days 的算术运算。
year_month_day_last
是 TriviallyCopyable StandardLayoutType。
内容 |
[编辑] 成员函数
构造一个 year_month_day_last 对象(公共成员函数) | |
通过一些月份或年份修改时间点 (公共成员函数) | |
访问此对象的字段 (公共成员函数) | |
转换为 std::chrono::time_point (公共成员函数) | |
检查此对象是否表示有效日期 (公共成员函数) |
[编辑] 非成员函数
(C++20) |
比较两个 year_month_day_last 值(函数) |
(C++20) |
添加或减去 year_month_day_last 和一些年份或月份(函数) |
(C++20) |
将 year_month_day_last 输出到流(函数模板) |
[编辑] 辅助类
year_month_day_last 的格式化支持(类模板特化) | |
为 std::chrono::year_month_day_last 提供哈希支持 (类模板特化) |
[编辑] 示例
运行此代码
#include <chrono> #include <iostream> int main() { const auto ymd = std::chrono::year_month_day { std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now()) }; const std::chrono::year_month_day_last ymdl { ymd.year(), ymd.month() / std::chrono::last }; std::cout << "The last day of present month (" << ymdl << ") is: " << std::chrono::year_month_day{ymdl}.day() << '\n'; // The 'last' object can be placed wherever it is legal to place a 'day': using namespace std::chrono; constexpr std::chrono::year_month_day_last ymdl1 = 2023y / February / last, ymdl2 = last / February / 2023y, ymdl3 = February / last / 2023y; static_assert(ymdl1 == ymdl2 && ymdl2 == ymdl3); }
可能的输出
The last day of present month (2023/Aug/last) is: 31
[编辑] 参见
(C++20) |
表示特定的 year、 month 和 day (类) |