xref: /llvm-project/libcxx/include/__chrono/year_month.h (revision 09e3a360581dc36d0820d3fb6da9bd7cfed87b5d)
1d7862497SMark de Wever // -*- C++ -*-
2d7862497SMark de Wever //===----------------------------------------------------------------------===//
3d7862497SMark de Wever //
4d7862497SMark de Wever // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5d7862497SMark de Wever // See https://llvm.org/LICENSE.txt for license information.
6d7862497SMark de Wever // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7d7862497SMark de Wever //
8d7862497SMark de Wever //===----------------------------------------------------------------------===//
9d7862497SMark de Wever 
10d7862497SMark de Wever #ifndef _LIBCPP___CHRONO_YEAR_MONTH_H
11d7862497SMark de Wever #define _LIBCPP___CHRONO_YEAR_MONTH_H
12d7862497SMark de Wever 
13d7862497SMark de Wever #include <__chrono/duration.h>
14d7862497SMark de Wever #include <__chrono/month.h>
15d7862497SMark de Wever #include <__chrono/year.h>
16*09e3a360SLouis Dionne #include <__compare/ordering.h>
17d7862497SMark de Wever #include <__config>
18d7862497SMark de Wever 
19d7862497SMark de Wever #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20d7862497SMark de Wever #  pragma GCC system_header
21d7862497SMark de Wever #endif
22d7862497SMark de Wever 
234f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 20
24d7862497SMark de Wever 
25d7862497SMark de Wever _LIBCPP_BEGIN_NAMESPACE_STD
26d7862497SMark de Wever 
279783f28cSLouis Dionne namespace chrono {
28d7862497SMark de Wever 
29d7862497SMark de Wever class year_month {
3084fc2c3cSNikolas Klauser   chrono::year __y_;
3184fc2c3cSNikolas Klauser   chrono::month __m_;
329783f28cSLouis Dionne 
33d7862497SMark de Wever public:
3483ce1397SNikolas Klauser   year_month() = default;
35d7862497SMark de Wever   _LIBCPP_HIDE_FROM_ABI constexpr year_month(const chrono::year& __yval, const chrono::month& __mval) noexcept
3684fc2c3cSNikolas Klauser       : __y_{__yval}, __m_{__mval} {}
3784fc2c3cSNikolas Klauser   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
3884fc2c3cSNikolas Klauser   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
39766bf140SMark de Wever   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const months& __dm) noexcept;
40766bf140SMark de Wever   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const months& __dm) noexcept;
41766bf140SMark de Wever   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const years& __dy) noexcept;
42766bf140SMark de Wever   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const years& __dy) noexcept;
4384fc2c3cSNikolas Klauser   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok(); }
44d7862497SMark de Wever };
45d7862497SMark de Wever 
469783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, const month& __m) noexcept {
479783f28cSLouis Dionne   return year_month{__y, __m};
489783f28cSLouis Dionne }
49d7862497SMark de Wever 
509783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, int __m) noexcept {
519783f28cSLouis Dionne   return year_month{__y, month(__m)};
529783f28cSLouis Dionne }
53d7862497SMark de Wever 
549783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept {
559783f28cSLouis Dionne   return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month();
569783f28cSLouis Dionne }
57d7862497SMark de Wever 
589783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
599783f28cSLouis Dionne operator<=>(const year_month& __lhs, const year_month& __rhs) noexcept {
6041f7bb99SMark de Wever   if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
6141f7bb99SMark de Wever     return __c;
6241f7bb99SMark de Wever   return __lhs.month() <=> __rhs.month();
6341f7bb99SMark de Wever }
64d7862497SMark de Wever 
659783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const months& __rhs) noexcept {
66d7862497SMark de Wever   int __dmi      = static_cast<int>(static_cast<unsigned>(__lhs.month())) - 1 + __rhs.count();
67d7862497SMark de Wever   const int __dy = (__dmi >= 0 ? __dmi : __dmi - 11) / 12;
68d7862497SMark de Wever   __dmi          = __dmi - __dy * 12 + 1;
69d7862497SMark de Wever   return (__lhs.year() + years(__dy)) / month(static_cast<unsigned>(__dmi));
70d7862497SMark de Wever }
71d7862497SMark de Wever 
729783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const months& __lhs, const year_month& __rhs) noexcept {
739783f28cSLouis Dionne   return __rhs + __lhs;
749783f28cSLouis Dionne }
75d7862497SMark de Wever 
769783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const years& __rhs) noexcept {
779783f28cSLouis Dionne   return (__lhs.year() + __rhs) / __lhs.month();
789783f28cSLouis Dionne }
79d7862497SMark de Wever 
809783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const years& __lhs, const year_month& __rhs) noexcept {
819783f28cSLouis Dionne   return __rhs + __lhs;
829783f28cSLouis Dionne }
83d7862497SMark de Wever 
849783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr months operator-(const year_month& __lhs, const year_month& __rhs) noexcept {
859783f28cSLouis Dionne   return (__lhs.year() - __rhs.year()) +
869783f28cSLouis Dionne          months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month()));
879783f28cSLouis Dionne }
88d7862497SMark de Wever 
899783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const months& __rhs) noexcept {
909783f28cSLouis Dionne   return __lhs + -__rhs;
919783f28cSLouis Dionne }
92d7862497SMark de Wever 
939783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const years& __rhs) noexcept {
949783f28cSLouis Dionne   return __lhs + -__rhs;
959783f28cSLouis Dionne }
96d7862497SMark de Wever 
97766bf140SMark de Wever _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const months& __dm) noexcept {
98766bf140SMark de Wever   *this = *this + __dm;
99766bf140SMark de Wever   return *this;
100766bf140SMark de Wever }
101766bf140SMark de Wever 
102766bf140SMark de Wever _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const months& __dm) noexcept {
103766bf140SMark de Wever   *this = *this - __dm;
104766bf140SMark de Wever   return *this;
105766bf140SMark de Wever }
106766bf140SMark de Wever 
107766bf140SMark de Wever _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const years& __dy) noexcept {
108766bf140SMark de Wever   *this = *this + __dy;
109766bf140SMark de Wever   return *this;
110766bf140SMark de Wever }
111766bf140SMark de Wever 
112766bf140SMark de Wever _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const years& __dy) noexcept {
113766bf140SMark de Wever   *this = *this - __dy;
114766bf140SMark de Wever   return *this;
115766bf140SMark de Wever }
116766bf140SMark de Wever 
117d7862497SMark de Wever } // namespace chrono
118d7862497SMark de Wever 
119d7862497SMark de Wever _LIBCPP_END_NAMESPACE_STD
120d7862497SMark de Wever 
1214f15267dSNikolas Klauser #endif // _LIBCPP_STD_VER >= 20
122d7862497SMark de Wever 
123d7862497SMark de Wever #endif // _LIBCPP___CHRONO_YEAR_MONTH_H
124