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
3#ifndef TETL_CHRONO_WEEKDAY_HPP
4#define TETL_CHRONO_WEEKDAY_HPP
5
11
12namespace etl::chrono {
13
14struct weekday_indexed;
15struct weekday_last;
16
18struct weekday {
19 weekday() = default;
20
21 constexpr explicit weekday(unsigned wd) noexcept
22 : _wd{static_cast<etl::uint8_t>(wd == 7 ? 0 : wd)}
23 {
24 }
25
26 constexpr weekday(sys_days const& dp) noexcept
27 : _wd{weekday_from_days(dp.time_since_epoch().count())}
28 {
29 }
30
31 constexpr explicit weekday(local_days const& dp) noexcept
32 : _wd{weekday_from_days(dp.time_since_epoch().count())}
33 {
34 }
35
36 constexpr auto operator++() noexcept -> weekday& { return *this += etl::chrono::days{1}; }
37
38 constexpr auto operator++(int) noexcept -> weekday { return *this += etl::chrono::days{1}; }
39
40 constexpr auto operator--() noexcept -> weekday& { return *this -= etl::chrono::days{1}; }
41
42 constexpr auto operator--(int) noexcept -> weekday { return *this -= etl::chrono::days{1}; }
43
44 constexpr auto operator+=(days const& d) noexcept -> weekday&
45 {
46 _wd += d.count();
47 _wd %= 7;
48 return *this;
49 }
50
51 constexpr auto operator-=(days const& d) noexcept -> weekday&
52 {
53 _wd -= d.count();
54 _wd %= 7;
55 return *this;
56 }
57
58 [[nodiscard]] constexpr auto c_encoding() const noexcept -> unsigned { return _wd; }
59
60 [[nodiscard]] constexpr auto iso_encoding() const noexcept -> unsigned { return _wd == 0U ? 7U : _wd; }
61
62 [[nodiscard]] constexpr auto ok() const noexcept -> bool { return _wd < 7U; }
63
64 // Defined in weekday_indexed
65 [[nodiscard]] constexpr auto operator[](unsigned index) const noexcept -> weekday_indexed;
66
67 // Defined in weekday_last
68 [[nodiscard]] constexpr auto operator[](etl::chrono::last_spec /*tag*/) const noexcept -> weekday_last;
69
70 friend constexpr auto operator==(weekday const& lhs, weekday const& rhs) noexcept -> bool
71 {
72 return lhs.c_encoding() == rhs.c_encoding();
73 }
74
75private:
76 [[nodiscard]] static constexpr auto weekday_from_days(int tp) noexcept -> etl::uint8_t
77 {
78 return static_cast<etl::uint8_t>(tp >= -4 ? (tp + 4) % 7 : (tp + 5) % 7 + 6);
79 }
80
81 etl::uint8_t _wd;
82};
83
84[[nodiscard]] constexpr auto operator+(weekday const& lhs, days const& rhs) noexcept -> weekday
85{
86 return weekday{static_cast<unsigned>((static_cast<int32_t>(lhs.c_encoding()) + rhs.count()) % 7)};
87}
88
89[[nodiscard]] constexpr auto operator+(days const& lhs, weekday const& rhs) noexcept -> weekday { return rhs + lhs; }
90
91[[nodiscard]] constexpr auto operator-(weekday const& lhs, days const& rhs) noexcept -> weekday
92{
93 return weekday{static_cast<unsigned>((static_cast<int32_t>(lhs.c_encoding()) - rhs.count()) % 7)};
94}
95
96[[nodiscard]] constexpr auto operator-(weekday const& lhs, weekday const& rhs) noexcept -> days
97{
98 auto const count = static_cast<int_least32_t>(lhs.c_encoding() - rhs.c_encoding());
99 return count >= 0 ? days{count} : days{count + 7};
100}
101
102inline constexpr auto Sunday = etl::chrono::weekday{0}; // NOLINT(readability-identifier-naming)
103inline constexpr auto Monday = etl::chrono::weekday{1}; // NOLINT(readability-identifier-naming)
104inline constexpr auto Tuesday = etl::chrono::weekday{2}; // NOLINT(readability-identifier-naming)
105inline constexpr auto Wednesday = etl::chrono::weekday{3}; // NOLINT(readability-identifier-naming)
106inline constexpr auto Thursday = etl::chrono::weekday{4}; // NOLINT(readability-identifier-naming)
107inline constexpr auto Friday = etl::chrono::weekday{5}; // NOLINT(readability-identifier-naming)
108inline constexpr auto Saturday = etl::chrono::weekday{6}; // NOLINT(readability-identifier-naming)
109
110} // namespace etl::chrono
111
112#endif // TETL_CHRONO_WEEKDAY_HPP
constexpr auto count(InputIt first, InputIt last, T const &value) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count.hpp:21
local_time< etl::chrono::days > local_days
Definition local_t.hpp:27
Definition abs.hpp:11
constexpr auto operator+(day const &d, days const &ds) noexcept -> day
Definition day.hpp:92
duration< int_least32_t, ratio< 86400 > > days
Signed integer type of at least 25 bits.
Definition duration.hpp:365
constexpr auto Saturday
Definition weekday.hpp:108
sys_time< chrono::days > sys_days
Definition system_clock.hpp:39
constexpr auto Friday
Definition weekday.hpp:107
constexpr auto Wednesday
Definition weekday.hpp:105
constexpr auto Tuesday
Definition weekday.hpp:104
constexpr auto operator-(day const &d, days const &ds) noexcept -> day
Definition day.hpp:102
constexpr auto Monday
Definition weekday.hpp:103
constexpr auto Thursday
Definition weekday.hpp:106
constexpr auto Sunday
Definition weekday.hpp:102
TETL_BUILTIN_INT32 int32_t
Signed integer type with width of exactly 32 bits.
Definition int_t.hpp:17
TETL_BUILTIN_UINT8 uint8_t
Unsigned integer type with width of exactly 8 bits.
Definition uint_t.hpp:11
TETL_BUILTIN_INT32 int_least32_t
Signed integer type with width of at least 32 bits.
Definition int_least_t.hpp:17
last_spec is an empty tag type that is used in conjunction with other calendar types to indicate the ...
Definition last_spec.hpp:19
Definition weekday_indexed.hpp:11
Definition weekday_last.hpp:11
Definition weekday.hpp:18
constexpr auto operator--(int) noexcept -> weekday
Definition weekday.hpp:42
constexpr auto operator--() noexcept -> weekday &
Definition weekday.hpp:40
constexpr auto ok() const noexcept -> bool
Definition weekday.hpp:62
constexpr weekday(sys_days const &dp) noexcept
Definition weekday.hpp:26
constexpr auto operator+=(days const &d) noexcept -> weekday &
Definition weekday.hpp:44
constexpr auto operator++(int) noexcept -> weekday
Definition weekday.hpp:38
constexpr auto operator++() noexcept -> weekday &
Definition weekday.hpp:36
constexpr auto iso_encoding() const noexcept -> unsigned
Definition weekday.hpp:60
constexpr auto c_encoding() const noexcept -> unsigned
Definition weekday.hpp:58
constexpr weekday(unsigned wd) noexcept
Definition weekday.hpp:21
constexpr auto operator-=(days const &d) noexcept -> weekday &
Definition weekday.hpp:51
constexpr auto operator[](unsigned index) const noexcept -> weekday_indexed
Definition weekday_indexed.hpp:39
friend constexpr auto operator==(weekday const &lhs, weekday const &rhs) noexcept -> bool
Definition weekday.hpp:70
constexpr weekday(local_days const &dp) noexcept
Definition weekday.hpp:31