xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/year_month.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
181ad6265SDimitry Andric // -*- C++ -*-
281ad6265SDimitry Andric //===----------------------------------------------------------------------===//
381ad6265SDimitry Andric //
481ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
581ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
681ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
781ad6265SDimitry Andric //
881ad6265SDimitry Andric //===----------------------------------------------------------------------===//
981ad6265SDimitry Andric 
1081ad6265SDimitry Andric #ifndef _LIBCPP___CHRONO_YEAR_MONTH_H
1181ad6265SDimitry Andric #define _LIBCPP___CHRONO_YEAR_MONTH_H
1281ad6265SDimitry Andric 
1381ad6265SDimitry Andric #include <__chrono/duration.h>
1481ad6265SDimitry Andric #include <__chrono/month.h>
1581ad6265SDimitry Andric #include <__chrono/year.h>
1681ad6265SDimitry Andric #include <__config>
17bdd1243dSDimitry Andric #include <compare>
1881ad6265SDimitry Andric 
1981ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2081ad6265SDimitry Andric #  pragma GCC system_header
2181ad6265SDimitry Andric #endif
2281ad6265SDimitry Andric 
2306c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
2481ad6265SDimitry Andric 
2581ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2681ad6265SDimitry Andric 
27*cb14a3feSDimitry Andric namespace chrono {
2881ad6265SDimitry Andric 
2981ad6265SDimitry Andric class year_month {
30bdd1243dSDimitry Andric   chrono::year __y_;
31bdd1243dSDimitry Andric   chrono::month __m_;
32*cb14a3feSDimitry Andric 
3381ad6265SDimitry Andric public:
3406c3fb27SDimitry Andric   year_month() = default;
3581ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr year_month(const chrono::year& __yval, const chrono::month& __mval) noexcept
36bdd1243dSDimitry Andric       : __y_{__yval}, __m_{__mval} {}
37bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
38bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
395f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const months& __dm) noexcept;
405f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const months& __dm) noexcept;
415f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const years& __dy) noexcept;
425f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const years& __dy) noexcept;
43bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok(); }
4481ad6265SDimitry Andric };
4581ad6265SDimitry Andric 
46*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, const month& __m) noexcept {
47*cb14a3feSDimitry Andric   return year_month{__y, __m};
48*cb14a3feSDimitry Andric }
4981ad6265SDimitry Andric 
50*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, int __m) noexcept {
51*cb14a3feSDimitry Andric   return year_month{__y, month(__m)};
52*cb14a3feSDimitry Andric }
5381ad6265SDimitry Andric 
54*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept {
55*cb14a3feSDimitry Andric   return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month();
56*cb14a3feSDimitry Andric }
5781ad6265SDimitry Andric 
58*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
59*cb14a3feSDimitry Andric operator<=>(const year_month& __lhs, const year_month& __rhs) noexcept {
60bdd1243dSDimitry Andric   if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
61bdd1243dSDimitry Andric     return __c;
62bdd1243dSDimitry Andric   return __lhs.month() <=> __rhs.month();
63bdd1243dSDimitry Andric }
6481ad6265SDimitry Andric 
65*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const months& __rhs) noexcept {
6681ad6265SDimitry Andric   int __dmi      = static_cast<int>(static_cast<unsigned>(__lhs.month())) - 1 + __rhs.count();
6781ad6265SDimitry Andric   const int __dy = (__dmi >= 0 ? __dmi : __dmi - 11) / 12;
6881ad6265SDimitry Andric   __dmi          = __dmi - __dy * 12 + 1;
6981ad6265SDimitry Andric   return (__lhs.year() + years(__dy)) / month(static_cast<unsigned>(__dmi));
7081ad6265SDimitry Andric }
7181ad6265SDimitry Andric 
72*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const months& __lhs, const year_month& __rhs) noexcept {
73*cb14a3feSDimitry Andric   return __rhs + __lhs;
74*cb14a3feSDimitry Andric }
7581ad6265SDimitry Andric 
76*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const years& __rhs) noexcept {
77*cb14a3feSDimitry Andric   return (__lhs.year() + __rhs) / __lhs.month();
78*cb14a3feSDimitry Andric }
7981ad6265SDimitry Andric 
80*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const years& __lhs, const year_month& __rhs) noexcept {
81*cb14a3feSDimitry Andric   return __rhs + __lhs;
82*cb14a3feSDimitry Andric }
8381ad6265SDimitry Andric 
84*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr months operator-(const year_month& __lhs, const year_month& __rhs) noexcept {
85*cb14a3feSDimitry Andric   return (__lhs.year() - __rhs.year()) +
86*cb14a3feSDimitry Andric          months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month()));
87*cb14a3feSDimitry Andric }
8881ad6265SDimitry Andric 
89*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const months& __rhs) noexcept {
90*cb14a3feSDimitry Andric   return __lhs + -__rhs;
91*cb14a3feSDimitry Andric }
9281ad6265SDimitry Andric 
93*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const years& __rhs) noexcept {
94*cb14a3feSDimitry Andric   return __lhs + -__rhs;
95*cb14a3feSDimitry Andric }
9681ad6265SDimitry Andric 
975f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const months& __dm) noexcept {
985f757f3fSDimitry Andric   *this = *this + __dm;
995f757f3fSDimitry Andric   return *this;
1005f757f3fSDimitry Andric }
1015f757f3fSDimitry Andric 
1025f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const months& __dm) noexcept {
1035f757f3fSDimitry Andric   *this = *this - __dm;
1045f757f3fSDimitry Andric   return *this;
1055f757f3fSDimitry Andric }
1065f757f3fSDimitry Andric 
1075f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const years& __dy) noexcept {
1085f757f3fSDimitry Andric   *this = *this + __dy;
1095f757f3fSDimitry Andric   return *this;
1105f757f3fSDimitry Andric }
1115f757f3fSDimitry Andric 
1125f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const years& __dy) noexcept {
1135f757f3fSDimitry Andric   *this = *this - __dy;
1145f757f3fSDimitry Andric   return *this;
1155f757f3fSDimitry Andric }
1165f757f3fSDimitry Andric 
11781ad6265SDimitry Andric } // namespace chrono
11881ad6265SDimitry Andric 
11981ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
12081ad6265SDimitry Andric 
12106c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
12281ad6265SDimitry Andric 
12381ad6265SDimitry Andric #endif // _LIBCPP___CHRONO_YEAR_MONTH_H
124