表示月份的特定年份、月份和第 n 个工作日。
语法
class year_month_weekday; // C++20
注解
year_month_weekday 支持面向年和月的算术,但不支持面向天数的算术。 对于面向天数的算术,请使用 sys_days 转换转换为支持日导向算术的 a sys_days。
year_month_weekday 是一种可完全复制的标准布局类类型。
成员
| 名称 | DESCRIPTION |
|---|---|
Constructor |
使用指定的月份和工作日构造 a year_month_weekday 。 |
index |
获取工作日的索引。 |
month |
获取月份值。 |
ok |
检查 year_month_weekday 是否有效。 |
operator+= |
添加指定的月数或年数。 |
operator-= |
减去指定的月数或年数。 |
operator local_days |
获取从system_clock纪元到此year_month_weekdaylocal_days日期的天数。 |
operator sys_days |
获取从system_clock纪元到此year_month_weekdaysys_days日期的天数。 |
weekday |
获取工作日。 |
weekday_indexed |
weekday_indexed获取存储在此year_month_weekday文件中。 |
year |
获取年份。 |
非成员
| 名称 | DESCRIPTION |
|---|---|
operator+ |
添加月或年。 |
operator- |
减去月份或年份。 |
operator== |
确定两 year_month_weekday 个值是否相等。 |
operator<< |
将 year_month_weekday 输出到给定流。 |
要求
标头:<chrono> (自C++20以来)
命名空间:std::chrono
编译器选项:/std:c++latest
构造函数
构造一个 year_month_weekday。
// 1)
year_month_weekday() = default
// 2)
constexpr year_month_weekday(const year& y, const month& m, const weekday_indexed& wdi) noexcept;
// 3)
constexpr explicit year_month_weekday(const local_days& dp) noexcept;
// 4)
constexpr year_month_weekday(const sys_days& dp) noexcept;
参数
m
month 值。
dp
A sys_days 或 local_days
wdi
weekday 值。
y
year 值。
注解:构造函数
1) 默认构造函数不会初始化任何字段。
2) 构造 year_month_weekday 与指定 year、 month和 weekday_indexed.
3) 构造与所sys_days{dp.time_since_epoch()}表示日期相对应的日期year_month_weekday。
4) 构造与所dp表示日期相对应的日期year_month_weekday。 对于任何year_month_weekday(ymdl),ok()true与operator==year_month_weekday{sys_days{ymdl}}它相比将是true。
有关用于指定日期的 C++20 语法的信息,请参阅 operator/
示例:创建 year_month_weekday
// compile using: /std:c++latest
#include <iostream>
#include <chrono>
using namespace std::chrono;
int main()
{
year_month_weekday ymw{1997y / January / Wednesday[1]};
std::cout << ymw << '\n';
return 0;
}
1997/Jan/Wed[1]
index
获取此 year_month_weekday工作日的周索引。
constexpr unsigned index() const noexcept;
返回值
工作日的索引。 例如,如果工作日是周中的第一个星期三,则索引将为 1。
month
获取月份值。
constexpr month month() const noexcept;
返回值
month 值。
ok
检查存储在此值 year_month_weekday 中的值是否有效。
month并且yearweekday_index存储在此year_month_weekday函数中必须全部ok返回true。 否则返回 false。
constexpr bool ok() const noexcept;
返回值
false。
如果两者均month有效且值有效,weekday_indexed则 A year_month_weekday 有效。
operator+=
为此 year_month_weekday添加月份或年份。
1) constexpr year_month_weekday& operator+=(const months& m) noexcept;
2) constexpr year_month_weekday& operator+=(const years& y) noexcept;
参数
m
要添加的月份数。
y
要添加的年数。
返回值
*this 这反映了添加的结果。
示例:operator+=
// compile using: /std:c++latest
#include <iostream>
#include <chrono>
using namespace std::chrono;
int main()
{
year_month_weekday ymw{1997y / January / Wednesday[1]};
std::cout << ymw << '\n';
ymw += months{1};
ymw += years{1};
std::cout << ymw << '\n';
return 0;
}
1997/Jan/Wed[1]
1998/Feb/Wed[1]
operator-=
从这 year_month_weekday一点中减去几个月或几年。
1) constexpr year_month_weekday& operator-=(const months& m) noexcept;
2) constexpr year_month_weekday& operator-=(const years& y) noexcept;
参数
m
要减去的月数。
y
要减去的年数。
返回值
*this 这反映了减法的结果。
示例:operator-=
// compile using: /std:c++latest
#include <iostream>
#include <chrono>
using namespace std::chrono;
int main()
{
year_month_weekday ymw{1997y / January / Wednesday[1]};
std::cout << ymw << '\n';
ymw -= months{1};
ymw -= years{1};
std::cout << ymw << '\n';
return 0;
}
1997/Jan/Wed[1]
1995/Dec/Wed[1]
operator local_days
从纪元(1/1/1/1)到这个year_month_weekday日期的计数system_clocklocal_days
constexpr explicit operator local_days() const noexcept;
返回值
如果ok()返回天数,则返回天数。local_days{sys_days{*this}.time_since_epoch()} 否则,返回的值未指定。
operator sys_days
从纪元(1/1/1/1)到这个year_month_day日期的计数system_clock。sys_days
constexpr operator sys_days() const noexcept;
返回值
如果ok()返回一个值,则返回一个表示第(index() - 1) * 7一sys_daysweekday()year()/month()个日期之后的日期。
0如果是index(),则返回sys_days的表示第一weekday()year()/month()天之前的日期 7 天。
weekday
weekday获取存储在此year_month_weekday文件中weekday_indexed的存储。
constexpr weekday weekday() const noexcept;
返回值
weekday 值。
weekday_indexed
weekday_indexed获取存储在此year_month_weekday文件中。
constexpr weekday_indexed weekday_indexed() const noexcept;
返回值
year
获取年份值。
constexpr year year() const noexcept;
返回值
year 值。
另请参阅
<chrono>
year
year_month
year_month_day
year_month_day_last
year_month_weekday_last
operator/
头文件引用