Files
2025-10-25 03:02:53 +03:00

12 KiB
Raw Permalink Blame History

[time.cal.ym]

30 Time library [time]

30.8 The civil calendar [time.cal]

30.8.13 Class year_month [time.cal.ym]

30.8.13.1 Overview [time.cal.ym.overview]

namespace std::chrono {class year_month { chrono::year y_; // exposition only chrono::month m_; // exposition onlypublic: year_month() = default; constexpr year_month(const chrono::year& y, const chrono::month& m) noexcept; constexpr chrono::year year() const noexcept; constexpr chrono::month month() const noexcept; constexpr year_month& operator+=(const months& dm) noexcept; constexpr year_month& operator-=(const months& dm) noexcept; constexpr year_month& operator+=(const years& dy) noexcept; constexpr year_month& operator-=(const years& dy) noexcept; constexpr bool ok() const noexcept; };}

1

#

year_month represents a specific month of a specific year, but with an unspecified day.

year_month is a field-based time point with a resolution of months.

year_month meets the Cpp17EqualityComparable (Table 28) and Cpp17LessThanComparable (Table 29) requirements.

2

#

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

30.8.13.2 Member functions [time.cal.ym.members]

🔗

constexpr year_month(const chrono::year& y, const chrono::month& m) noexcept;

1

#

Effects: Initializes y_ with y, and m_ with m.

🔗

constexpr chrono::year year() const noexcept;

2

#

Returns: y_.

🔗

constexpr chrono::month month() const noexcept;

3

#

Returns: m_.

🔗

constexpr year_month& operator+=(const months& dm) noexcept;

4

#

Constraints: If the argument supplied by the caller for the months parameter is convertible to years, its implicit conversion sequence to years is worse than its implicit conversion sequence tomonths ([over.ics.rank]).

5

#

Effects: *this = *this + dm.

6

#

Returns: *this.

🔗

constexpr year_month& operator-=(const months& dm) noexcept;

7

#

Constraints: If the argument supplied by the caller for the months parameter is convertible to years, its implicit conversion sequence to years is worse than its implicit conversion sequence tomonths ([over.ics.rank]).

8

#

Effects: *this = *this - dm.

9

#

Returns: *this.

🔗

constexpr year_month& operator+=(const years& dy) noexcept;

10

#

Effects: *this = *this + dy.

11

#

Returns: *this.

🔗

constexpr year_month& operator-=(const years& dy) noexcept;

12

#

Effects: *this = *this - dy.

13

#

Returns: *this.

🔗

constexpr bool ok() const noexcept;

14

#

Returns: y_.ok() && m_.ok().

30.8.13.3 Non-member functions [time.cal.ym.nonmembers]

🔗

constexpr bool operator==(const year_month& x, const year_month& y) noexcept;

1

#

Returns: x.year() == y.year() && x.month() == y.month().

🔗

constexpr strong_ordering operator<=>(const year_month& x, const year_month& y) noexcept;

2

#

Effects: Equivalent to:if (auto c = x.year() <=> y.year(); c != 0) return c;return x.month() <=> y.month();

🔗

constexpr year_month operator+(const year_month& ym, const months& dm) noexcept;

3

#

Constraints: If the argument supplied by the caller for the months parameter is convertible to years, its implicit conversion sequence to years is worse than its implicit conversion sequence tomonths ([over.ics.rank]).

4

#

Returns: A year_month value z such that z.ok() && z - ym == dm is true.

5

#

Complexity: O(1) with respect to the value of dm.

🔗

constexpr year_month operator+(const months& dm, const year_month& ym) noexcept;

6

#

Constraints: If the argument supplied by the caller for the months parameter is convertible to years, its implicit conversion sequence to years is worse than its implicit conversion sequence tomonths ([over.ics.rank]).

7

#

Returns: ym + dm.

🔗

constexpr year_month operator-(const year_month& ym, const months& dm) noexcept;

8

#

Constraints: If the argument supplied by the caller for the months parameter is convertible to years, its implicit conversion sequence to years is worse than its implicit conversion sequence tomonths ([over.ics.rank]).

9

#

Returns: ym + -dm.

🔗

constexpr months operator-(const year_month& x, const year_month& y) noexcept;

10

#

Returns: x.year() - y.year() + months{static_cast(unsigned{x.month()}) -static_cast(unsigned{y.month()})}

🔗

constexpr year_month operator+(const year_month& ym, const years& dy) noexcept;

11

#

Returns: (ym.year() + dy) / ym.month().

🔗

constexpr year_month operator+(const years& dy, const year_month& ym) noexcept;

12

#

Returns: ym + dy.

🔗

constexpr year_month operator-(const year_month& ym, const years& dy) noexcept;

13

#

Returns: ym + -dy.

🔗

template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const year_month& ym);

14

#

Effects: Equivalent to:return os << format(os.getloc(), STATICALLY-WIDEN("{}/{:L}"), ym.year(), ym.month());

🔗

template<class charT, class traits, class Alloc = allocator<charT>> basic_istream<charT, traits>& from_stream(basic_istream<charT, traits>& is, const charT* fmt, year_month& ym, basic_string<charT, traits, Alloc>* abbrev = nullptr, minutes* offset = nullptr);

15

#

Effects: Attempts to parse the input stream is into the year_month ym using the format flags given in the NTCTS fmt as specified in [time.parse].

If the parse fails to decode a valid year_month,is.setstate(ios_base::failbit) is called andym is not modified.

If %Z is used and successfully parsed, that value will be assigned to *abbrev if abbrev is non-null.

If %z (or a modified variant) is used and successfully parsed, that value will be assigned to *offset if offset is non-null.

16

#

Returns: is.