year_month_day
class
Represents a month, year, and day.
Syntax
class year_month_day; // C++20
Members
Name | Description |
---|---|
Constructors | Construct a year_month_day |
day |
Returns the day. |
month |
Returns the month. |
ok |
Verify that the year and month values are in the valid range. |
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_day as local_days . |
operator sys_days |
Get the count of days from the system_clock epoch to this year_month_day as sys_days . |
year |
Returns the year. |
Non-members
Name | Description |
---|---|
from_stream |
Parse a year_month_day from a stream using the specified format |
operator+ |
Add months or years. |
operator- |
Subtract months or years. |
operator== |
Determine whether two year_month_day values are equal. |
operator<=> |
Compare two year_month_day values. The >, >=, <=, <, != operators are synthesized by the compiler. |
operator<< |
Output a year_month_day to a stream. |
Requirements
Header: <chrono>
(since C++20)
Namespace: std::chrono
Compiler Option: /std:c++latest
Constructors
Construct a year_month_day
.
1) year_month_day() = default;
2) constexpr year_month_day(const year& y, const month& m, day& d) noexcept;
3) constexpr year_month_day(const year_month_day_last& ymdl) noexcept;
4) constexpr year_month_day(const sys_days& dp) noexcept;
5) constexpr explicit year_month_day(const local_days& dp) noexcept;
Parameters
d
A day
value.
dp
A sys_days
or local_days
value.
m
A month
value.
y
A year
value.
ymdl
A year_month_day_last
value.
Remarks
1) The default constructor doesn't initialize the month or day.
2) Constructs a year_month_day
with the specified year, month, and day.
3) Constructs a year_month_day
with the specified year, month, and day from ymdl
4) Constructs a year_month_day
with the same date as dp
.
5) Constructs a year_month_day
with the same date as dp
but as though constructed by year_month_day(sys_days(dp.time_since_epoch()))
.
For information about C++20 syntax used to specify dates, see operator/
Example: Create a year_month_day
// compile using: /std:c++latest
#include <iostream>
#include <chrono>
using namespace std::chrono;
int main()
{
year_month_day ymd{ April / 4 / 1975 };
std::cout << ymd;
return 0;
}
1975-04-04
day
Get the day.
constexpr day day() const noexcept;
Return value
The day
value.
month
Get the month.
constexpr month month() const noexcept;
Return value
The month
value.
operator local_days
Get the count of days from the system_clock
epoch (1/1/1970) to this year_month_day
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()}
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
holding a count of days from the sys_days
epoch (1/1/1970) to the date held in this year_month_day
. The value will be negative if the date in this year_month_day
is prior to the sys_days
epoch.
If the year and month in this year_month_day
are ok()
, returns sys_days{year/month/1d} + (day-1d)
. Otherwise the value returned is unspecified.
A sys_days
in the range [days{-12687428}
, days{11248737}
] can be converted to a year_month_day
and back and have have the same value.
year
Get the year.
constexpr year year() const noexcept;
Return value
The year
.
ok
Check if the year and month value stored in this year_month_day
are both in the valid range. Ensures that the day is in the range [1d, (y/m/last).day()], accounting for leap years and the different number of days in each month.
constexpr bool ok() const noexcept;
Return value
true
if the year_month_day
year, month, and day values are in the valid range. Otherwise, false
.
operator+=
Add months or years to this year_month_day
.
1) constexpr year_month_day& operator+=(const months& dm) noexcept;
2) constexpr year_month_day& operator+=(const years& dy) noexcept;
Parameters
dm
The number of months to add.
dy
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_day ymd{June / 1d / 2021y};
std::cout << ymd << '\n';
ymd += months{2};
ymd += years{1};
std::cout << ymd;
return 0;
}
2021-06-01
2022-08-01
operator-=
Subtract months or years from this year_month_day
.
1) constexpr year_month_day& operator-=(const months& dm) noexcept;
2) constexpr year_month_day& operator-=(const years& dy) noexcept;
Parameters
dm
The number of months to subtract.
dy
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 ymd{June / 1d / 2021y};
std::cout << ymd << '\n';
ymd -= months{2};
ymd -= years{1};
std::cout << ymd;
return 0;
}
2021-06-01
2020-04-01
See also
<chrono>
year
year_month
year_month_day_last
year_month_weekday
year_month_weekday_last
operator/
Header Files Reference