xref: /llvm-project/libcxx/include/__chrono/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_MONTH_H
11d7862497SMark de Wever #define _LIBCPP___CHRONO_MONTH_H
12d7862497SMark de Wever 
13d7862497SMark de Wever #include <__chrono/duration.h>
14*09e3a360SLouis Dionne #include <__compare/ordering.h>
15d7862497SMark de Wever #include <__config>
16d7862497SMark de Wever 
17d7862497SMark de Wever #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18d7862497SMark de Wever #  pragma GCC system_header
19d7862497SMark de Wever #endif
20d7862497SMark de Wever 
214f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 20
22d7862497SMark de Wever 
23d7862497SMark de Wever _LIBCPP_BEGIN_NAMESPACE_STD
24d7862497SMark de Wever 
259783f28cSLouis Dionne namespace chrono {
26d7862497SMark de Wever 
27d7862497SMark de Wever class month {
28d7862497SMark de Wever private:
2984fc2c3cSNikolas Klauser   unsigned char __m_;
309783f28cSLouis Dionne 
31d7862497SMark de Wever public:
3283ce1397SNikolas Klauser   month() = default;
339783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit inline constexpr month(unsigned __val) noexcept
349783f28cSLouis Dionne       : __m_(static_cast<unsigned char>(__val)) {}
359783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI inline constexpr month& operator++() noexcept {
369783f28cSLouis Dionne     *this += months{1};
379783f28cSLouis Dionne     return *this;
389783f28cSLouis Dionne   }
399783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI inline constexpr month operator++(int) noexcept {
409783f28cSLouis Dionne     month __tmp = *this;
419783f28cSLouis Dionne     ++(*this);
429783f28cSLouis Dionne     return __tmp;
439783f28cSLouis Dionne   }
449783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI inline constexpr month& operator--() noexcept {
459783f28cSLouis Dionne     *this -= months{1};
469783f28cSLouis Dionne     return *this;
479783f28cSLouis Dionne   }
489783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI inline constexpr month operator--(int) noexcept {
499783f28cSLouis Dionne     month __tmp = *this;
509783f28cSLouis Dionne     --(*this);
519783f28cSLouis Dionne     return __tmp;
529783f28cSLouis Dionne   }
53d7862497SMark de Wever   _LIBCPP_HIDE_FROM_ABI constexpr month& operator+=(const months& __m1) noexcept;
54d7862497SMark de Wever   _LIBCPP_HIDE_FROM_ABI constexpr month& operator-=(const months& __m1) noexcept;
5584fc2c3cSNikolas Klauser   _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator unsigned() const noexcept { return __m_; }
5684fc2c3cSNikolas Klauser   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_ >= 1 && __m_ <= 12; }
57d7862497SMark de Wever };
58d7862497SMark de Wever 
599783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const month& __lhs, const month& __rhs) noexcept {
609783f28cSLouis Dionne   return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs);
619783f28cSLouis Dionne }
62d7862497SMark de Wever 
63b81c6941SLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(const month& __lhs, const month& __rhs) noexcept {
6441f7bb99SMark de Wever   return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
6541f7bb99SMark de Wever }
66d7862497SMark de Wever 
679783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr month operator+(const month& __lhs, const months& __rhs) noexcept {
68d7862497SMark de Wever   auto const __mu = static_cast<long long>(static_cast<unsigned>(__lhs)) + (__rhs.count() - 1);
69d7862497SMark de Wever   auto const __yr = (__mu >= 0 ? __mu : __mu - 11) / 12;
70d7862497SMark de Wever   return month{static_cast<unsigned>(__mu - __yr * 12 + 1)};
71d7862497SMark de Wever }
72d7862497SMark de Wever 
739783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr month operator+(const months& __lhs, const month& __rhs) noexcept {
749783f28cSLouis Dionne   return __rhs + __lhs;
759783f28cSLouis Dionne }
76d7862497SMark de Wever 
779783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr month operator-(const month& __lhs, const months& __rhs) noexcept {
789783f28cSLouis Dionne   return __lhs + -__rhs;
799783f28cSLouis Dionne }
80d7862497SMark de Wever 
819783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr months operator-(const month& __lhs, const month& __rhs) noexcept {
82d7862497SMark de Wever   auto const __dm = static_cast<unsigned>(__lhs) - static_cast<unsigned>(__rhs);
83d7862497SMark de Wever   return months(__dm <= 11 ? __dm : __dm + 12);
84d7862497SMark de Wever }
85d7862497SMark de Wever 
869783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr month& month::operator+=(const months& __dm) noexcept {
879783f28cSLouis Dionne   *this = *this + __dm;
889783f28cSLouis Dionne   return *this;
899783f28cSLouis Dionne }
90d7862497SMark de Wever 
919783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI inline constexpr month& month::operator-=(const months& __dm) noexcept {
929783f28cSLouis Dionne   *this = *this - __dm;
939783f28cSLouis Dionne   return *this;
949783f28cSLouis Dionne }
95d7862497SMark de Wever 
96d7862497SMark de Wever inline constexpr month January{1};
97d7862497SMark de Wever inline constexpr month February{2};
98d7862497SMark de Wever inline constexpr month March{3};
99d7862497SMark de Wever inline constexpr month April{4};
100d7862497SMark de Wever inline constexpr month May{5};
101d7862497SMark de Wever inline constexpr month June{6};
102d7862497SMark de Wever inline constexpr month July{7};
103d7862497SMark de Wever inline constexpr month August{8};
104d7862497SMark de Wever inline constexpr month September{9};
105d7862497SMark de Wever inline constexpr month October{10};
106d7862497SMark de Wever inline constexpr month November{11};
107d7862497SMark de Wever inline constexpr month December{12};
108d7862497SMark de Wever 
109d7862497SMark de Wever } // namespace chrono
110d7862497SMark de Wever 
111d7862497SMark de Wever _LIBCPP_END_NAMESPACE_STD
112d7862497SMark de Wever 
1134f15267dSNikolas Klauser #endif // _LIBCPP_STD_VER >= 20
114d7862497SMark de Wever 
115d7862497SMark de Wever #endif // _LIBCPP___CHRONO_MONTH_H
116