blob: c0ee3e4a94fda6ff293ec1cdb69dd5823f1f87f8 [file] [log] [blame]
Mark de Weverd7862492022-04-03 13:58:571// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___CHRONO_MONTHDAY_H
11#define _LIBCPP___CHRONO_MONTHDAY_H
12
13#include <__chrono/calendar.h>
14#include <__chrono/day.h>
15#include <__chrono/month.h>
16#include <__config>
Mark de Wever41f7bb92022-07-13 05:59:4617#include <compare>
Mark de Weverd7862492022-04-03 13:58:5718
19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20# pragma GCC system_header
21#endif
22
23#if _LIBCPP_STD_VER > 17
24
25_LIBCPP_BEGIN_NAMESPACE_STD
26
27namespace chrono
28{
29
30class month_day {
31private:
Nikolas Klauser84fc2c32022-09-02 14:19:0732 chrono::month __m_;
33 chrono::day __d_;
Mark de Weverd7862492022-04-03 13:58:5734public:
35 _LIBCPP_HIDE_FROM_ABI month_day() = default;
36 _LIBCPP_HIDE_FROM_ABI constexpr month_day(const chrono::month& __mval, const chrono::day& __dval) noexcept
Nikolas Klauser84fc2c32022-09-02 14:19:0737 : __m_{__mval}, __d_{__dval} {}
38 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
39 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; }
Mark de Weverd7862492022-04-03 13:58:5740 _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
41};
42
43_LIBCPP_HIDE_FROM_ABI inline constexpr
44bool month_day::ok() const noexcept
45{
Nikolas Klauser84fc2c32022-09-02 14:19:0746 if (!__m_.ok()) return false;
47 const unsigned __dval = static_cast<unsigned>(__d_);
Mark de Weverd7862492022-04-03 13:58:5748 if (__dval < 1 || __dval > 31) return false;
49 if (__dval <= 29) return true;
50// Now we've got either 30 or 31
Nikolas Klauser84fc2c32022-09-02 14:19:0751 const unsigned __mval = static_cast<unsigned>(__m_);
Mark de Weverd7862492022-04-03 13:58:5752 if (__mval == 2) return false;
53 if (__mval == 4 || __mval == 6 || __mval == 9 || __mval == 11)
54 return __dval == 30;
55 return true;
56}
57
58_LIBCPP_HIDE_FROM_ABI inline constexpr
59bool operator==(const month_day& __lhs, const month_day& __rhs) noexcept
60{ return __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); }
61
Mark de Wever41f7bb92022-07-13 05:59:4662_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const month_day& __lhs, const month_day& __rhs) noexcept {
63 if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
64 return __c;
65 return __lhs.day() <=> __rhs.day();
66}
Mark de Weverd7862492022-04-03 13:58:5767
68_LIBCPP_HIDE_FROM_ABI inline constexpr
69month_day operator/(const month& __lhs, const day& __rhs) noexcept
70{ return month_day{__lhs, __rhs}; }
71
72_LIBCPP_HIDE_FROM_ABI constexpr
73month_day operator/(const day& __lhs, const month& __rhs) noexcept
74{ return __rhs / __lhs; }
75
76_LIBCPP_HIDE_FROM_ABI inline constexpr
77month_day operator/(const month& __lhs, int __rhs) noexcept
78{ return __lhs / day(__rhs); }
79
80_LIBCPP_HIDE_FROM_ABI constexpr
81month_day operator/(int __lhs, const day& __rhs) noexcept
82{ return month(__lhs) / __rhs; }
83
84_LIBCPP_HIDE_FROM_ABI constexpr
85month_day operator/(const day& __lhs, int __rhs) noexcept
86{ return month(__rhs) / __lhs; }
87
Mark de Weverd7862492022-04-03 13:58:5788class month_day_last {
89private:
Nikolas Klauser84fc2c32022-09-02 14:19:0790 chrono::month __m_;
Mark de Weverd7862492022-04-03 13:58:5791public:
92 _LIBCPP_HIDE_FROM_ABI explicit constexpr month_day_last(const chrono::month& __val) noexcept
Nikolas Klauser84fc2c32022-09-02 14:19:0793 : __m_{__val} {}
94 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
95 _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_.ok(); }
Mark de Weverd7862492022-04-03 13:58:5796};
97
98_LIBCPP_HIDE_FROM_ABI inline constexpr
99bool operator==(const month_day_last& __lhs, const month_day_last& __rhs) noexcept
100{ return __lhs.month() == __rhs.month(); }
101
Mark de Wever41f7bb92022-07-13 05:59:46102_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering
103operator<=>(const month_day_last& __lhs, const month_day_last& __rhs) noexcept {
104 return __lhs.month() <=> __rhs.month();
105}
Mark de Weverd7862492022-04-03 13:58:57106
107_LIBCPP_HIDE_FROM_ABI inline constexpr
108month_day_last operator/(const month& __lhs, last_spec) noexcept
109{ return month_day_last{__lhs}; }
110
111_LIBCPP_HIDE_FROM_ABI inline constexpr
112month_day_last operator/(last_spec, const month& __rhs) noexcept
113{ return month_day_last{__rhs}; }
114
115_LIBCPP_HIDE_FROM_ABI inline constexpr
116month_day_last operator/(int __lhs, last_spec) noexcept
117{ return month_day_last{month(__lhs)}; }
118
119_LIBCPP_HIDE_FROM_ABI inline constexpr
120month_day_last operator/(last_spec, int __rhs) noexcept
121{ return month_day_last{month(__rhs)}; }
122
123} // namespace chrono
124
125_LIBCPP_END_NAMESPACE_STD
126
127#endif // _LIBCPP_STD_VER > 17
128
129#endif // _LIBCPP___CHRONO_MONTHDAY_H