Muokkaa

Jaa


day class

Represents a day of the month. For example, the 25th day of the month.

Syntax

class day; // C++20

Remarks

A day normally holds values in the range [1, 31]. It may hold non-negative values outside this range, but the behavior is unspecified if it isn't within the range [0, 255].

Members

Name Description
Constructors Construct a day.
ok Verify that the day value is in the valid range [1,31].
operator++ Increment this day.
operator+= Add the specified number of days to this day.
operator-- Decrement the day.
operator-= Subtract the specified number of days from this day
operator unsigned Get the day value as an unsigned integer.

Non-members

Name Description
from_stream Parse a day from the given stream using the specified format.
operator+ Add specified number of days to this day, returning a new day object.
operator- Subtract the specified number of days from this day, returning a new day object.
operator== Determine whether two day instances are equal.
operator<=> Compare this day against another day. The >, >=, <=, <, != operators are synthesized by the compiler.
operator<< Output a day to the given stream.
operator""d Create a day literal for a day in the month.

Requirements

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

Namespace: std::chrono

Compiler Option: /std:c++latest

Constructors

Constructs a day.

1) day() = default;
2) explicit constexpr day(unsigned d) noexcept;

Parameters

d
Construct a day with value d.

Remarks

1) The default constructor doesn't initialize the day value.
2) Construct a day with the day value initialized to d.

Example: Create a day

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

using namespace std::chrono;

int main()
{
    day d{1}; // day 1
    day d2 = 2d; // day 2

    std::cout << d << ", " << d2;

    return 0;
}
01, 02

ok

Check if the value stored in this day is in the valid range.

constexpr bool ok() const noexcept;

Return value

true if the day value is in the range [1,31]. Otherwise, false.

operator++

Add 1 to the day value.

1) constexpr std::chrono::day& operator++() noexcept;
2) constexpr std::chrono::day operator++(int) noexcept;

Return value

1) A reference to *this day after it has been incremented (a postfix increment).
2) A copy of the day, before it has been incremented (a prefix increment).

Example: operator++

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

using namespace std::chrono;

int main()
{
    day d{1};

    std::cout << d << " " << ++d << "\n"; // constexpr day& operator++() noexcept
    std::cout << d << " " << d++ << "\n"; // constexpr day operator++(int) noexcept
    std::cout << d;

    return 0;
}
01 02
02 02
03

Remarks

If the incremented result is out of the range [0, 255], then the stored value is unspecified.

operator--

Subtract 1 from the day value.

1) constexpr std::chrono::day& operator--() noexcept;
2) constexpr std::chrono::day operator--(int) noexcept;

Return value

1) A reference to *this day after it has been decremented (a postfix decrement).
2) A copy of the *day, before it has been decremented (a prefix decrement).

Example: operator--

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

int main()
{
    day d{5};
    std::cout << d << " " << --d << "\n"; // constexpr day& operator--() noexcept
    std::cout << d << " " << d-- << "\n"; // constexpr day operator--(int) noexcept
    std::cout << d << "\n";

    return 0;
}
05 04
04 04
03

Remarks

If the decremented result is out of the range [0, 255], then the stored value is unspecified.

operator+=

Add days to this day.

constexpr day& operator+=(const days& d) noexcept;

Parameters

d
The number of days to add.

Return value

*this

If the result is outside the range [0, 255], then the stored value is unspecified.

operator-=

Subtract days from this day.

constexpr day& operator-=(const days& d) noexcept;

Parameters

d
The number of days to subtract.

Return value

*this. If the result is outside the range [0, 255], then the stored value is unspecified.

operator unsigned

Get the day value.

explicit constexpr operator unsigned() const noexcept;

Return value

The value of the day

Example: operator unsigned()

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

int main()
{
    chrono::day d{14d};
    unsigned dayValue = static_cast<unsigned>(d);
    cout << dayValue << "\n";
    
    return 0;
}
14

See also

<chrono>
month_day class
month_day_last class
year_month_day
year_month_day_last
Header Files Reference