tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
month.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_CHRONO_MONTH_HPP
4#define TETL_CHRONO_MONTH_HPP
5
9
10namespace etl::chrono {
11
21struct month {
22 month() = default;
23
24 constexpr explicit month(unsigned m) noexcept
25 : _count{static_cast<unsigned char>(m)}
26 {
28 }
29
30 [[nodiscard]] constexpr auto ok() const noexcept -> bool { return (_count > 0U) and (_count <= 12U); }
31
32 constexpr explicit operator unsigned() const noexcept { return _count; }
33
34 constexpr auto operator++() noexcept -> month&;
35 constexpr auto operator++(int) noexcept -> month;
36 constexpr auto operator--() noexcept -> month&;
37 constexpr auto operator--(int) noexcept -> month;
38
39 constexpr auto operator+=(months const& m) noexcept -> month&;
40 constexpr auto operator-=(months const& m) noexcept -> month&;
41
42private:
43 unsigned char _count;
44};
45
46[[nodiscard]] constexpr auto operator==(month lhs, month rhs) noexcept -> bool
47{
48 return static_cast<unsigned>(lhs) == static_cast<unsigned>(rhs);
49}
50
51[[nodiscard]] constexpr auto operator<(month lhs, month rhs) noexcept -> bool
52{
53 return static_cast<unsigned>(lhs) < static_cast<unsigned>(rhs);
54}
55
56[[nodiscard]] constexpr auto operator<=(month lhs, month rhs) noexcept -> bool
57{
58 return static_cast<unsigned>(lhs) <= static_cast<unsigned>(rhs);
59}
60
61[[nodiscard]] constexpr auto operator>(month lhs, month rhs) noexcept -> bool
62{
63 return static_cast<unsigned>(lhs) > static_cast<unsigned>(rhs);
64}
65
66[[nodiscard]] constexpr auto operator>=(month lhs, month rhs) noexcept -> bool
67{
68 return static_cast<unsigned>(lhs) >= static_cast<unsigned>(rhs);
69}
70
71[[nodiscard]] constexpr auto operator+(month const& m, months const& ms) noexcept -> month
72{
73 auto const mo = static_cast<long long>(static_cast<unsigned>(m)) + (ms.count() - 1);
74 auto const div = (mo >= 0 ? mo : mo - 11) / 12;
75 return month{static_cast<unsigned int>(mo - div * 12 + 1)};
76}
77
78[[nodiscard]] constexpr auto operator+(months const& ms, month const& m) noexcept -> month { return m + ms; }
79
80[[nodiscard]] constexpr auto operator-(month const& m, months const& ms) noexcept -> month { return m + -ms; }
81
82[[nodiscard]] constexpr auto operator-(month const& m1, month const& m2) noexcept -> months
83{
84 auto const delta = static_cast<unsigned>(m1) - static_cast<unsigned>(m2);
85 return months{static_cast<etl::int_least32_t>(delta <= 11 ? delta : delta + 12)};
86}
87
88constexpr auto month::operator++() noexcept -> month&
89{
90 *this = *this + months{1};
91 return *this;
92}
93
94constexpr auto month::operator++(int) noexcept -> month
95{
96 auto tmp = *this;
97 ++(*this);
98 return tmp;
99}
100
101constexpr auto month::operator--() noexcept -> month&
102{
103 *this = *this - months{1};
104 return *this;
105}
106
107constexpr auto month::operator--(int) noexcept -> month
108{
109 auto tmp = *this;
110 --(*this);
111 return tmp;
112}
113
114constexpr auto month::operator+=(months const& m) noexcept -> month&
115{
116 *this = *this + m;
117 return *this;
118}
119
120constexpr auto month::operator-=(months const& m) noexcept -> month&
121{
122 *this = *this - m;
123 return *this;
124}
125
126inline constexpr auto January = etl::chrono::month{1};
127inline constexpr auto February = etl::chrono::month{2};
128inline constexpr auto March = etl::chrono::month{3};
129inline constexpr auto April = etl::chrono::month{4};
130inline constexpr auto May = etl::chrono::month{5};
131inline constexpr auto June = etl::chrono::month{6};
132inline constexpr auto July = etl::chrono::month{7};
133inline constexpr auto August = etl::chrono::month{8};
134inline constexpr auto September = etl::chrono::month{9};
135inline constexpr auto October = etl::chrono::month{10};
136inline constexpr auto November = etl::chrono::month{11};
137inline constexpr auto December = etl::chrono::month{12};
138
139} // namespace etl::chrono
140
141#endif // TETL_CHRONO_MONTH_HPP
#define TETL_PRECONDITION(...)
Definition check.hpp:16
Definition abs.hpp:11
constexpr auto February
Definition month.hpp:127
constexpr auto operator<(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:316
constexpr auto operator+(day const &d, days const &ds) noexcept -> day
Definition day.hpp:92
constexpr auto operator>(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:333
constexpr auto July
Definition month.hpp:132
duration< int_least32_t, ratio< 31556952 > > months
Signed integer type of at least 20 bits.
Definition duration.hpp:371
constexpr auto October
Definition month.hpp:135
constexpr auto January
Definition month.hpp:126
constexpr auto August
Definition month.hpp:133
constexpr auto June
Definition month.hpp:131
constexpr auto March
Definition month.hpp:128
constexpr auto April
Definition month.hpp:129
constexpr auto operator<=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:325
constexpr auto May
Definition month.hpp:130
constexpr auto November
Definition month.hpp:136
constexpr auto September
Definition month.hpp:134
constexpr auto December
Definition month.hpp:137
constexpr auto operator-(day const &d, days const &ds) noexcept -> day
Definition day.hpp:102
constexpr auto operator>=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:341
constexpr auto div(int x, int y) noexcept -> div_t
Computes both the quotient and the remainder of the division of the numerator x by the denominator y....
Definition div.hpp:37
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
constexpr auto operator-=(months const &m) noexcept -> month &
Definition month.hpp:120
constexpr auto ok() const noexcept -> bool
Definition month.hpp:30
constexpr auto operator+=(months const &m) noexcept -> month &
Definition month.hpp:114
constexpr month(unsigned m) noexcept
Definition month.hpp:24
constexpr auto operator--() noexcept -> month &
Definition month.hpp:101
constexpr auto operator++() noexcept -> month &
Definition month.hpp:88
static constexpr auto max() noexcept
Definition numeric_limits.hpp:21