std::chrono::operator+, std::chrono::operator- (std::chrono::year_month)
来自 cppreference.cn
< cpp | chrono | year month
constexpr std::chrono::year_month operator+( const std::chrono::year_month& ym, const std::chrono::years& dy ) noexcept; |
(1) | (C++20 起) |
constexpr std::chrono::year_month operator+( const std::chrono::years& dy, const std::chrono::year_month& ym ) noexcept; |
(2) | (C++20 起) |
constexpr std::chrono::year_month operator+( const std::chrono::year_month& ym, const std::chrono::months& dm ) noexcept; |
(3) | (C++20 起) |
constexpr std::chrono::year_month operator+( const std::chrono::months& dm, const std::chrono::year_month& ym ) noexcept; |
(4) | (C++20 起) |
constexpr std::chrono::year_month operator-( const std::chrono::year_month& ym, const std::chrono::years& dy ) noexcept; |
(5) | (C++20 起) |
constexpr std::chrono::year_month operator-( const std::chrono::year_month& ym, const std::chrono::months& dm ) noexcept; |
(6) | (C++20 起) |
constexpr std::chrono::months operator-( const std::chrono::year_month& ym1, const std::chrono::year_month& ym2 ) noexcept; |
(7) | (C++20 起) |
1,2) 将 dy.count() years 加到 ym。
3,4) 将 dm.count() months 加到 ym。
5) 从 ym 中减去 dy.count() years。
6) 从 ym 中减去 dm.count() months。
7) 返回由 ym1 和 ym2 表示的两个时间点之间月份的差值。
对于可以转换为 std::chrono::years 和 std::chrono::months 的持续时间,如果调用在其他情况下会产生歧义,则优先选择 years 重载 (1,2,5)。
目录 |
[编辑] 返回值
1,2) std::chrono::year_month(ym.year() + dy, ym.month())
3,4) A
year_month
value z
such that z - ym == dm and z.ok() == true.5) ym + -dy
6) ym + -dm
7)
ym1.year() - ym2.year() + std::chrono::months(int(unsigned(ym1.month())) -
int(unsigned(ym2.month())))
ym1.year() - ym2.year() + std::chrono::months(int(unsigned(ym1.month())) -
int(unsigned(ym2.month())))
[编辑] 注释
两个 year_month 值相减的结果是 std::chrono::months 类型的持续时间。此持续时间单位表示格里高利历平均月份的长度(30.436875 天),并且结果持续时间与问题时间段内的实际天数无关。例如,2017y/3 - 2017y/2 的结果是 std::chrono::months(1),即使 2017 年 2 月仅包含 28 天。
[编辑] 示例
运行此代码
#include <cassert> #include <chrono> int main() { auto ym{std::chrono::year(2021)/std::chrono::July}; ym = ym + std::chrono::months(14); assert(ym.month() == std::chrono::September); assert(ym.year() == std::chrono::year(2022)); ym = ym - std::chrono::years(3); assert(ym.month() == std::chrono::month(9)); assert(ym.year() == std::chrono::year(2019)); ym = ym + (std::chrono::September - std::chrono::month(2)); assert(ym.month() == std::chrono::April); assert(ym.year() == std::chrono::year(2020)); }
[编辑] 参见
通过一定数量的月份或年份修改 year_month (公共成员函数) |