Path: blob/main/contrib/llvm-project/libcxx/include/__chrono/year_month.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___CHRONO_YEAR_MONTH_H10#define _LIBCPP___CHRONO_YEAR_MONTH_H1112#include <__chrono/duration.h>13#include <__chrono/month.h>14#include <__chrono/year.h>15#include <__config>16#include <compare>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122#if _LIBCPP_STD_VER >= 202324_LIBCPP_BEGIN_NAMESPACE_STD2526namespace chrono {2728class year_month {29chrono::year __y_;30chrono::month __m_;3132public:33year_month() = default;34_LIBCPP_HIDE_FROM_ABI constexpr year_month(const chrono::year& __yval, const chrono::month& __mval) noexcept35: __y_{__yval}, __m_{__mval} {}36_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }37_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }38_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const months& __dm) noexcept;39_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const months& __dm) noexcept;40_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const years& __dy) noexcept;41_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const years& __dy) noexcept;42_LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok(); }43};4445_LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, const month& __m) noexcept {46return year_month{__y, __m};47}4849_LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, int __m) noexcept {50return year_month{__y, month(__m)};51}5253_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept {54return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month();55}5657_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering58operator<=>(const year_month& __lhs, const year_month& __rhs) noexcept {59if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)60return __c;61return __lhs.month() <=> __rhs.month();62}6364_LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const months& __rhs) noexcept {65int __dmi = static_cast<int>(static_cast<unsigned>(__lhs.month())) - 1 + __rhs.count();66const int __dy = (__dmi >= 0 ? __dmi : __dmi - 11) / 12;67__dmi = __dmi - __dy * 12 + 1;68return (__lhs.year() + years(__dy)) / month(static_cast<unsigned>(__dmi));69}7071_LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const months& __lhs, const year_month& __rhs) noexcept {72return __rhs + __lhs;73}7475_LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const years& __rhs) noexcept {76return (__lhs.year() + __rhs) / __lhs.month();77}7879_LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const years& __lhs, const year_month& __rhs) noexcept {80return __rhs + __lhs;81}8283_LIBCPP_HIDE_FROM_ABI inline constexpr months operator-(const year_month& __lhs, const year_month& __rhs) noexcept {84return (__lhs.year() - __rhs.year()) +85months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month()));86}8788_LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const months& __rhs) noexcept {89return __lhs + -__rhs;90}9192_LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const years& __rhs) noexcept {93return __lhs + -__rhs;94}9596_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const months& __dm) noexcept {97*this = *this + __dm;98return *this;99}100101_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const months& __dm) noexcept {102*this = *this - __dm;103return *this;104}105106_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const years& __dy) noexcept {107*this = *this + __dy;108return *this;109}110111_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const years& __dy) noexcept {112*this = *this - __dy;113return *this;114}115116} // namespace chrono117118_LIBCPP_END_NAMESPACE_STD119120#endif // _LIBCPP_STD_VER >= 20121122#endif // _LIBCPP___CHRONO_YEAR_MONTH_H123124125