tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
weekday.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2023 Tobias Hienzsch
3
4#ifndef TETL_CHRONO_WEEKDAY_HPP
5#define TETL_CHRONO_WEEKDAY_HPP
6
7#include <etl/_chrono/duration.hpp>
8#include <etl/_chrono/last_spec.hpp>
9#include <etl/_chrono/local_t.hpp>
10#include <etl/_chrono/system_clock.hpp>
11#include <etl/_cstdint/uint_t.hpp>
12
13namespace etl::chrono {
14
15struct weekday_indexed;
16struct weekday_last;
17
18/// \ingroup chrono
19struct weekday {
20 weekday() = default;
21
22 constexpr explicit weekday(unsigned wd) noexcept
23 : _wd{static_cast<etl::uint8_t>(wd == 7 ? 0 : wd)}
24 {
25 }
26
27 constexpr weekday(sys_days const& dp) noexcept
28 : _wd{weekday_from_days(dp.time_since_epoch().count())}
29 {
30 }
31
32 constexpr explicit weekday(local_days const& dp) noexcept
33 : _wd{weekday_from_days(dp.time_since_epoch().count())}
34 {
35 }
36
37 constexpr auto operator++() noexcept -> weekday&
38 {
39 return *this += etl::chrono::days{1};
40 }
41
42 constexpr auto operator++(int) noexcept -> weekday
43 {
44 return *this += etl::chrono::days{1};
45 }
46
47 constexpr auto operator--() noexcept -> weekday&
48 {
49 return *this -= etl::chrono::days{1};
50 }
51
52 constexpr auto operator--(int) noexcept -> weekday
53 {
54 return *this -= etl::chrono::days{1};
55 }
56
57 constexpr auto operator+=(days const& d) noexcept -> weekday&
58 {
59 _wd += d.count();
60 _wd %= 7;
61 return *this;
62 }
63
64 constexpr auto operator-=(days const& d) noexcept -> weekday&
65 {
66 _wd -= d.count();
67 _wd %= 7;
68 return *this;
69 }
70
71 [[nodiscard]] constexpr auto c_encoding() const noexcept -> unsigned
72 {
73 return _wd;
74 }
75
76 [[nodiscard]] constexpr auto iso_encoding() const noexcept -> unsigned
77 {
78 return _wd == 0U ? 7U : _wd;
79 }
80
81 [[nodiscard]] constexpr auto ok() const noexcept -> bool
82 {
83 return _wd < 7U;
84 }
85
86 // Defined in weekday_indexed
87 [[nodiscard]] constexpr auto operator[](unsigned index) const noexcept -> weekday_indexed;
88
89 // Defined in weekday_last
90 [[nodiscard]] constexpr auto operator[](etl::chrono::last_spec /*tag*/) const noexcept -> weekday_last;
91
92 friend constexpr auto operator==(weekday const& lhs, weekday const& rhs) noexcept -> bool
93 {
94 return lhs.c_encoding() == rhs.c_encoding();
95 }
96
97private:
98 [[nodiscard]] static constexpr auto weekday_from_days(int tp) noexcept -> etl::uint8_t
99 {
100 return static_cast<etl::uint8_t>(tp >= -4 ? (tp + 4) % 7 : ((tp + 5) % 7) + 6);
101 }
102
103 etl::uint8_t _wd;
104};
105
106[[nodiscard]] constexpr auto operator+(weekday const& lhs, days const& rhs) noexcept -> weekday
107{
108 return weekday{static_cast<unsigned>((static_cast<int32_t>(lhs.c_encoding()) + rhs.count()) % 7)};
109}
110
111[[nodiscard]] constexpr auto operator+(days const& lhs, weekday const& rhs) noexcept -> weekday
112{
113 return rhs + lhs;
114}
115
116[[nodiscard]] constexpr auto operator-(weekday const& lhs, days const& rhs) noexcept -> weekday
117{
118 return weekday{static_cast<unsigned>((static_cast<int32_t>(lhs.c_encoding()) - rhs.count()) % 7)};
119}
120
121[[nodiscard]] constexpr auto operator-(weekday const& lhs, weekday const& rhs) noexcept -> days
122{
123 auto const count = static_cast<int_least32_t>(lhs.c_encoding() - rhs.c_encoding());
124 return count >= 0 ? days{count} : days{count + 7};
125}
126
127// NOLINTBEGIN(readability-identifier-naming)
128inline constexpr auto Sunday = etl::chrono::weekday{0};
129inline constexpr auto Monday = etl::chrono::weekday{1};
130inline constexpr auto Tuesday = etl::chrono::weekday{2};
131inline constexpr auto Wednesday = etl::chrono::weekday{3};
132inline constexpr auto Thursday = etl::chrono::weekday{4};
133inline constexpr auto Friday = etl::chrono::weekday{5};
134inline constexpr auto Saturday = etl::chrono::weekday{6};
135// NOLINTEND(readability-identifier-naming)
136
137} // namespace etl::chrono
138
139#endif // TETL_CHRONO_WEEKDAY_HPP
Definition abs.hpp:12
constexpr auto operator-(weekday const &lhs, days const &rhs) noexcept -> weekday
Definition weekday.hpp:116
constexpr auto Saturday
Definition weekday.hpp:134
constexpr auto operator-(weekday const &lhs, weekday const &rhs) noexcept -> days
Definition weekday.hpp:121
constexpr auto operator+(weekday const &lhs, days const &rhs) noexcept -> weekday
Definition weekday.hpp:106
constexpr auto operator+(days const &lhs, weekday const &rhs) noexcept -> weekday
Definition weekday.hpp:111
constexpr auto Friday
Definition weekday.hpp:133
constexpr auto Wednesday
Definition weekday.hpp:131
constexpr auto Tuesday
Definition weekday.hpp:130
constexpr auto Monday
Definition weekday.hpp:129
constexpr auto Thursday
Definition weekday.hpp:132
constexpr auto Sunday
Definition weekday.hpp:128
Definition adjacent_find.hpp:9
constexpr auto count() const -> rep
Returns the number of ticks for this duration.
Definition duration.hpp:98
last_spec is an empty tag type that is used in conjunction with other calendar types to indicate the ...
Definition last_spec.hpp:20
constexpr auto time_since_epoch() const noexcept -> duration
Returns a duration representing the amount of time between *this and the clock's epoch.
Definition time_point.hpp:62
Definition weekday_indexed.hpp:12
Definition weekday_last.hpp:12
Definition weekday.hpp:19
constexpr auto operator--(int) noexcept -> weekday
Definition weekday.hpp:52
constexpr auto operator--() noexcept -> weekday &
Definition weekday.hpp:47
constexpr auto ok() const noexcept -> bool
Definition weekday.hpp:81
constexpr weekday(sys_days const &dp) noexcept
Definition weekday.hpp:27
constexpr auto operator[](etl::chrono::last_spec) const noexcept -> weekday_last
Definition weekday_last.hpp:37
constexpr auto operator+=(days const &d) noexcept -> weekday &
Definition weekday.hpp:57
constexpr auto operator++(int) noexcept -> weekday
Definition weekday.hpp:42
constexpr auto operator++() noexcept -> weekday &
Definition weekday.hpp:37
constexpr auto iso_encoding() const noexcept -> unsigned
Definition weekday.hpp:76
constexpr auto c_encoding() const noexcept -> unsigned
Definition weekday.hpp:71
constexpr weekday(unsigned wd) noexcept
Definition weekday.hpp:22
constexpr auto operator-=(days const &d) noexcept -> weekday &
Definition weekday.hpp:64
constexpr auto operator[](unsigned index) const noexcept -> weekday_indexed
Definition weekday_indexed.hpp:46
friend constexpr auto operator==(weekday const &lhs, weekday const &rhs) noexcept -> bool
Definition weekday.hpp:92
constexpr weekday(local_days const &dp) noexcept
Definition weekday.hpp:32