semf
date.cpp
Go to the documentation of this file.
1
12
13namespace semf
14{
15Date::Date(uint8_t day, Month month, uint16_t year)
16{
18}
19
20bool Date::setDate(uint8_t day, Month month, uint16_t year)
21{
22 bool isvalid = isValid(day, month, year);
23 if (isvalid)
24 {
25 m_day = day;
26 m_month = month;
27 m_year = year;
28 SEMF_INFO("setDate to: y %u; m %u; d %u", year, static_cast<uint8_t>(month), day);
29 }
30 else
31 {
32 SEMF_ERROR("invalid parameter in setDate: y %u; m %u; d %u", year, static_cast<uint8_t>(month), day);
33 }
34 return isvalid;
35}
36
37uint8_t Date::day() const
38{
39 return m_day;
40}
41
42bool Date::setDay(uint8_t day)
43{
44 bool isvalid = isValid(day, month(), year());
45 if (isvalid)
46 {
47 m_day = day;
48 SEMF_INFO("setDay to %u", day);
49 }
50 else
51 {
52 SEMF_ERROR("invalid parameter in setDay: y %u; m %u; d %u", year(), static_cast<uint8_t>(month()), day);
53 }
54 return isvalid;
55}
56
58{
59 return m_month;
60}
61
63{
64 bool isvalid = isValid(day(), month, year());
65 if (isvalid)
66 {
67 m_month = month;
68 SEMF_INFO("setMonth to %u", month);
69 }
70 else
71 {
72 SEMF_ERROR("invalid parameter in setDay: y %u; m %u; d %u", year(), static_cast<uint8_t>(month), day());
73 }
74 return isvalid;
75}
76
77uint16_t Date::year() const
78{
79 return m_year;
80}
81
82bool Date::setYear(uint16_t year)
83{
84 bool isvalid = isValid(day(), month(), year);
85 if (isvalid)
86 {
87 m_year = year;
88 SEMF_INFO("setYear to %u", year);
89 }
90 else
91 {
92 SEMF_ERROR("invalid parameter in setDay: y %u; m %u; d %u", year, static_cast<uint8_t>(month()), day());
93 }
94 return isvalid;
95}
96
97void Date::addDays(int32_t days)
98{
99 int32_t addmonths = 0;
100 uint8_t m = month();
101 uint16_t y = year();
102 uint8_t daysinmonth = daysInMonth(static_cast<Month>(m), y);
103
104 if (days >= 0)
105 {
106 while (days + day() > daysinmonth)
107 {
108 days -= daysinmonth;
109 if (++m > 12)
110 {
111 m = 1;
112 y++;
113 }
114 addmonths++;
115 daysinmonth = daysInMonth(static_cast<Month>(m), y);
116 }
117 }
118 else
119 {
120 while (days + day() < 1)
121 {
122 if (--m == 0)
123 {
124 m = 12;
125 if (y > 0)
126 y--;
127 }
128 daysinmonth = daysInMonth(static_cast<Month>(m), y);
129 days += daysinmonth;
130 addmonths--;
131 }
132 }
133 m_day = static_cast<uint8_t>(m_day + days);
134 SEMF_INFO("addDays to %u", m_day);
135 addMonths(addmonths);
136}
137
138void Date::addMonths(int32_t months)
139{
140 int32_t addyears = 0;
141
142 if (months >= 0)
143 {
144 addyears = months / 12;
145 months -= addyears * 12;
146 addyears += (month() + months - 1) / 12;
147 m_month = static_cast<Month>(((month() + months - 1) % 12) + 1);
148 }
149 else
150 {
151 if (months + static_cast<uint8_t>(month()) < 1)
152 {
153 addyears = ((months + static_cast<uint8_t>(month()) - 1) / 12) - 1;
154 months -= addyears * 12;
155 }
156 m_month = static_cast<Month>(months + static_cast<uint8_t>(month()));
157 SEMF_INFO("addMonths to %u", m_month);
158 }
159 addYears(addyears);
160}
161
162void Date::addYears(int32_t years)
163{
164 if (static_cast<int32_t>(year()) + years >= 0)
165 {
166 m_year = static_cast<uint16_t>(m_year + years);
167 }
168 else
169 {
170 m_year = 0;
171 }
172 SEMF_INFO("addYears to %u", m_year);
173}
174
176{
177 // URL https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto's_methods
178
179 static const uint8_t t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
180 uint32_t y = m_year;
181 y -= static_cast<uint8_t>(m_month) < 3;
182 return static_cast<Weekday>((y + y / 4 - y / 100 + y / 400 + t[static_cast<uint8_t>(m_month) - 1] + m_day) % 7);
183}
184
185bool Date::operator==(const Date& dateToCompareWith) const
186{
187 if (year() == dateToCompareWith.year() && month() == dateToCompareWith.month() && day() == dateToCompareWith.day())
188 {
189 return true;
190 }
191 else
192 {
193 return false;
194 }
195}
196
197bool Date::operator!=(const Date& dateToCompareWith) const
198{
199 return !(*this == dateToCompareWith);
200}
201
202bool Date::operator<(const Date& dateToCompareWith) const
203{
204 if (year() < dateToCompareWith.year() || (year() == dateToCompareWith.year() && month() < dateToCompareWith.month()) ||
205 (year() == dateToCompareWith.year() && month() == dateToCompareWith.month() && day() < dateToCompareWith.day()))
206 {
207 return true;
208 }
209 else
210 {
211 return false;
212 }
213}
214
215bool Date::operator<=(const Date& dateToCompareWith) const
216{
217 return (*this < dateToCompareWith || *this == dateToCompareWith);
218}
219
220bool Date::operator>(const Date& dateToCompareWith) const
221{
222 return !(*this <= dateToCompareWith);
223}
224
225bool Date::operator>=(const Date& dateToCompareWith) const
226{
227 return !(*this < dateToCompareWith);
228}
229
230uint8_t Date::daysInMonth(Month month, uint16_t year)
231{
232 // Jan., Mar., Mai., Jul., Aug., Okt., Dec.
233 if (month == January || month == March || month == May || month == July || month == August || month == October || month == December)
234 {
235 return 31;
236 }
237 // Apr., Jun., Sep., Nov.
238 else if (month == April || month == June || month == September || month == November)
239 {
240 return 30;
241 }
242 // Feb.
243 else if (month == February)
244 {
245 if (isLeapYear(year))
246 {
247 return 29;
248 }
249 else
250 {
251 return 28;
252 }
253 }
254 else
255 {
256 return 0;
257 }
258}
259
260bool Date::isLeapYear(uint16_t year)
261{
262 return (((year) % 4 == 0) && ((year) % 100 != 0)) || ((year) % 400 == 0);
263}
264
265bool Date::isValid(uint8_t day, Month month, uint16_t year)
266{
267 uint8_t daysinmonth = daysInMonth(month, year);
268 return (daysinmonth > 0 && day != 0 && day <= daysinmonth) ? true : false;
269}
270} /* namespace semf */
The Date class provides date functions.
Definition: date.h:34
bool operator>=(const Date &dateToCompareWith) const
Compares this date with other.
Definition: date.cpp:225
uint16_t year() const
Returns the year.
Definition: date.cpp:77
bool operator<(const Date &dateToCompareWith) const
Compares this date with other.
Definition: date.cpp:202
bool operator==(const Date &dateToCompareWith) const
Compares this date with other.
Definition: date.cpp:185
Date()=default
static bool isValid(uint8_t day, Month month, uint16_t year)
Returns if the given date is valid.
Definition: date.cpp:265
void addYears(int32_t years)
Adds years to this date.
Definition: date.cpp:162
void addDays(int32_t days)
Adds days to this date.
Definition: date.cpp:97
Weekday weekday() const
Returns the day in the week of the actual Date.
Definition: date.cpp:175
bool setMonth(Month month)
Sets a month.
Definition: date.cpp:62
static bool isLeapYear(uint16_t year)
Returns if a specific year is a leap year or not.
Definition: date.cpp:260
bool operator>(const Date &dateToCompareWith) const
Compares this date with other.
Definition: date.cpp:220
static uint8_t daysInMonth(Month month, uint16_t year)
Returns the number of days in a specific month and year.
Definition: date.cpp:230
bool setDay(uint8_t day)
Sets a day.
Definition: date.cpp:42
@ March
Definition: date.h:41
@ August
Definition: date.h:46
@ October
Definition: date.h:48
@ July
Definition: date.h:45
@ June
Definition: date.h:44
@ April
Definition: date.h:42
@ January
Definition: date.h:39
@ February
Definition: date.h:40
@ December
Definition: date.h:50
@ September
Definition: date.h:47
@ May
Definition: date.h:43
@ November
Definition: date.h:49
uint8_t day() const
Returns the day.
Definition: date.cpp:37
bool setYear(uint16_t year)
Sets a year.
Definition: date.cpp:82
Month month() const
Returns the month.
Definition: date.cpp:57
Weekday
Definition: date.h:54
bool operator<=(const Date &dateToCompareWith) const
Compares this date with other.
Definition: date.cpp:215
bool operator!=(const Date &dateToCompareWith) const
Compares this date with other.
Definition: date.cpp:197
void addMonths(int32_t months)
Adds months to this date.
Definition: date.cpp:138
bool setDate(uint8_t day, Month month, uint16_t year)
Sets date information.
Definition: date.cpp:20
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41