semf
datetime.cpp
Go to the documentation of this file.
1
12
13namespace semf
14{
15DateTime::DateTime(uint16_t millisecond, uint8_t second, uint8_t minute, uint8_t hour, uint8_t day, Month month, uint16_t year)
16{
18}
19
20DateTime::DateTime(uint64_t unixTimeInSeconds)
21{
23}
24
25bool DateTime::setDateTime(uint16_t millisecond, uint8_t second, uint8_t minute, uint8_t hour, uint8_t day, Month month, uint16_t year)
26{
28 {
31 return true;
32 }
33 else
34 {
35 SEMF_ERROR("invalid parameter in setDateTime: y %u; m %u; d %u; h %u; m %u; s %u; mi %u", year, static_cast<uint8_t>(month), day, hour, minute, second,
37 return false;
38 }
39}
40
41bool DateTime::setDateTime(const Time& copyFromTime, const Date& copyFromDate)
42{
43 return setDateTime(copyFromTime.millisecond(), copyFromTime.second(), copyFromTime.minute(), copyFromTime.hour(), copyFromDate.day(), copyFromDate.month(),
44 copyFromDate.year());
45}
46
47void DateTime::setDateTime(uint64_t unixTimeInSeconds)
48{
49 uint8_t second = 0, minute = 0, hour = 0, month = 1;
50 uint16_t day = 365, year = 1970;
51
52 // Second
53 second = static_cast<uint8_t>(unixTimeInSeconds % 60);
55 // Minute
56 minute = static_cast<uint8_t>(unixTimeInSeconds % 60);
58 // Hour
59 hour = static_cast<uint8_t>(unixTimeInSeconds % 24);
61 // Year
62 while (unixTimeInSeconds >= day)
63 {
65 day = isLeapYear(++year) ? 366 : 365;
66 }
67 // Month
68 day = daysInMonth(static_cast<Month>(month), year);
69 while (unixTimeInSeconds >= day)
70 {
72 day = daysInMonth(static_cast<Month>(++month), year);
73 }
74 // Day
75 day = static_cast<uint16_t>(unixTimeInSeconds + 1);
76
77 SEMF_INFO("setDateTime from unix-timestamp %u", unixTimeInSeconds);
78 setDate(static_cast<uint8_t>(day), static_cast<Month>(month), year);
80}
81
83{
84 uint8_t sec = this->second();
85 uint8_t min = this->minute();
86 uint8_t hrs = this->hour();
87 uint8_t day = this->day();
88 uint8_t mon = this->month();
89 uint16_t year = this->year();
90
91 uint64_t ts = 0;
92 uint8_t years = 0;
93 uint8_t leapYears = 0;
94
95 for (uint16_t i = 1970; i < year; i++)
96 {
97 if (isLeapYear(i))
98 {
99 leapYears++;
100 }
101 else
102 {
103 years++;
104 }
105 }
106 ts += ((years * 365) + (leapYears * 366)) * static_cast<uint64_t>(86400);
107
108 // Add up the seconds from all prev days this year, up until today.
109 for (uint8_t i = static_cast<uint8_t>(January); i < mon; i++)
110 {
111 // days from previous months this year
112 ts += daysInMonth(static_cast<Month>(i), year) * 86400;
113 }
114 ts += (day - 1) * 86400; // days from this month
115
116 // Calculate seconds elapsed just today.
117 ts += hrs * 3600;
118 ts += min * 60;
119 ts += sec;
120
121 SEMF_INFO("unixTimeInSeconds %u out of y %u; m %u; d %u; h %u; m %m; s %u", ts, year, mon, day, hrs, min, sec);
122 return ts;
123}
124
125void DateTime::addHours(int32_t hours)
126{
127 int32_t adddays = 0;
128
129 if (hours >= 0)
130 {
131 adddays = hours / 24;
132 hours -= adddays * 24;
133 adddays += (hour() + hours) / 24;
134 setHour(static_cast<uint8_t>((hour() + hours) % 24));
135 }
136 else
137 {
138 if ((static_cast<int32_t>(hour()) + hours) >= 0)
139 {
140 setHour(static_cast<uint8_t>(hour() + hours) % 24);
141 }
142 else
143 {
144 adddays = ((hours + hour()) / 24) - 1;
145 setHour(static_cast<uint8_t>((24 + hour() + hours) % 24));
146 }
147 }
148 SEMF_INFO("addHours to %u", hour());
149 addDays(adddays);
150}
151
152bool DateTime::operator==(const DateTime& other) const
153{
154 if (Date::operator==(static_cast<Date>(other)) && Time::operator==(static_cast<Time>(other)))
155 {
156 return true;
157 }
158 else
159 {
160 return false;
161 }
162}
163
164bool DateTime::operator!=(const DateTime& other) const
165{
166 return !(*this == other);
167}
168
169bool DateTime::operator<(const DateTime& other) const
170{
171 if (Date::operator<(static_cast<Date>(other)) || (Date::operator==(static_cast<Date>(other)) && Time::operator<(static_cast<Time>(other))))
172 {
173 return true;
174 }
175 else
176 {
177 return false;
178 }
179}
180
181bool DateTime::operator<=(const DateTime& other) const
182{
183 return (*this < other || *this == other);
184}
185
186bool DateTime::operator>(const DateTime& other) const
187{
188 return !(*this <= other);
189}
190
191bool DateTime::operator>=(const DateTime& other) const
192{
193 return !(*this < other);
194}
195} /* namespace semf */
The Date class provides date functions.
Definition: date.h:34
uint16_t year() const
Returns the year.
Definition: date.cpp:77
static bool isValid(uint8_t day, Month month, uint16_t year)
Returns if the given date is valid.
Definition: date.cpp:265
void addDays(int32_t days)
Adds days to this date.
Definition: date.cpp:97
static bool isLeapYear(uint16_t year)
Returns if a specific year is a leap year or not.
Definition: date.cpp:260
static uint8_t daysInMonth(Month month, uint16_t year)
Returns the number of days in a specific month and year.
Definition: date.cpp:230
@ January
Definition: date.h:39
uint8_t day() const
Returns the day.
Definition: date.cpp:37
Month month() const
Returns the month.
Definition: date.cpp:57
bool setDate(uint8_t day, Month month, uint16_t year)
Sets date information.
Definition: date.cpp:20
The DateTime class provides date and time functionality as a point of time. It combines features of t...
Definition: datetime.h:31
bool operator==(const DateTime &other) const
Compares this date time with other.
Definition: datetime.cpp:152
DateTime()=default
void addHours(int32_t hours)
Adds hours to this datetime.
Definition: datetime.cpp:125
uint64_t unixTimeInSeconds() const
Returns the date time in seconds since the 1 January 1970.
Definition: datetime.cpp:82
bool operator<=(const DateTime &other) const
Compares this date time with other.
Definition: datetime.cpp:181
bool operator>=(const DateTime &other) const
Compares this date time with other.
Definition: datetime.cpp:191
bool operator>(const DateTime &other) const
Compares this date time with other.
Definition: datetime.cpp:186
bool operator!=(const DateTime &other) const
Compares this date time with other.
Definition: datetime.cpp:164
bool operator<(const DateTime &other) const
Compares this date time with other.
Definition: datetime.cpp:169
bool setDateTime(uint16_t millisecond, uint8_t second, uint8_t minute, uint8_t hour, uint8_t day, Month month, uint16_t year)
Sets date time information and system clock if Rtc is registered.
Definition: datetime.cpp:25
The Time class provides clock time functions.
Definition: time.h:37
uint16_t millisecond() const
Returns the milliseconds from system clock or static value.
Definition: time.cpp:43
static bool isValid(uint16_t millisecond, uint8_t second, uint8_t minute, uint8_t hour)
Returns if the given time is valid.
Definition: time.cpp:300
bool setTime(uint16_t millisecond, uint8_t second, uint8_t minute, uint8_t hour)
Sets date time information and system clock if Rtc is registered.
Definition: time.cpp:25
bool setHour(uint8_t hour)
Sets the hours.
Definition: time.cpp:108
uint8_t second() const
Returns the seconds from system clock or static value.
Definition: time.cpp:63
uint8_t minute() const
Returns the minutes.
Definition: time.cpp:83
uint8_t hour() const
Returns the hours.
Definition: time.cpp:103
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41