命名空间
变体
操作

std::chrono::year_month_day_last

来自 cppreference.cn
< cpp‎ | chrono
 
 
日期和时间库
时间点
(C++11)
(C++20)
持续时间
(C++11)
时钟
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
一天中的时间
(C++20)(C++20)
(C++20)(C++20)
(C++20)
日历
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
year_month_day_last
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
chrono I/O
(C++20)

 
 
定义于头文件 <chrono>
class year_month_day_last;
(自 C++20 起)

year_month_day_last 表示特定年份和月份的最后一天。它是一个基于字段的时间点,分辨率为 std::chrono::days,但受限于它只能表示一个月的最后一天。

直接支持面向 std::chrono::yearsstd::chrono::months 的算术运算。隐式转换为 std::chrono::sys_days 允许高效地执行面向 std::chrono::days 的算术运算。

year_month_day_lastTriviallyCopyable StandardLayoutType

内容

[编辑] 成员函数

构造一个 year_month_day_last 对象
(公共成员函数) [编辑]
通过一些月份或年份修改时间点
(公共成员函数) [编辑]
访问此对象的字段
(公共成员函数) [编辑]
转换为 std::chrono::time_point
(公共成员函数) [编辑]
检查此对象是否表示有效日期
(公共成员函数) [编辑]

[编辑] 非成员函数

比较两个 year_month_day_last
(函数) [编辑]
添加或减去 year_month_day_last 和一些年份或月份
(函数) [编辑]
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

[编辑] 参见

表示特定的 yearmonthday
(类) [编辑]