std::chrono::weekday::weekday
来自cppreference.com
weekday() = default; |
(1) | (C++20 起) |
constexpr explicit weekday( unsigned wd ) noexcept; |
(2) | (C++20 起) |
constexpr weekday( const std::chrono::sys_days& sd ) noexcept; |
(3) | (C++20 起) |
constexpr explicit weekday( const std::chrono::local_days& ld ) noexcept; |
(4) | (C++20 起) |
构造 weekday
对象。
1) 默认构造函数保留星期之日值未初始化。
2) 构造保有星期之日值 wd 的
weekday
对象。若 wd == 7,则保有的值为 0。若 wd > 255,则保有的值未指定。3) 构造表示 sd 所对应星期之日的
weekday
。此构造函数定义从 std::chrono::sys_days 到 weekday
的隐式转换。参数
wd | - | 星期之日值 |
sd | - | 系统日期对象 |
ld | - | 本地日期对象 |
示例
运行此代码
#include <chrono> #include <iomanip> #include <iostream> int main() { constexpr std::chrono::weekday friday{5}; // 使用重载 (2) static_assert(friday == std::chrono::Friday); for (int y{2020}; y <= 2024; ++y) { const std::chrono::year cur_year{y}; for (int cur_month{1}; cur_month != 13; ++cur_month) { const std::chrono::year_month_day ymd{cur_year/cur_month/13}; const std::chrono::weekday cur_weekday{std::chrono::sys_days(ymd)}; // (3) if (cur_weekday == friday) std::cout << ymd << " is " << friday << '\n'; } } }
输出:
2020-03-13 is Fri 2020-11-13 is Fri 2021-08-13 is Fri 2022-05-13 is Fri 2023-01-13 is Fri 2023-10-13 is Fri 2024-09-13 is Fri 2024-12-13 is Fri