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
3#ifndef TETL_CHRONO_MONTH_DAY_HPP
4#define TETL_CHRONO_MONTH_DAY_HPP
5
7#include <etl/_chrono/day.hpp>
9
10namespace etl::chrono {
11
13struct month_day {
14 month_day() = default;
15
16 constexpr month_day(chrono::month const& m, chrono::day const& d) noexcept
17 : _m{m}
18 , _d{d}
19 {
20 }
21
22 [[nodiscard]] constexpr auto month() const noexcept -> chrono::month { return _m; }
23
24 [[nodiscard]] constexpr auto day() const noexcept -> chrono::day { return _d; }
25
26 [[nodiscard]] constexpr auto ok() const noexcept -> bool
27 {
28 constexpr auto maxDaysInMonth = array<uint8_t, 12>{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
29 if (not month().ok()) {
30 return false;
31 }
32 if (static_cast<unsigned>(day()) < 1) {
33 return false;
34 }
35 return static_cast<unsigned>(day()) <= maxDaysInMonth[unsigned{month()} - 1];
36 }
37
38 friend constexpr auto operator==(month_day const& lhs, month_day const& rhs) noexcept -> bool
39 {
40 return lhs.month() == rhs.month() and lhs.day() == rhs.day();
41 }
42
43private:
45 chrono::day _d;
46};
47
48[[nodiscard]] constexpr auto operator/(month const& m, day const& d) noexcept -> month_day { return {m, d}; }
49[[nodiscard]] constexpr auto operator/(month const& m, int d) noexcept -> month_day
50{
51 return {m, day(static_cast<unsigned>(d))};
52}
53[[nodiscard]] constexpr auto operator/(int m, day const& d) noexcept -> month_day
54{
55 return {month(static_cast<unsigned>(m)), d};
56}
57[[nodiscard]] constexpr auto operator/(day const& d, month const& m) noexcept -> month_day { return {m, d}; }
58[[nodiscard]] constexpr auto operator/(day const& d, int m) noexcept -> month_day
59{
60 return {month(static_cast<unsigned>(m)), d};
61}
62
63} // namespace etl::chrono
64
65#endif // TETL_CHRONO_MONTH_DAY_HPP
Definition abs.hpp:11
constexpr auto operator/(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> common_type_t< Rep1, Rep2 >
Performs basic arithmetic operations between two durations or between a duration and a tick count.
Definition duration.hpp:272
A container that encapsulates fixed size arrays.
Definition array.hpp:48
The class day represents a day in a month.
Definition day.hpp:20
Definition month_day.hpp:13
constexpr auto ok() const noexcept -> bool
Definition month_day.hpp:26
friend constexpr auto operator==(month_day const &lhs, month_day const &rhs) noexcept -> bool
Definition month_day.hpp:38
constexpr auto month() const noexcept -> chrono::month
Definition month_day.hpp:22
constexpr month_day(chrono::month const &m, chrono::day const &d) noexcept
Definition month_day.hpp:16
constexpr auto day() const noexcept -> chrono::day
Definition month_day.hpp:24
The class month represents a month in a year.
Definition month.hpp:21