最終更新日時(UTC):
が更新

履歴 編集

class
<chrono>

std::chrono::year_month(C++20)

namespace std::chrono {
  class year_month;
}

概要

year_monthは、年と月を表すカレンダー表現のためクラスである。

このクラスは、年、および月に関する演算に対応している。

このクラスは等値比較および大小比較ができ、EqualityComparableおよびLessThanComparableの要件を満たす。

このクラスは、トリビアルコピー可能で、かつスタンダードレイアウト型である。

メンバ関数

構築/コピー/破棄

名前 説明 対応バージョン
(constructor) コンストラクタ C++20
year_month& operator=(const year_month&) = default;
year_month& operator=(year_month&&) = default;
代入演算子 C++20

算術演算

名前 説明 対応バージョン
operator+= 加算の複合代入 C++20
operator-= 減算の複合代入 C++20

観測

名前 説明 対応バージョン
year 年要素を取得する C++20
month 月要素を取得する C++20

検証

名前 説明 対応バージョン
ok 値が範囲に収まっているか判定する C++20

非メンバ関数

カレンダー構文演算子

名前 説明 対応バージョン
operator/ カレンダー要素同士をつなぎ合わせる C++20

算術演算

名前 説明 対応バージョン
operator+ 加算 C++20
operator- 減算 C++20

比較演算

名前 説明 対応バージョン
operator== 等値比較を行う C++20
bool operator!=(const year_month&, const year_month&) noexcept; 非等値比較を行う (==により使用可能) C++20
operator<=> 三方比較を行う C++20
bool operator<(const year_month&, const year_month&) noexcept; 左辺が右辺より小さいかを判定する (<=>により使用可能) C++20
bool operator<=(const year_month&, const year_month&) noexcept; 左辺が右辺以下を判定する (<=>により使用可能) C++20
bool operator>(const year_month&, const year_month&) noexcept; 左辺が右辺より大きいかを判定する (<=>により使用可能) C++20
bool operator>=(const year_month&, const year_month&) noexcept; 左辺が右辺以上を判定する (<=>により使用可能) C++20

入出力

名前 説明 対応バージョン
operator<< 出力ストリームに出力する C++20
from_stream フォーマット指定して入力ストリームから入力する C++20

文字列フォーマット

名前 説明 対応バージョン
formatter std::formatterクラスの特殊化 C++20

#include <iostream>
#include <chrono>

namespace chrono = std::chrono;
using namespace std::chrono_literals;

int main()
{
  // すべて2020年3月を表す
  chrono::year_month date1 = 2020y/3;
  chrono::year_month date2 = 2020y/chrono::March;

  // 各カレンダー要素のコンストラクタはexplicitなので、
  // 指定順は年、月で決まっているが、int値は指定できない
  chrono::year_month date3{2020y, chrono::March};
  chrono::year_month date4{chrono::year{2020}, chrono::month{3}};

  std::cout << date1 << std::endl;
  std::cout << date2 << std::endl;
  std::cout << date3 << std::endl;
  std::cout << date4 << std::endl;
}

出力

2020/Mar
2020/Mar
2020/Mar
2020/Mar

バージョン

言語

  • C++20

処理系

  • Clang: 8.0 (入出力ストリームなし)
  • GCC: 11.1 (入出力ストリームなし)
  • Visual C++: (2019 Update 3時点で実装なし)