xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/day.h (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 // -*- 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_DAY_H
11 #define _LIBCPP___CHRONO_DAY_H
12 
13 #include <__chrono/duration.h>
14 #include <__config>
15 #include <compare>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 #if _LIBCPP_STD_VER >= 20
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 namespace chrono {
26 
27 class day {
28 private:
29   unsigned char __d_;
30 
31 public:
32   day() = default;
33   _LIBCPP_HIDE_FROM_ABI explicit inline constexpr day(unsigned __val) noexcept
34       : __d_(static_cast<unsigned char>(__val)) {}
35   _LIBCPP_HIDE_FROM_ABI inline constexpr day& operator++() noexcept {
36     ++__d_;
37     return *this;
38   }
39   _LIBCPP_HIDE_FROM_ABI inline constexpr day operator++(int) noexcept {
40     day __tmp = *this;
41     ++(*this);
42     return __tmp;
43   }
44   _LIBCPP_HIDE_FROM_ABI inline constexpr day& operator--() noexcept {
45     --__d_;
46     return *this;
47   }
48   _LIBCPP_HIDE_FROM_ABI inline constexpr day operator--(int) noexcept {
49     day __tmp = *this;
50     --(*this);
51     return __tmp;
52   }
53   _LIBCPP_HIDE_FROM_ABI constexpr day& operator+=(const days& __dd) noexcept;
54   _LIBCPP_HIDE_FROM_ABI constexpr day& operator-=(const days& __dd) noexcept;
55   _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator unsigned() const noexcept { return __d_; }
56   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __d_ >= 1 && __d_ <= 31; }
57 };
58 
59 _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const day& __lhs, const day& __rhs) noexcept {
60   return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs);
61 }
62 
63 _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(const day& __lhs, const day& __rhs) noexcept {
64   return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
65 }
66 
67 _LIBCPP_HIDE_FROM_ABI inline constexpr day operator+(const day& __lhs, const days& __rhs) noexcept {
68   return day(static_cast<unsigned>(__lhs) + __rhs.count());
69 }
70 
71 _LIBCPP_HIDE_FROM_ABI inline constexpr day operator+(const days& __lhs, const day& __rhs) noexcept {
72   return __rhs + __lhs;
73 }
74 
75 _LIBCPP_HIDE_FROM_ABI inline constexpr day operator-(const day& __lhs, const days& __rhs) noexcept {
76   return __lhs + -__rhs;
77 }
78 
79 _LIBCPP_HIDE_FROM_ABI inline constexpr days operator-(const day& __lhs, const day& __rhs) noexcept {
80   return days(static_cast<int>(static_cast<unsigned>(__lhs)) - static_cast<int>(static_cast<unsigned>(__rhs)));
81 }
82 
83 _LIBCPP_HIDE_FROM_ABI inline constexpr day& day::operator+=(const days& __dd) noexcept {
84   *this = *this + __dd;
85   return *this;
86 }
87 
88 _LIBCPP_HIDE_FROM_ABI inline constexpr day& day::operator-=(const days& __dd) noexcept {
89   *this = *this - __dd;
90   return *this;
91 }
92 
93 } // namespace chrono
94 
95 _LIBCPP_END_NAMESPACE_STD
96 
97 #endif // _LIBCPP_STD_VER >= 20
98 
99 #endif // _LIBCPP___CHRONO_DAY_H
100