xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/month.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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_MONTH_H
1181ad6265SDimitry Andric #define _LIBCPP___CHRONO_MONTH_H
1281ad6265SDimitry Andric 
1381ad6265SDimitry Andric #include <__chrono/duration.h>
1481ad6265SDimitry Andric #include <__config>
15*bdd1243dSDimitry Andric #include <compare>
1681ad6265SDimitry Andric 
1781ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1881ad6265SDimitry Andric #  pragma GCC system_header
1981ad6265SDimitry Andric #endif
2081ad6265SDimitry Andric 
2181ad6265SDimitry Andric #if _LIBCPP_STD_VER > 17
2281ad6265SDimitry Andric 
2381ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2481ad6265SDimitry Andric 
2581ad6265SDimitry Andric namespace chrono
2681ad6265SDimitry Andric {
2781ad6265SDimitry Andric 
2881ad6265SDimitry Andric class month {
2981ad6265SDimitry Andric private:
30*bdd1243dSDimitry Andric     unsigned char __m_;
3181ad6265SDimitry Andric public:
3281ad6265SDimitry Andric     _LIBCPP_HIDE_FROM_ABI month() = default;
33*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI explicit inline constexpr month(unsigned __val) noexcept : __m_(static_cast<unsigned char>(__val)) {}
34*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI inline constexpr month& operator++()    noexcept { ++__m_; return *this; }
3581ad6265SDimitry Andric     _LIBCPP_HIDE_FROM_ABI inline constexpr month  operator++(int) noexcept { month __tmp = *this; ++(*this); return __tmp; }
36*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI inline constexpr month& operator--()    noexcept { --__m_; return *this; }
3781ad6265SDimitry Andric     _LIBCPP_HIDE_FROM_ABI inline constexpr month  operator--(int) noexcept { month __tmp = *this; --(*this); return __tmp; }
3881ad6265SDimitry Andric     _LIBCPP_HIDE_FROM_ABI        constexpr month& operator+=(const months& __m1) noexcept;
3981ad6265SDimitry Andric     _LIBCPP_HIDE_FROM_ABI        constexpr month& operator-=(const months& __m1) noexcept;
40*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator unsigned() const noexcept { return __m_; }
41*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_ >= 1 && __m_ <= 12; }
4281ad6265SDimitry Andric };
4381ad6265SDimitry Andric 
4481ad6265SDimitry Andric 
4581ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr
4681ad6265SDimitry Andric bool operator==(const month& __lhs, const month& __rhs) noexcept
4781ad6265SDimitry Andric { return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs); }
4881ad6265SDimitry Andric 
49*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const month& __lhs, const month& __rhs) noexcept {
50*bdd1243dSDimitry Andric     return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
51*bdd1243dSDimitry Andric }
5281ad6265SDimitry Andric 
5381ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr
5481ad6265SDimitry Andric month operator+ (const month& __lhs, const months& __rhs) noexcept
5581ad6265SDimitry Andric {
5681ad6265SDimitry Andric     auto const __mu = static_cast<long long>(static_cast<unsigned>(__lhs)) + (__rhs.count() - 1);
5781ad6265SDimitry Andric     auto const __yr = (__mu >= 0 ? __mu : __mu - 11) / 12;
5881ad6265SDimitry Andric     return month{static_cast<unsigned>(__mu - __yr * 12 + 1)};
5981ad6265SDimitry Andric }
6081ad6265SDimitry Andric 
6181ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr
6281ad6265SDimitry Andric month operator+ (const months& __lhs, const month& __rhs) noexcept
6381ad6265SDimitry Andric { return __rhs + __lhs; }
6481ad6265SDimitry Andric 
6581ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr
6681ad6265SDimitry Andric month operator- (const month& __lhs, const months& __rhs) noexcept
6781ad6265SDimitry Andric { return __lhs + -__rhs; }
6881ad6265SDimitry Andric 
6981ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr
7081ad6265SDimitry Andric months operator-(const month& __lhs, const month& __rhs) noexcept
7181ad6265SDimitry Andric {
7281ad6265SDimitry Andric     auto const __dm = static_cast<unsigned>(__lhs) - static_cast<unsigned>(__rhs);
7381ad6265SDimitry Andric     return months(__dm <= 11 ? __dm : __dm + 12);
7481ad6265SDimitry Andric }
7581ad6265SDimitry Andric 
7681ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr
7781ad6265SDimitry Andric month& month::operator+=(const months& __dm) noexcept
7881ad6265SDimitry Andric { *this = *this + __dm; return *this; }
7981ad6265SDimitry Andric 
8081ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI inline constexpr
8181ad6265SDimitry Andric month& month::operator-=(const months& __dm) noexcept
8281ad6265SDimitry Andric { *this = *this - __dm; return *this; }
8381ad6265SDimitry Andric 
8481ad6265SDimitry Andric inline constexpr month January{1};
8581ad6265SDimitry Andric inline constexpr month February{2};
8681ad6265SDimitry Andric inline constexpr month March{3};
8781ad6265SDimitry Andric inline constexpr month April{4};
8881ad6265SDimitry Andric inline constexpr month May{5};
8981ad6265SDimitry Andric inline constexpr month June{6};
9081ad6265SDimitry Andric inline constexpr month July{7};
9181ad6265SDimitry Andric inline constexpr month August{8};
9281ad6265SDimitry Andric inline constexpr month September{9};
9381ad6265SDimitry Andric inline constexpr month October{10};
9481ad6265SDimitry Andric inline constexpr month November{11};
9581ad6265SDimitry Andric inline constexpr month December{12};
9681ad6265SDimitry Andric 
9781ad6265SDimitry Andric } // namespace chrono
9881ad6265SDimitry Andric 
9981ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
10081ad6265SDimitry Andric 
10181ad6265SDimitry Andric #endif // _LIBCPP_STD_VER > 17
10281ad6265SDimitry Andric 
10381ad6265SDimitry Andric #endif // _LIBCPP___CHRONO_MONTH_H
104