year_month_day_last class

Represents the last day of a specific year and month.

year_month_day_last supports month arithmetic and years arithmetic, but not days arithmetic. To do days-oriented arithmetic, convert the year_month_day_last to a sys_days.

Syntax

class year_month_day_last; // C++20

Members

Name Description
Constructors Construct a year_month_day_last
day Get the last day of the month/year.
month Get the month.
month_day_last Get the month_day_last value stored in this year_month_day_last.
ok Verify that the year and month values are in the valid range.
operator+= Add months or years to this year_month_day_last.
operator-= Subtract months or years from this year_month_day_last.
operator local_days Get the count of days from the sys_days epoch to this year_month_day_last as local_days.
operator sys_days Get the count of days from the sys_days epoch to this year_month_day_last as sys_days.
year Get the year.

Non-members

Name Description
operator+ Add months or years.
operator- Subtract months or years.
operator== Determine whether two year_month_day_last values are equal.
operator<=> Compare two year_month_day_last values. The >, >=, <=, <, != operators are synthesized by the compiler.
operator<< Output a year_month_day_last to a stream.

Requirements

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

Namespace: std::chrono

Compiler Option: /std:c++latest

Constructors

Construct a year_month_day_last for a specified month and year.

constexpr year_month_day_last(const year& y, const month_day_last& mdl) noexcept;

Parameters

mdl The month value from the month_day_last is stored in the constructed year_month_day_last.

y
The year value is stored in the constructed year_month_day_last.

Remarks

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

Example: Create a year_month_day_last

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month_day_last ymdl{ April / last / 1975 };
    std::cout << ymdl;

    return 0;
}
1975/Apr/last

day

If ok() is true, returns a day representing the last day of the year, month represented by this year_month_day_last.

constexpr day day() const noexcept;

Return value

If ok() is true, returns a day representing the last day of the year and month represented by *this. Otherwise, the return value is unspecified.

local_days

Get the month, day, and year in this year_month_day_last as a count of days.

constexpr explicit operator local_days() const noexcept;

Return value

local_days{sys_days{*this}.time_since_epoch()}

Example

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month_day_last ymdl{ June / last / 2021 };
    auto ld = local_days(ymdl);
    std::cout << ld.time_since_epoch();

    return 0;
}
18808d

month

Get the stored month value.

constexpr month month() const noexcept;

Return value

The month value.

month_day_last

Gets the month_day_last value stored in this year_month_day_last.

constexpr month_day_last month_day_last() const noexcept;

Return value

The month_day_last value stored in this year_month_day_last.

operator sys_days

Get the month, day, and year in this year_month_day_last as a count of days from the epoch for the system clock.

constexpr explicit operator sys_days() const noexcept;

Return value

Returns sys_days{this->year()/this->month()/this->day()}

Example

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month_day_last ymdl{ June / last / 2021 };
    auto sd = sys_days(ymdl);
    std::cout << sd.time_since_epoch();

    return 0;
}
18808d

year

Get the year.

constexpr year year() const noexcept;

Return value

The year.

ok

Check if the year and month_day_last value stored in this year_month_day_last are both in the valid range.

constexpr bool ok() const noexcept;

Return value

true if the year and month_day_last value stored in this year_month_day_last are both in the valid range. Otherwise, false.

operator+=

Add months or years to this year_month_day_last.

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

Parameters

m
The number of months to add.

y
The numbers 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_day_last ymdl{ April / last / 1975 };

    std::cout << ymdl << '\n';

    ymdl += months{1};
    ymdl += years{1};

    std::cout << ymdl;
    
    return 0;
}
1975/Apr/last
1976/May/last

operator-=

Subtract months or years from this year_month_day_last.

1) constexpr year_month_day_last& operator-=(const months& m) noexcept;
2) constexpr year_month_day_last& 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_day_last ymdl{ April / last / 1975 };

    std::cout << ymdl << '\n';

    ymdl -= months{1};
    ymdl -= years{1};

    std::cout << ymdl;
    
    return 0;
}
1975/Apr/last
1974/Mar/last

See also

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