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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_CHRONO_MONTH_HPP
5#define TETL_CHRONO_MONTH_HPP
6
7#include <etl/_chrono/duration.hpp>
8#include <etl/_contracts/check.hpp>
9#include <etl/_limits/numeric_limits.hpp>
10
11namespace etl::chrono {
12
13/// The class month represents a month in a year.
14///
15/// Its normal range is [1, 12], but it may hold any number in [​0​, 255].
16/// Twelve named constants are predefined in the etl::chrono namespace
17/// for the twelve months of the year.
18///
19/// - https://en.cppreference.com/w/cpp/chrono/month
20///
21/// \ingroup chrono
22struct month {
23 month() = default;
24
25 constexpr explicit month(unsigned m) noexcept
26 : _count{static_cast<unsigned char>(m)}
27 {
28 TETL_PRECONDITION(m < etl::numeric_limits<unsigned char>::max());
29 }
30
31 [[nodiscard]] constexpr auto ok() const noexcept -> bool
32 {
33 return (_count > 0U) and (_count <= 12U);
34 }
35
36 constexpr explicit operator unsigned() const noexcept
37 {
38 return _count;
39 }
40
41 constexpr auto operator++() noexcept -> month&;
42 constexpr auto operator++(int) noexcept -> month;
43 constexpr auto operator--() noexcept -> month&;
44 constexpr auto operator--(int) noexcept -> month;
45
46 constexpr auto operator+=(months const& m) noexcept -> month&;
47 constexpr auto operator-=(months const& m) noexcept -> month&;
48
49private:
50 unsigned char _count;
51};
52
53[[nodiscard]] constexpr auto operator==(month lhs, month rhs) noexcept -> bool
54{
55 return static_cast<unsigned>(lhs) == static_cast<unsigned>(rhs);
56}
57
58[[nodiscard]] constexpr auto operator<(month lhs, month rhs) noexcept -> bool
59{
60 return static_cast<unsigned>(lhs) < static_cast<unsigned>(rhs);
61}
62
63[[nodiscard]] constexpr auto operator<=(month lhs, month rhs) noexcept -> bool
64{
65 return static_cast<unsigned>(lhs) <= static_cast<unsigned>(rhs);
66}
67
68[[nodiscard]] constexpr auto operator>(month lhs, month rhs) noexcept -> bool
69{
70 return static_cast<unsigned>(lhs) > static_cast<unsigned>(rhs);
71}
72
73[[nodiscard]] constexpr auto operator>=(month lhs, month rhs) noexcept -> bool
74{
75 return static_cast<unsigned>(lhs) >= static_cast<unsigned>(rhs);
76}
77
78[[nodiscard]] constexpr auto operator+(month const& m, months const& ms) noexcept -> month
79{
80 auto const mo = static_cast<long long>(static_cast<unsigned>(m)) + (ms.count() - 1);
81 auto const div = (mo >= 0 ? mo : mo - 11) / 12;
82 return month{static_cast<unsigned int>(mo - (div * 12) + 1)};
83}
84
85[[nodiscard]] constexpr auto operator+(months const& ms, month const& m) noexcept -> month
86{
87 return m + ms;
88}
89
90[[nodiscard]] constexpr auto operator-(month const& m, months const& ms) noexcept -> month
91{
92 return m + -ms;
93}
94
95[[nodiscard]] constexpr auto operator-(month const& m1, month const& m2) noexcept -> months
96{
97 auto const delta = static_cast<unsigned>(m1) - static_cast<unsigned>(m2);
98 return months{static_cast<etl::int_least32_t>(delta <= 11 ? delta : delta + 12)};
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--() noexcept -> month&
115{
116 *this = *this - months{1};
117 return *this;
118}
119
120constexpr auto month::operator--(int) noexcept -> month
121{
122 auto tmp = *this;
123 --(*this);
124 return tmp;
125}
126
127constexpr auto month::operator+=(months const& m) noexcept -> month&
128{
129 *this = *this + m;
130 return *this;
131}
132
133constexpr auto month::operator-=(months const& m) noexcept -> month&
134{
135 *this = *this - m;
136 return *this;
137}
138
139inline constexpr auto January = etl::chrono::month{1};
140inline constexpr auto February = etl::chrono::month{2};
141inline constexpr auto March = etl::chrono::month{3};
142inline constexpr auto April = etl::chrono::month{4};
143inline constexpr auto May = etl::chrono::month{5};
144inline constexpr auto June = etl::chrono::month{6};
145inline constexpr auto July = etl::chrono::month{7};
146inline constexpr auto August = etl::chrono::month{8};
147inline constexpr auto September = etl::chrono::month{9};
148inline constexpr auto October = etl::chrono::month{10};
149inline constexpr auto November = etl::chrono::month{11};
150inline constexpr auto December = etl::chrono::month{12};
151
152} // namespace etl::chrono
153
154#endif // TETL_CHRONO_MONTH_HPP
Definition abs.hpp:12
constexpr auto February
Definition month.hpp:140
constexpr auto July
Definition month.hpp:145
constexpr auto October
Definition month.hpp:148
constexpr auto January
Definition month.hpp:139
constexpr auto August
Definition month.hpp:146
constexpr auto June
Definition month.hpp:144
constexpr auto operator+(month const &m, months const &ms) noexcept -> month
Definition month.hpp:78
constexpr auto operator<=(month lhs, month rhs) noexcept -> bool
Definition month.hpp:63
constexpr auto March
Definition month.hpp:141
constexpr auto April
Definition month.hpp:142
constexpr auto operator+(months const &ms, month const &m) noexcept -> month
Definition month.hpp:85
constexpr auto May
Definition month.hpp:143
constexpr auto November
Definition month.hpp:149
constexpr auto September
Definition month.hpp:147
constexpr auto operator==(month lhs, month rhs) noexcept -> bool
Definition month.hpp:53
constexpr auto operator>(month lhs, month rhs) noexcept -> bool
Definition month.hpp:68
constexpr auto operator-(month const &m, months const &ms) noexcept -> month
Definition month.hpp:90
constexpr auto December
Definition month.hpp:150
constexpr auto operator<(month lhs, month rhs) noexcept -> bool
Definition month.hpp:58
constexpr auto operator>=(month lhs, month rhs) noexcept -> bool
Definition month.hpp:73
constexpr auto operator-(month const &m1, month const &m2) noexcept -> months
Definition month.hpp:95
Definition adjacent_find.hpp:9
constexpr auto count() const -> rep
Returns the number of ticks for this duration.
Definition duration.hpp:98
constexpr auto operator-() const -> etl::common_type_t< duration >
Implements unary plus and unary minus for the durations.
Definition duration.hpp:128
The class month represents a month in a year.
Definition month.hpp:22
constexpr auto operator-=(months const &m) noexcept -> month &
Definition month.hpp:133
constexpr auto operator++(int) noexcept -> month
Definition month.hpp:107
constexpr operator unsigned() const noexcept
Definition month.hpp:36
constexpr auto ok() const noexcept -> bool
Definition month.hpp:31
constexpr auto operator+=(months const &m) noexcept -> month &
Definition month.hpp:127
constexpr month(unsigned m) noexcept
Definition month.hpp:25
constexpr auto operator--() noexcept -> month &
Definition month.hpp:114
constexpr auto operator++() noexcept -> month &
Definition month.hpp:101
constexpr auto operator--(int) noexcept -> month
Definition month.hpp:120