year_month_weekday class

Represents a specific year, month, and nth weekday of the month.

Syntax

class year_month_weekday; // C++20

Remarks

year_month_weekday supports years- and months-oriented arithmetic, but not days-oriented arithmetic. For days-oriented arithmetic, use the sys_days conversion to convert to a sys_days, which supports days-oriented arithmetic.

year_month_weekday is a trivially copyable and standard-layout class type.

Members

Name Description
Constructor Construct a year_month_weekday with the specified month and weekday.
index Get the index of the weekday.
month Get the month value.
ok Check if the year_month_weekday is valid.
operator+= Add the specified number of months or years.
operator-= Subtract the specified number of months or years.
operator local_days Get the count of days from the system_clock epoch to this year_month_weekday as local_days.
operator sys_days Get the count of days from the system_clock epoch to this year_month_weekday as sys_days.
weekday Get the weekday.
weekday_indexed Get the [weekday_indexed] stored in this year_month_weekday.
year Get the year.

Non-members

Name Description
operator+ Add months or years.
operator- Subtract months or years.
operator== Determine whether two year_month_weekday values are equal.
operator<< Output a year_month_weekday to the given stream.

Requirements

Header: <chrono> (since C++20)

Namespace: std::chrono

Compiler Option: /std:c++latest

Constructor

Constructs a 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;

Parameters

m
The month value.

dp
A sys_days or local_days

wdi
The weekday value.

y
The year value.

Remarks: Constructor

1) The default constructor doesn't initialize any of the fields.

2) Constructs a year_month_weekday that corresponds to the specified year, month, and weekday_indexed.

3) Constructs a year_month_weekday that corresponds to the date represented by sys_days{dp.time_since_epoch()}.

4) Constructs a year_month_weekday that corresponds to the date represented by dp. For any year_month_weekday (ymdl) for which ok() is true, comparison with operator== to year_month_weekday{sys_days{ymdl}} will be true.

For information about C++20 syntax used to specify dates, see operator/

Example: Create a 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

Get the week index of the weekday in this year_month_weekday.

constexpr unsigned index() const noexcept;

Return value

The index of the weekday. For example, if the weekday was the first Wednesday of the week, the index would be 1.

month

Get the month value.

constexpr month month() const noexcept;

Return value

The month value.

ok

Check if the value stored in this year_month_weekday is valid. The year, month, and weekday_index stored in this year_month_weekday must all be ok for this function to return true. Otherwise, returns false.

constexpr bool ok() const noexcept;

Return value

true if the year_month_weekday value is valid. Otherwise, false.
A year_month_weekday is valid if both the month is valid and the weekday_indexed value is valid.

operator+=

Add months or years to this year_month_weekday.

1) constexpr year_month_weekday& operator+=(const months& m) noexcept;
2) constexpr year_month_weekday& operator+=(const years& y) noexcept;

Parameters

m
The number of months to add.

y
The number of years to add.

Return value

*this which reflects the result of the addition.

Example: 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-=

Subtract months or years from this year_month_weekday.

1) constexpr year_month_weekday& operator-=(const months& m) noexcept;
2) constexpr year_month_weekday& operator-=(const years& y) noexcept;

Parameters

m
The number of months to subtract.

y
The number of years to subtract.

Return value

*this which reflects the result of the subtraction.

Example: 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

Get the count of days from the system_clock epoch (1/1/1970) to this year_month_weekday as local_days

constexpr explicit operator local_days() const noexcept;

Return value

If ok(), returns a count of days as local_days{sys_days{*this}.time_since_epoch()}. Otherwise the returned value is unspecified.

operator sys_days

Get the count of days from the system_clock epoch (1/1/1970) to this year_month_day as sys_days.

constexpr operator sys_days() const noexcept;

Return value

If ok(), returns a sys_days that represents the date that is (index() - 1) * 7 days after the first weekday() of year()/month(). If index() is 0, the returned sys_days represents the date 7 days before the first weekday() of year()/month().

weekday

Get the weekday stored in the weekday_indexed stored in this year_month_weekday.

constexpr weekday weekday() const noexcept;

Return value

The weekday value.

weekday_indexed

Get the weekday_indexed stored in this year_month_weekday.

constexpr weekday_indexed weekday_indexed() const noexcept;

Return value

The weekday_indexed value.

year

Get the year value.

constexpr year year() const noexcept;

Return value

The year value.

See also

<chrono>
year
year_month
year_month_day
year_month_day_last
year_month_weekday_last
operator/ Header Files Reference