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