std::chrono::operator/ (日历)

来自cppreference.com
< cpp‎ | chrono
 
 
工具库
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
初等字符串转换
(C++17)
(C++17)
 
日期和时间工具
时间点
(C++11)
(C++20)
时长
(C++11)
时钟
(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)
operator/
(C++20)
(C++20)
(C++20)(C++20)
时区
(C++20)
(C++20)
(C++20)
chrono I/O
(C++20)
C 风格日期和时间
 
在标头 <chrono> 定义
year_month
constexpr auto operator/(const std::chrono::year& y,

                         const std::chrono::month& m) noexcept

    -> std::chrono::year_month;
(1) (C++20 起)
constexpr auto operator/(const std::chrono::year& y, int m) noexcept
    -> std::chrono::year_month;
(2) (C++20 起)
month_day
constexpr auto operator/(const std::chrono::month& m,

                         const std::chrono::day& d) noexcept

    -> std::chrono::month_day;
(3) (C++20 起)
constexpr auto operator/(const std::chrono::month& m, int d) noexcept
    -> std::chrono::month_day;
(4) (C++20 起)
constexpr auto operator/(int m, const std::chrono::day& d) noexcept
    -> std::chrono::month_day;
(5) (C++20 起)
constexpr auto operator/(const std::chrono::day& d,

                         const std::chrono::month& m) noexcept

   -> std::chrono::month_day;
(6) (C++20 起)
constexpr auto operator/(const std::chrono::day& d, int m) noexcept
    -> std::chrono::month_day;
(7) (C++20 起)
month_day_last
constexpr auto operator/(const std::chrono::month& m,

                         std::chrono::last_spec) noexcept

    -> std::chrono::month_day_last;
(8) (C++20 起)
constexpr auto operator/(int m, std::chrono::last_spec) noexcept
    -> std::chrono::month_day_last;
(9) (C++20 起)
constexpr auto operator/(std::chrono::last_spec,

                         const std::chrono::month& m) noexcept

    -> std::chrono::month_day_last;
(10) (C++20 起)
constexpr auto operator/(std::chrono::last_spec, int m) noexcept
    -> std::chrono::month_day_last;
(11) (C++20 起)
month_weekday
constexpr auto operator/(const std::chrono::month& m,

                         const std::chrono::weekday_indexed& wdi) noexcept

    -> std::chrono::month_weekday;
(12) (C++20 起)
constexpr auto operator/(int m, const std::chrono::weekday_indexed& wdi) noexcept
    -> std::chrono::month_weekday;
(13) (C++20 起)
constexpr auto operator/(const std::chrono::weekday_indexed& wdi,

                         const std::chrono::month& m) noexcept

    -> std::chrono::month_weekday;
(14) (C++20 起)
constexpr auto operator/(const std::chrono::weekday_indexed& wdi, int m) noexcept
    -> std::chrono::month_weekday;
(15) (C++20 起)
month_weekday_last
constexpr auto operator/(const std::chrono::month& m,

                         const std::chrono::weekday_last& wdl) noexcept

    -> std::chrono::month_weekday_last;
(16) (C++20 起)
constexpr auto operator/(int m, const std::chrono::weekday_last& wdl) noexcept
    -> std::chrono::month_weekday_last;
(17) (C++20 起)
constexpr auto operator/(const std::chrono::weekday_last& wdl,

                         const std::chrono::month& m) noexcept

    -> std::chrono::month_weekday_last;
(18) (C++20 起)
constexpr auto operator/(const std::chrono::weekday_last& wdl, int m) noexcept
    -> std::chrono::month_weekday_last;
(19) (C++20 起)
year_month_day
constexpr auto operator/(const std::chrono::year_month& ym,

                         const std::chrono::day& d) noexcept

    -> std::chrono::year_month_day;
(20) (C++20 起)
constexpr auto operator/(const std::chrono::year_month& ym, int d) noexcept
    -> std::chrono::year_month_day;
(21) (C++20 起)
constexpr auto operator/(const std::chrono::year& y,

                         const std::chrono::month_day& md) noexcept

    -> std::chrono::year_month_day;
(22) (C++20 起)
constexpr auto operator/(int y, const std::chrono::month_day& md) noexcept
    -> std::chrono::year_month_day;
(23) (C++20 起)
constexpr auto operator/(const std::chrono::month_day& md,

                         const std::chrono::year& y) noexcept

    -> std::chrono::year_month_day;
(24) (C++20 起)
constexpr auto operator/(const std::chrono::month_day& md, int y) noexcept
    -> std::chrono::year_month_day;
(25) (C++20 起)
year_month_day_last
constexpr auto operator/(const std::chrono::year_month& ym,

                         std::chrono::last_spec) noexcept

    -> std::chrono::year_month_day_last;
(26) (C++20 起)
constexpr auto operator/(const std::chrono::year& y,

                         const std::chrono::month_day_last& mdl) noexcept

    -> std::chrono::year_month_day_last;
(27) (C++20 起)
constexpr auto operator/(int y, const std::chrono::month_day_last& mdl) noexcept
    -> std::chrono::year_month_day_last;
(28) (C++20 起)
constexpr auto operator/(const std::chrono::month_day_last& mdl,

                         const std::chrono::year& y) noexcept

    -> std::chrono::year_month_day_last;
(29) (C++20 起)
constexpr auto operator/(const std::chrono::month_day_last& mdl, int y) noexcept
    -> std::chrono::year_month_day_last;
(30) (C++20 起)
year_month_weekday
constexpr auto operator/(const std::chrono::year_month& ym,

                         const std::chrono::weekday_indexed& wdi) noexcept

    -> std::chrono::year_month_weekday;
(31) (C++20 起)
constexpr auto operator/(const std::chrono::year& y,

                         const std::chrono::month_weekday& mwd) noexcept

    -> std::chrono::year_month_weekday;
(32) (C++20 起)
constexpr auto operator/(int y, const std::chrono::month_weekday& mwd) noexcept
    -> std::chrono::year_month_weekday;
(33) (C++20 起)
constexpr auto operator/(const std::chrono::month_weekday& mwd,

                         const std::chrono::year& y) noexcept

    -> std::chrono::year_month_weekday;
(34) (C++20 起)
constexpr auto operator/(const std::chrono::month_weekday& mwd, int y) noexcept
    -> std::chrono::year_month_weekday;
(35) (C++20 起)
year_month_weekday_last
constexpr auto operator/(const std::chrono::year_month& ym,

                         const std::chrono::weekday_last& wdl) noexcept

    -> std::chrono::year_month_weekday_last;
(36) (C++20 起)
constexpr auto operator/(const std::chrono::year& y,

                         const std::chrono::month_weekday_last& mwdl) noexcept

    -> std::chrono::year_month_weekday_last;
(37) (C++20 起)
constexpr auto operator/(int y, const std::chrono::month_weekday_last& mwdl) noexcept
    -> std::chrono::year_month_weekday_last;
(38) (C++20 起)
constexpr auto operator/(const std::chrono::month_weekday_last& mwdl,

                         const std::chrono::year& y) noexcept

    -> std::chrono::year_month_weekday_last;
(39) (C++20 起)
constexpr auto operator/(const std::chrono::month_weekday_last& mwdl, int y) noexcept
    -> std::chrono::year_month_weekday_last;
(40) (C++20 起)

这些 operator/ 为创建格里高利历日期提供约定语法。

对于创建完整日期,任何下列三种顺序都可接受:

  • year/month/day
  • month/day/year
  • day/month/year

每种情况中,能以下列之一替换 day

  • std::chrono::last ,对于月的最后一日;
  • weekday[i] ,对于月的第 iweekday
  • weekday[std::chrono::last] ,对于该月的最后一个 weekday

若单纯的整数的含义相对于其他运算数的类型无歧义,则可接受它:允许 2005y/4/5 ,但不允许 5/April/2005

能够不以三种顺序之一应用第二个 operator/ 创建部分日期类型( year_monthmonth_day 等)。

返回值

1) std::chrono::year_month(y, m)
2) std::chrono::year_month(y, std::chrono::month(m))
3,6) std::chrono::month_day(m, d)
4) std::chrono::month_day(m, std::chrono::day(d))
5,7) std::chrono::month_day(std::chrono::month(m), d)
8,10) std::chrono::month_day_last(m)
9,11) std::chrono::month_day_last(std::chrono::month(m))
12,14) std::chrono::month_weekday(m, wdi)
13,15) std::chrono::month_weekday(std::chrono::month(m), wdi)
16,18) std::chrono::month_weekday_last(m, wdl)
17,19) std::chrono::month_weekday_last(std::chrono::month(m), wdl)
20) std::chrono::year_month_day(ym.year(), ym.month(), d)
21) std::chrono::year_month_day(ym.year(), ym.month(), std::chrono::day(d))
22,24) std::chrono::year_month_day(y, md.month(), md.day())
23,25) std::chrono::year_month_day(std::chrono::year(y), md.month(), md.day())
26) std::chrono::year_month_day_last(ym.year(), std::chrono::month_day_last(ym.month()))
27,29) std::chrono::year_month_day_last(y, mdl)
28,30) std::chrono::year_month_day_last(std::chrono::year(y), mdl)
31) std::chrono::year_month_weekday(ym.year(), ym.month(), wdi)
32,34) std::chrono::year_month_weekday(y, mwd.month(), mwd.weekday_indexed())
33,35) std::chrono::year_month_weekday(std::chrono::year(y), mwd.month(), mwd.weekday_indexed())
36) std::chrono::year_month_weekday_last(ym.year(), ym.month(), wdl)
37,39) std::chrono::year_month_weekday_last(y, mwdl.month(), mwdl.weekday_last())
38,40) std::chrono::year_month_weekday_last(std::chrono::year(y), mwdl.month(), mwdl.weekday_last())

示例

#include <iostream>
#include <chrono>
using namespace std::chrono;
 
int main()
{
    std::cout << std::boolalpha;
 
    // 标准提供 2021y 作为 std::chrono::year(2021) 的备选
    // 标准提供 15d 作为 std::chrono::day(15) 的备选
 
    constexpr auto ym {year(2021)/8};
    std::cout << (ym == year_month(year(2021), August)) << ' ';
 
    constexpr auto md {9/day(15)};
    std::cout << (md == month_day(September, day(15))) << ' ';
 
    constexpr auto mdl {October/last};
    std::cout << (mdl == month_day_last(month(10))) << ' ';
 
    constexpr auto mw {11/Monday[3]};
    std::cout << (mw == month_weekday(November, Monday[3])) << ' ';
 
    constexpr auto mwdl {December/Sunday[last]};
    std::cout << (mwdl == month_weekday_last(month(12), weekday_last(Sunday))) << ' ';
 
    constexpr auto ymd {year(2021)/January/day(23)};
    std::cout << (ymd == year_month_day(2021y, month(January), 23d)) << '\n';
}

输出:

true true true true true true