tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
month_day.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_MONTH_DAY_HPP
5#define TETL_CHRONO_MONTH_DAY_HPP
6
7#include <etl/_array/array.hpp>
8#include <etl/_chrono/day.hpp>
9#include <etl/_chrono/month.hpp>
10
11namespace etl::chrono {
12
13/// \ingroup chrono
14struct month_day {
15 month_day() = default;
16
17 constexpr month_day(chrono::month const& m, chrono::day const& d) noexcept
18 : _m{m}
19 , _d{d}
20 {
21 }
22
23 [[nodiscard]] constexpr auto month() const noexcept -> chrono::month
24 {
25 return _m;
26 }
27
28 [[nodiscard]] constexpr auto day() const noexcept -> chrono::day
29 {
30 return _d;
31 }
32
33 [[nodiscard]] constexpr auto ok() const noexcept -> bool
34 {
35 constexpr auto maxDaysInMonth = array<uint8_t, 12>{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
36 if (not month().ok()) {
37 return false;
38 }
39 if (static_cast<unsigned>(day()) < 1) {
40 return false;
41 }
42 return static_cast<unsigned>(day()) <= maxDaysInMonth[unsigned{month()} - 1];
43 }
44
45 friend constexpr auto operator==(month_day const& lhs, month_day const& rhs) noexcept -> bool
46 {
47 return lhs.month() == rhs.month() and lhs.day() == rhs.day();
48 }
49
50private:
51 chrono::month _m;
52 chrono::day _d;
53};
54
55[[nodiscard]] constexpr auto operator/(month const& m, day const& d) noexcept -> month_day
56{
57 return {m, d};
58}
59[[nodiscard]] constexpr auto operator/(month const& m, int d) noexcept -> month_day
60{
61 return {m, day(static_cast<unsigned>(d))};
62}
63[[nodiscard]] constexpr auto operator/(int m, day const& d) noexcept -> month_day
64{
65 return {month(static_cast<unsigned>(m)), d};
66}
67[[nodiscard]] constexpr auto operator/(day const& d, month const& m) noexcept -> month_day
68{
69 return {m, d};
70}
71[[nodiscard]] constexpr auto operator/(day const& d, int m) noexcept -> month_day
72{
73 return {month(static_cast<unsigned>(m)), d};
74}
75
76} // namespace etl::chrono
77
78#endif // TETL_CHRONO_MONTH_DAY_HPP
Definition abs.hpp:12
constexpr auto operator/(month const &m, day const &d) noexcept -> month_day
Definition month_day.hpp:55
constexpr auto operator/(month const &m, int d) noexcept -> month_day
Definition month_day.hpp:59
constexpr auto operator==(month lhs, month rhs) noexcept -> bool
Definition month.hpp:53
constexpr auto operator/(day const &d, month const &m) noexcept -> month_day
Definition month_day.hpp:67
constexpr auto operator/(day const &d, int m) noexcept -> month_day
Definition month_day.hpp:71
constexpr auto operator/(int m, day const &d) noexcept -> month_day
Definition month_day.hpp:63
Definition adjacent_find.hpp:9
A container that encapsulates fixed size arrays.
Definition array.hpp:49
constexpr auto operator[](size_type const pos) const noexcept -> const_reference
Accesses the specified item with range checking.
Definition array.hpp:74
The class day represents a day in a month.
Definition day.hpp:21
friend constexpr auto operator==(day lhs, day rhs) noexcept -> bool
Definition day.hpp:76
constexpr day(unsigned d) noexcept
Definition day.hpp:24
Definition month_day.hpp:14
constexpr auto ok() const noexcept -> bool
Definition month_day.hpp:33
friend constexpr auto operator==(month_day const &lhs, month_day const &rhs) noexcept -> bool
Definition month_day.hpp:45
constexpr auto month() const noexcept -> chrono::month
Definition month_day.hpp:23
constexpr month_day(chrono::month const &m, chrono::day const &d) noexcept
Definition month_day.hpp:17
constexpr auto day() const noexcept -> chrono::day
Definition month_day.hpp:28
The class month represents a month in a year.
Definition month.hpp:22
constexpr auto ok() const noexcept -> bool
Definition month.hpp:31
constexpr month(unsigned m) noexcept
Definition month.hpp:25