xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/monthday.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_MONTHDAY_H
1181ad6265SDimitry Andric #define _LIBCPP___CHRONO_MONTHDAY_H
1281ad6265SDimitry Andric 
1381ad6265SDimitry Andric #include <__chrono/calendar.h>
1481ad6265SDimitry Andric #include <__chrono/day.h>
1581ad6265SDimitry Andric #include <__chrono/month.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 month_day {
3081ad6265SDimitry Andric private:
31bdd1243dSDimitry Andric   chrono::month __m_;
32bdd1243dSDimitry Andric   chrono::day __d_;
33*cb14a3feSDimitry Andric 
3481ad6265SDimitry Andric public:
3506c3fb27SDimitry Andric   month_day() = default;
3681ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr month_day(const chrono::month& __mval, const chrono::day& __dval) noexcept
37bdd1243dSDimitry Andric       : __m_{__mval}, __d_{__dval} {}
38bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
39bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; }
4081ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
4181ad6265SDimitry Andric };
4281ad6265SDimitry Andric 
43*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr bool month_day::ok() const noexcept {
44*cb14a3feSDimitry Andric   if (!__m_.ok())
45*cb14a3feSDimitry Andric     return false;
46bdd1243dSDimitry Andric   const unsigned __dval = static_cast<unsigned>(__d_);
47*cb14a3feSDimitry Andric   if (__dval < 1 || __dval > 31)
48*cb14a3feSDimitry Andric     return false;
49*cb14a3feSDimitry Andric   if (__dval <= 29)
50*cb14a3feSDimitry Andric     return true;
5181ad6265SDimitry Andric   //  Now we've got either 30 or 31
52bdd1243dSDimitry Andric   const unsigned __mval = static_cast<unsigned>(__m_);
53*cb14a3feSDimitry Andric   if (__mval == 2)
54*cb14a3feSDimitry Andric     return false;
5581ad6265SDimitry Andric   if (__mval == 4 || __mval == 6 || __mval == 9 || __mval == 11)
5681ad6265SDimitry Andric     return __dval == 30;
5781ad6265SDimitry Andric   return true;
5881ad6265SDimitry Andric }
5981ad6265SDimitry Andric 
60*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const month_day& __lhs, const month_day& __rhs) noexcept {
61*cb14a3feSDimitry Andric   return __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day();
62*cb14a3feSDimitry Andric }
6381ad6265SDimitry Andric 
64*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
65*cb14a3feSDimitry Andric operator<=>(const month_day& __lhs, const month_day& __rhs) noexcept {
66bdd1243dSDimitry Andric   if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
67bdd1243dSDimitry Andric     return __c;
68bdd1243dSDimitry Andric   return __lhs.day() <=> __rhs.day();
69bdd1243dSDimitry Andric }
7081ad6265SDimitry Andric 
71*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day operator/(const month& __lhs, const day& __rhs) noexcept {
72*cb14a3feSDimitry Andric   return month_day{__lhs, __rhs};
73*cb14a3feSDimitry Andric }
7481ad6265SDimitry Andric 
75*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day operator/(const day& __lhs, const month& __rhs) noexcept {
76*cb14a3feSDimitry Andric   return __rhs / __lhs;
77*cb14a3feSDimitry Andric }
7881ad6265SDimitry Andric 
79*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day operator/(const month& __lhs, int __rhs) noexcept {
80*cb14a3feSDimitry Andric   return __lhs / day(__rhs);
81*cb14a3feSDimitry Andric }
8281ad6265SDimitry Andric 
83*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day operator/(int __lhs, const day& __rhs) noexcept {
84*cb14a3feSDimitry Andric   return month(__lhs) / __rhs;
85*cb14a3feSDimitry Andric }
8681ad6265SDimitry Andric 
87*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day operator/(const day& __lhs, int __rhs) noexcept {
88*cb14a3feSDimitry Andric   return month(__rhs) / __lhs;
89*cb14a3feSDimitry Andric }
9081ad6265SDimitry Andric 
9181ad6265SDimitry Andric class month_day_last {
9281ad6265SDimitry Andric private:
93bdd1243dSDimitry Andric   chrono::month __m_;
94*cb14a3feSDimitry Andric 
9581ad6265SDimitry Andric public:
96*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit constexpr month_day_last(const chrono::month& __val) noexcept : __m_{__val} {}
97bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
98bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_.ok(); }
9981ad6265SDimitry Andric };
10081ad6265SDimitry Andric 
101*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr bool
102*cb14a3feSDimitry Andric operator==(const month_day_last& __lhs, const month_day_last& __rhs) noexcept {
103*cb14a3feSDimitry Andric   return __lhs.month() == __rhs.month();
104*cb14a3feSDimitry Andric }
10581ad6265SDimitry Andric 
1065f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
107bdd1243dSDimitry Andric operator<=>(const month_day_last& __lhs, const month_day_last& __rhs) noexcept {
108bdd1243dSDimitry Andric   return __lhs.month() <=> __rhs.month();
109bdd1243dSDimitry Andric }
11081ad6265SDimitry Andric 
111*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day_last operator/(const month& __lhs, last_spec) noexcept {
112*cb14a3feSDimitry Andric   return month_day_last{__lhs};
113*cb14a3feSDimitry Andric }
11481ad6265SDimitry Andric 
115*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day_last operator/(last_spec, const month& __rhs) noexcept {
116*cb14a3feSDimitry Andric   return month_day_last{__rhs};
117*cb14a3feSDimitry Andric }
11881ad6265SDimitry Andric 
119*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day_last operator/(int __lhs, last_spec) noexcept {
120*cb14a3feSDimitry Andric   return month_day_last{month(__lhs)};
121*cb14a3feSDimitry Andric }
12281ad6265SDimitry Andric 
123*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr month_day_last operator/(last_spec, int __rhs) noexcept {
124*cb14a3feSDimitry Andric   return month_day_last{month(__rhs)};
125*cb14a3feSDimitry Andric }
12681ad6265SDimitry Andric 
12781ad6265SDimitry Andric } // namespace chrono
12881ad6265SDimitry Andric 
12981ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
13081ad6265SDimitry Andric 
13106c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
13281ad6265SDimitry Andric 
13381ad6265SDimitry Andric #endif // _LIBCPP___CHRONO_MONTHDAY_H
134