tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
year_month_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_YEAR_MONTH_WEEKDAY_HPP
5#define TETL_CHRONO_YEAR_MONTH_WEEKDAY_HPP
6
7#include <etl/_chrono/local_t.hpp>
8#include <etl/_chrono/month.hpp>
9#include <etl/_chrono/system_clock.hpp>
10#include <etl/_chrono/weekday.hpp>
11#include <etl/_chrono/weekday_indexed.hpp>
12#include <etl/_chrono/year.hpp>
13
14namespace etl::chrono {
15
16/// \ingroup chrono
18 year_month_weekday() = default;
19
21 chrono::year const& y,
22 chrono::month const& m,
23 chrono::weekday_indexed const& wdi
24 ) noexcept
25 : _y{y}
26 , _m{m}
27 , _wdi{wdi}
28 {
29 }
30
31 constexpr year_month_weekday(sys_days const& dp) noexcept;
32 constexpr explicit year_month_weekday(local_days const& dp) noexcept;
33
34 constexpr auto operator+=(months const& m) noexcept -> year_month_weekday&;
35 constexpr auto operator-=(months const& m) noexcept -> year_month_weekday&;
36 constexpr auto operator+=(years const& y) noexcept -> year_month_weekday&;
37 constexpr auto operator-=(years const& y) noexcept -> year_month_weekday&;
38
39 [[nodiscard]] constexpr auto year() const noexcept -> chrono::year
40 {
41 return _y;
42 }
43 [[nodiscard]] constexpr auto month() const noexcept -> chrono::month
44 {
45 return _m;
46 }
47 [[nodiscard]] constexpr auto weekday() const noexcept -> chrono::weekday
48 {
49 return _wdi.weekday();
50 }
51 [[nodiscard]] constexpr auto index() const noexcept -> unsigned
52 {
53 return _wdi.index();
54 }
55 [[nodiscard]] constexpr auto weekday_indexed() const noexcept -> chrono::weekday_indexed
56 {
57 return _wdi;
58 }
59
60 [[nodiscard]] constexpr auto ok() const noexcept -> bool
61 {
62 if (not _y.ok() or not _m.ok() or not _wdi.weekday().ok() or _wdi.index() < 1) {
63 return false;
64 }
65 if (_wdi.index() <= 4) {
66 return true;
67 }
68 auto firstOfMonth = chrono::weekday(static_cast<sys_days>(_y / _m / 1));
69 auto d2 = _wdi.weekday() - firstOfMonth + days(static_cast<int_least32_t>(((_wdi.index() - 1) * 7) + 1));
70
71 // NOLINTNEXTLINE(modernize-use-integer-sign-comparison)
72 return static_cast<unsigned>(d2.count()) <= static_cast<unsigned>((_y / _m / last).day());
73 }
74
75 [[nodiscard]] constexpr operator sys_days() const noexcept;
76 [[nodiscard]] constexpr explicit operator local_days() const noexcept;
77
78private:
79 chrono::year _y;
80 chrono::month _m;
82};
83
84[[nodiscard]] constexpr auto operator==(year_month_weekday const& lhs, year_month_weekday const& rhs) noexcept -> bool
85{
87}
88
89[[nodiscard]] constexpr auto operator+(year_month_weekday const& lhs, months const& rhs) noexcept -> year_month_weekday
90{
91 auto const ym = year_month{lhs.year(), lhs.month()} + rhs;
92 return {ym.year(), ym.month(), lhs.weekday_indexed()};
93}
94
95[[nodiscard]] constexpr auto operator+(months const& lhs, year_month_weekday const& rhs) noexcept -> year_month_weekday
96{
97 return rhs + lhs;
98}
99
100[[nodiscard]] constexpr auto operator-(year_month_weekday const& lhs, months const& rhs) noexcept -> year_month_weekday
101{
102 return lhs + -rhs;
103}
104
105[[nodiscard]] constexpr auto operator+(year_month_weekday const& lhs, years const& rhs) noexcept -> year_month_weekday
106{
108}
109
110[[nodiscard]] constexpr auto operator+(years const& lhs, year_month_weekday const& rhs) noexcept -> year_month_weekday
111{
112 return rhs + lhs;
113}
114
115[[nodiscard]] constexpr auto operator-(year_month_weekday const& lhs, years const& rhs) noexcept -> year_month_weekday
116{
117 return lhs + -rhs;
118}
119
120} // namespace etl::chrono
121
122#endif // TETL_CHRONO_YEAR_MONTH_WEEKDAY_HPP
constexpr auto last
Definition last_spec.hpp:26
Definition abs.hpp:12
constexpr auto operator-(year_month_weekday const &lhs, months const &rhs) noexcept -> year_month_weekday
Definition year_month_weekday.hpp:100
constexpr auto operator-(year_month_weekday const &lhs, years const &rhs) noexcept -> year_month_weekday
Definition year_month_weekday.hpp:115
constexpr auto operator==(year_month_weekday const &lhs, year_month_weekday const &rhs) noexcept -> bool
Definition year_month_weekday.hpp:84
constexpr auto operator+(years const &lhs, year_month_weekday const &rhs) noexcept -> year_month_weekday
Definition year_month_weekday.hpp:110
constexpr auto operator+(months const &lhs, year_month_weekday const &rhs) noexcept -> year_month_weekday
Definition year_month_weekday.hpp:95
constexpr auto operator==(month lhs, month rhs) noexcept -> bool
Definition month.hpp:53
constexpr auto operator+(year_month_weekday const &lhs, years const &rhs) noexcept -> year_month_weekday
Definition year_month_weekday.hpp:105
constexpr auto operator+(year const &lhs, years const &rhs) noexcept -> year
Definition year.hpp:120
constexpr auto operator+(year_month_weekday const &lhs, months const &rhs) noexcept -> year_month_weekday
Definition year_month_weekday.hpp:89
Definition adjacent_find.hpp:9
constexpr auto operator-() const -> etl::common_type_t< duration >
Implements unary plus and unary minus for the durations.
Definition duration.hpp:128
The class month represents a month in a year.
Definition month.hpp:22
constexpr auto ok() const noexcept -> bool
Definition month.hpp:31
Definition weekday_indexed.hpp:12
constexpr auto index() const noexcept -> unsigned
Definition weekday_indexed.hpp:26
constexpr auto weekday() const noexcept -> etl::chrono::weekday
Definition weekday_indexed.hpp:21
friend constexpr auto operator==(weekday_indexed const &lhs, weekday_indexed const &rhs) noexcept -> bool
Definition weekday_indexed.hpp:36
Definition weekday.hpp:19
constexpr auto ok() const noexcept -> bool
Definition weekday.hpp:81
Definition year_month_weekday.hpp:17
constexpr auto index() const noexcept -> unsigned
Definition year_month_weekday.hpp:51
constexpr year_month_weekday(chrono::year const &y, chrono::month const &m, chrono::weekday_indexed const &wdi) noexcept
Definition year_month_weekday.hpp:20
constexpr auto ok() const noexcept -> bool
Definition year_month_weekday.hpp:60
constexpr auto weekday_indexed() const noexcept -> chrono::weekday_indexed
Definition year_month_weekday.hpp:55
constexpr auto month() const noexcept -> chrono::month
Definition year_month_weekday.hpp:43
constexpr auto weekday() const noexcept -> chrono::weekday
Definition year_month_weekday.hpp:47
constexpr auto operator-=(months const &m) noexcept -> year_month_weekday &
constexpr year_month_weekday(sys_days const &dp) noexcept
constexpr auto operator-=(years const &y) noexcept -> year_month_weekday &
constexpr auto year() const noexcept -> chrono::year
Definition year_month_weekday.hpp:39
constexpr year_month_weekday(local_days const &dp) noexcept
constexpr auto operator+=(years const &y) noexcept -> year_month_weekday &
constexpr operator sys_days() const noexcept
constexpr auto operator+=(months const &m) noexcept -> year_month_weekday &
Definition year.hpp:14
constexpr auto ok() const noexcept -> bool
Definition year.hpp:76
friend constexpr auto operator==(year lhs, year rhs) noexcept -> bool
Definition year.hpp:91