Namespaces
Variants

std::chrono:: operator+, std::chrono:: operator- (std::chrono::year_month)

From cppreference.net
(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
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 ( ) 年分を ym に加算します。
3,4) dm. count ( ) の月数を ym に加算します。
5) dy. count ( ) 年分の年数を ym から減算します。
6) dm. count ( ) ヶ月を ym から減算します。
7) ym1 ym2 によって表される2つの時点間の月数の差を返します。

期間が std::chrono::years std::chrono::months の両方に変換可能な場合、 years のオーバーロード (1,2,5) は、呼び出しが曖昧になる可能性がある場合に優先されます。

目次

戻り値

1,2) std:: chrono :: year_month ( ym. year ( ) + dy, ym. month ( ) )
3,4) year_month z であり、 z - ym == dm かつ 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 ( ) ) ) )

注記

2つの 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 を変更する
(公開メンバ関数)