tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
year.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_YEAR_HPP
5#define TETL_CHRONO_YEAR_HPP
6
7#include <etl/_chrono/duration.hpp>
8#include <etl/_cstdint/int_t.hpp>
9#include <etl/_cstdint/uint_t.hpp>
10#include <etl/_limits/numeric_limits.hpp>
11
12namespace etl::chrono {
13
14struct year {
15 year() = default;
16
17 constexpr explicit year(int y) noexcept
18 : _count{static_cast<int16_t>(y)}
19 {
20 }
21
22 constexpr auto operator++() noexcept -> year&
23 {
24 ++_count;
25 return *this;
26 }
27
28 constexpr auto operator++(int) noexcept -> year
29 {
30 return year{_count++};
31 }
32
33 constexpr auto operator--() noexcept -> year&
34 {
35 --_count;
36 return *this;
37 }
38
39 constexpr auto operator--(int) noexcept -> year
40 {
41 return year{_count--};
42 }
43
44 constexpr auto operator+=(years const& countS) noexcept -> year&
45 {
46 _count = static_cast<int16_t>(_count + countS.count());
47 return *this;
48 }
49
50 constexpr auto operator-=(years const& countS) noexcept -> year&
51 {
52 _count = static_cast<int16_t>(_count - countS.count());
53 return *this;
54 }
55
56 [[nodiscard]] constexpr auto operator+() const noexcept -> year
57 {
58 return *this;
59 }
60
61 [[nodiscard]] constexpr auto operator-() const noexcept -> year
62 {
63 return year{-_count};
64 }
65
66 [[nodiscard]] constexpr auto is_leap() const noexcept -> bool
67 {
68 return (_count % 4 == 0) and (_count % 100 != 0 or _count % 400 == 0);
69 }
70
71 [[nodiscard]] constexpr explicit operator int() const noexcept
72 {
73 return _count;
74 }
75
76 [[nodiscard]] constexpr auto ok() const noexcept -> bool
77 {
78 return _count != numeric_limits<int16_t>::min();
79 }
80
81 [[nodiscard]] static constexpr auto min() noexcept -> year
82 {
83 return year{-32767};
84 }
85
86 [[nodiscard]] static constexpr auto max() noexcept -> year
87 {
88 return year{32767};
89 }
90
91 friend constexpr auto operator==(year lhs, year rhs) noexcept -> bool
92 {
93 return static_cast<int>(lhs) == static_cast<int>(rhs);
94 }
95
96 friend constexpr auto operator<(year lhs, year rhs) noexcept -> bool
97 {
98 return static_cast<int>(lhs) < static_cast<int>(rhs);
99 }
100
101 friend constexpr auto operator<=(year lhs, year rhs) noexcept -> bool
102 {
103 return static_cast<int>(lhs) <= static_cast<int>(rhs);
104 }
105
106 friend constexpr auto operator>(year lhs, year rhs) noexcept -> bool
107 {
108 return static_cast<int>(lhs) > static_cast<int>(rhs);
109 }
110
111 friend constexpr auto operator>=(year lhs, year rhs) noexcept -> bool
112 {
113 return static_cast<int>(lhs) >= static_cast<int>(rhs);
114 }
115
116private:
117 int16_t _count;
118};
119
120[[nodiscard]] constexpr auto operator+(year const& lhs, years const& rhs) noexcept -> year
121{
122 return year{static_cast<int>(static_cast<int>(lhs) + rhs.count())};
123}
124
125[[nodiscard]] constexpr auto operator+(years const& lhs, year const& rhs) noexcept -> year
126{
127 return rhs + lhs;
128}
129
130[[nodiscard]] constexpr auto operator-(year const& lhs, years const& rhs) noexcept -> year
131{
132 return lhs + -rhs;
133}
134
135[[nodiscard]] constexpr auto operator-(year const& lhs, year const& rhs) noexcept -> years
136{
137 return years{static_cast<int>(lhs) - static_cast<int>(rhs)};
138}
139
140} // namespace etl::chrono
141
142// NOLINTNEXTLINE(modernize-concat-nested-namespaces)
143namespace etl {
144inline namespace literals {
145inline namespace chrono_literals {
146
147/// Forms a etl::chrono::year literal representing a year in the proleptic Gregorian calendar.
148[[nodiscard]] constexpr auto operator""_y(unsigned long long y) noexcept -> etl::chrono::year
149{
150 return etl::chrono::year{static_cast<int>(y)};
151}
152
153} // namespace chrono_literals
154} // namespace literals
155} // namespace etl
156
157namespace etl::chrono {
158using namespace etl::literals::chrono_literals;
159} // namespace etl::chrono
160
161#endif // TETL_CHRONO_YEAR_HPP
Definition abs.hpp:12
constexpr auto operator-(year const &lhs, years const &rhs) noexcept -> year
Definition year.hpp:130
constexpr auto operator-(year const &lhs, year const &rhs) noexcept -> years
Definition year.hpp:135
constexpr auto operator+(years const &lhs, year const &rhs) noexcept -> year
Definition year.hpp:125
constexpr auto operator+(year const &lhs, years const &rhs) noexcept -> year
Definition year.hpp:120
Definition day.hpp:130
Definition day.hpp:129
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
Definition year.hpp:14
constexpr auto operator--(int) noexcept -> year
Definition year.hpp:39
static constexpr auto min() noexcept -> year
Definition year.hpp:81
friend constexpr auto operator>(year lhs, year rhs) noexcept -> bool
Definition year.hpp:106
friend constexpr auto operator<=(year lhs, year rhs) noexcept -> bool
Definition year.hpp:101
friend constexpr auto operator>=(year lhs, year rhs) noexcept -> bool
Definition year.hpp:111
constexpr auto ok() const noexcept -> bool
Definition year.hpp:76
static constexpr auto max() noexcept -> year
Definition year.hpp:86
constexpr year(int y) noexcept
Definition year.hpp:17
constexpr auto operator++(int) noexcept -> year
Definition year.hpp:28
friend constexpr auto operator<(year lhs, year rhs) noexcept -> bool
Definition year.hpp:96
constexpr auto operator++() noexcept -> year &
Definition year.hpp:22
constexpr auto is_leap() const noexcept -> bool
Definition year.hpp:66
constexpr operator int() const noexcept
Definition year.hpp:71
constexpr auto operator-=(years const &countS) noexcept -> year &
Definition year.hpp:50
constexpr auto operator+=(years const &countS) noexcept -> year &
Definition year.hpp:44
constexpr auto operator--() noexcept -> year &
Definition year.hpp:33
constexpr auto operator-() const noexcept -> year
Definition year.hpp:61
friend constexpr auto operator==(year lhs, year rhs) noexcept -> bool
Definition year.hpp:91
constexpr auto operator+() const noexcept -> year
Definition year.hpp:56
static constexpr auto min() noexcept -> short
Definition numeric_limits.hpp:451
Definition numeric_limits.hpp:18