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
3#ifndef TETL_CHRONO_YEAR_MONTH_WEEKDAY_HPP
4#define TETL_CHRONO_YEAR_MONTH_WEEKDAY_HPP
5
11#include <etl/_chrono/year.hpp>
12
13namespace etl::chrono {
14
17 year_month_weekday() = default;
18
19 constexpr year_month_weekday(chrono::year const& y, chrono::month const& m, chrono::weekday_indexed const& wdi)
20 noexcept
21 : _y{y}
22 , _m{m}
23 , _wdi{wdi}
24 {
25 }
26
27 constexpr year_month_weekday(sys_days const& dp) noexcept;
28 constexpr explicit year_month_weekday(local_days const& dp) noexcept;
29
30 constexpr auto operator+=(months const& m) noexcept -> year_month_weekday&;
31 constexpr auto operator-=(months const& m) noexcept -> year_month_weekday&;
32 constexpr auto operator+=(years const& y) noexcept -> year_month_weekday&;
33 constexpr auto operator-=(years const& y) noexcept -> year_month_weekday&;
34
35 [[nodiscard]] constexpr auto year() const noexcept -> chrono::year { return _y; }
36 [[nodiscard]] constexpr auto month() const noexcept -> chrono::month { return _m; }
37 [[nodiscard]] constexpr auto weekday() const noexcept -> chrono::weekday { return _wdi.weekday(); }
38 [[nodiscard]] constexpr auto index() const noexcept -> unsigned { return _wdi.index(); }
39 [[nodiscard]] constexpr auto weekday_indexed() const noexcept -> chrono::weekday_indexed { return _wdi; }
40
41 [[nodiscard]] constexpr auto ok() const noexcept -> bool
42 {
43 if (not _y.ok() or not _m.ok() or not _wdi.weekday().ok() or _wdi.index() < 1) {
44 return false;
45 }
46 if (_wdi.index() <= 4) {
47 return true;
48 }
49 auto firstOfMonth = chrono::weekday(static_cast<sys_days>(_y / _m / 1));
50 auto d2 = _wdi.weekday() - firstOfMonth + days(static_cast<int_least32_t>((_wdi.index() - 1) * 7 + 1));
51 return static_cast<unsigned>(d2.count()) <= static_cast<unsigned>((_y / _m / last).day());
52 }
53
54 [[nodiscard]] constexpr operator sys_days() const noexcept;
55 [[nodiscard]] constexpr explicit operator local_days() const noexcept;
56
57private:
58 chrono::year _y;
59 chrono::month _m;
61};
62
63[[nodiscard]] constexpr auto operator==(year_month_weekday const& lhs, year_month_weekday const& rhs) noexcept -> bool
64{
65 return lhs.year() == rhs.year() and lhs.month() == rhs.month() and lhs.weekday_indexed() == rhs.weekday_indexed();
66}
67
68[[nodiscard]] constexpr auto operator+(year_month_weekday const& lhs, months const& rhs) noexcept -> year_month_weekday
69{
70 auto const ym = year_month{lhs.year(), lhs.month()} + rhs;
71 return {ym.year(), ym.month(), lhs.weekday_indexed()};
72}
73
74[[nodiscard]] constexpr auto operator+(months const& lhs, year_month_weekday const& rhs) noexcept -> year_month_weekday
75{
76 return rhs + lhs;
77}
78
79[[nodiscard]] constexpr auto operator-(year_month_weekday const& lhs, months const& rhs) noexcept -> year_month_weekday
80{
81 return lhs + -rhs;
82}
83
84[[nodiscard]] constexpr auto operator+(year_month_weekday const& lhs, years const& rhs) noexcept -> year_month_weekday
85{
86 return year_month_weekday{lhs.year() + rhs, lhs.month(), lhs.weekday_indexed()};
87}
88
89[[nodiscard]] constexpr auto operator+(years const& lhs, year_month_weekday const& rhs) noexcept -> year_month_weekday
90{
91 return rhs + lhs;
92}
93
94[[nodiscard]] constexpr auto operator-(year_month_weekday const& lhs, years const& rhs) noexcept -> year_month_weekday
95{
96 return lhs + -rhs;
97}
98
99} // namespace etl::chrono
100
101#endif // TETL_CHRONO_YEAR_MONTH_WEEKDAY_HPP
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< 31556952 > > months
Signed integer type of at least 20 bits.
Definition duration.hpp:371
duration< int_least32_t, ratio< 86400 > > days
Signed integer type of at least 25 bits.
Definition duration.hpp:365
sys_time< chrono::days > sys_days
Definition system_clock.hpp:39
duration< int_least32_t, ratio< 2629746 > > years
Signed integer type of at least 17 bits.
Definition duration.hpp:374
constexpr auto operator-(day const &d, days const &ds) noexcept -> day
Definition day.hpp:102
TETL_BUILTIN_INT32 int_least32_t
Signed integer type with width of at least 32 bits.
Definition int_least_t.hpp:17
The class month represents a month in a year.
Definition month.hpp:21
Definition weekday_indexed.hpp:11
Definition weekday.hpp:18
Definition year_month_weekday.hpp:16
constexpr auto index() const noexcept -> unsigned
Definition year_month_weekday.hpp:38
constexpr year_month_weekday(chrono::year const &y, chrono::month const &m, chrono::weekday_indexed const &wdi) noexcept
Definition year_month_weekday.hpp:19
constexpr auto ok() const noexcept -> bool
Definition year_month_weekday.hpp:41
constexpr auto weekday_indexed() const noexcept -> chrono::weekday_indexed
Definition year_month_weekday.hpp:39
constexpr auto month() const noexcept -> chrono::month
Definition year_month_weekday.hpp:36
constexpr auto weekday() const noexcept -> chrono::weekday
Definition year_month_weekday.hpp:37
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:35
constexpr year_month_weekday(local_days const &dp) noexcept
constexpr auto operator+=(years const &y) noexcept -> year_month_weekday &
constexpr auto operator+=(months const &m) noexcept -> year_month_weekday &
Definition year_month.hpp:11
Definition year.hpp:13