tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
year_month.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_CHRONO_YEAR_MONTH_HPP
4#define TETL_CHRONO_YEAR_MONTH_HPP
5
7
8namespace etl::chrono {
9
11struct year_month {
12 year_month() = default;
13
14 constexpr year_month(chrono::year const& y, chrono::month const& m) noexcept
15 : _y{y}
16 , _m{m}
17 {
18 }
19
20 [[nodiscard]] constexpr auto ok() const noexcept -> bool { return year().ok() and month().ok(); }
21 [[nodiscard]] constexpr auto year() const noexcept -> chrono::year { return _y; }
22 [[nodiscard]] constexpr auto month() const noexcept -> chrono::month { return _m; }
23
24 constexpr auto operator+=(months const& dm) noexcept -> year_month&;
25 constexpr auto operator-=(months const& dm) noexcept -> year_month&;
26 constexpr auto operator+=(years const& dy) noexcept -> year_month&;
27 constexpr auto operator-=(years const& dy) noexcept -> year_month&;
28
29 friend constexpr auto operator==(year_month const& lhs, year_month const& rhs) noexcept -> bool
30 {
31 return lhs.year() == rhs.year() and lhs.month() == rhs.month();
32 }
33
34private:
35 chrono::year _y;
37};
38
39[[nodiscard]] constexpr auto operator+(chrono::year_month const& ym, chrono::years const& dy) noexcept
41{
42 return chrono::year_month{ym.year() + dy, ym.month()};
43}
44
45[[nodiscard]] constexpr auto operator+(chrono::years const& dy, chrono::year_month const& ym) noexcept
47{
48 return chrono::year_month{ym.year() + dy, ym.month()};
49}
50
51[[nodiscard]] constexpr auto operator+(chrono::year_month const& ym, chrono::months const& dm) noexcept
53{
54 return {ym.year(), ym.month() + dm};
55}
56
57[[nodiscard]] constexpr auto operator+(chrono::months const& dm, chrono::year_month const& ym) noexcept
59{
60 return {ym.year(), ym.month() + dm};
61}
62
63[[nodiscard]] constexpr auto operator-(chrono::year_month const& ym, chrono::years const& dy) noexcept
65{
66 return {ym.year() - dy, ym.month()};
67}
68
69[[nodiscard]] constexpr auto operator-(chrono::year_month const& ym, chrono::months const& dm) noexcept
71{
72 return {ym.year(), ym.month() - dm};
73}
74
75// [[nodiscard]] constexpr auto operator-(chrono::year_month const& ym1, chrono::year_month const&
76// ym2) noexcept
77// -> chrono::months
78// {
79// }
80
81constexpr auto year_month::operator+=(months const& dm) noexcept -> year_month&
82{
83 *this = *this + dm;
84 return *this;
85}
86
87constexpr auto year_month::operator-=(months const& dm) noexcept -> year_month&
88{
89 *this = *this - dm;
90 return *this;
91}
92
93constexpr auto year_month::operator+=(years const& dy) noexcept -> year_month&
94{
95 *this = *this + dy;
96 return *this;
97}
98
99constexpr auto year_month::operator-=(years const& dy) noexcept -> year_month&
100{
101 *this = *this - dy;
102 return *this;
103}
104
105[[nodiscard]] constexpr auto operator/(year const& y, month const& m) noexcept -> year_month
106{
107 return year_month{y, m};
108}
109
110[[nodiscard]] constexpr auto operator/(year const& y, int m) noexcept -> year_month
111{
112 return year_month{y, month(static_cast<unsigned>(m))};
113}
114
115} // namespace etl::chrono
116
117#endif // TETL_CHRONO_YEAR_MONTH_HPP
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
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
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
The class month represents a month in a year.
Definition month.hpp:21
Definition year_month.hpp:11
friend constexpr auto operator==(year_month const &lhs, year_month const &rhs) noexcept -> bool
Definition year_month.hpp:29
constexpr auto ok() const noexcept -> bool
Definition year_month.hpp:20
constexpr auto month() const noexcept -> chrono::month
Definition year_month.hpp:22
constexpr auto operator-=(months const &dm) noexcept -> year_month &
Definition year_month.hpp:87
constexpr auto operator+=(months const &dm) noexcept -> year_month &
Definition year_month.hpp:81
constexpr auto year() const noexcept -> chrono::year
Definition year_month.hpp:21
constexpr year_month(chrono::year const &y, chrono::month const &m) noexcept
Definition year_month.hpp:14
Definition year.hpp:13