Skip to content

Commit 5b5c317

Browse files
committed
Implement the calculateFullDateComponents and other improvements
1 parent 3b359eb commit 5b5c317

File tree

2 files changed

+79
-103
lines changed

2 files changed

+79
-103
lines changed

NTPClient.cpp

+46-96
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,27 @@
2121

2222
#include "NTPClient.h"
2323

24-
const DateLanguageData EnglishData = {
24+
const NTPClient::DateLanguageData NTPClient::EnglishData = {
2525
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
2626
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
2727
{"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"},
2828
{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
2929
};
3030

31-
const DateLanguageData SpanishData = {
31+
const NTPClient::DateLanguageData NTPClient::SpanishData = {
3232
{"Dom", "Lun", "Mart", "Miérc", "Juev", "Vier", "Sáb"},
3333
{"Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"},
3434
{"ene", "feb", "mar", "abr", "mayo", "jun", "jul", "ago", "sept", "oct", "nov", "dic"},
3535
{"enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"}
3636
};
3737

38-
const DateLanguageData PortugueseData = {
38+
const NTPClient::DateLanguageData NTPClient::PortugueseData = {
3939
{"Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb"},
4040
{"Domingo", "Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"},
4141
{"jan", "fev", "mar", "abr", "maio", "jun", "jul", "ago", "set", "out", "nov", "dez"},
4242
{"janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"}
4343
};
4444

45-
// Language map definition
46-
const struct LanguageMap {
47-
const char* code;
48-
const DateLanguageData* data;
49-
} languageMap[] = {
50-
{"en", &EnglishData},
51-
{"es", &SpanishData},
52-
{"pt", &PortugueseData}
53-
// Add new languages here
54-
};
55-
const int languageMapSize = sizeof(languageMap) / sizeof(LanguageMap);
56-
57-
// Function to find language data by code
58-
const DateLanguageData* findLanguageData(const String& code) {
59-
for (int i = 0; i < languageMapSize; ++i) {
60-
if (code == languageMap[i].code) {
61-
return languageMap[i].data;
62-
}
63-
}
64-
return &EnglishData; // Default to English if not found
65-
}
66-
6745
NTPClient::NTPClient(UDP& udp) {
6846
this->_udp = &udp;
6947
}
@@ -169,6 +147,16 @@ bool NTPClient::update() {
169147
return false; // return false if update does not occur
170148
}
171149

150+
// Function to find language data by code
151+
const NTPClient::DateLanguageData* NTPClient::findLanguageData(const String& code) const {
152+
for (int i = 0; i < languageMapSize; ++i) {
153+
if (code == languageMap[i].code) {
154+
return languageMap[i].data;
155+
}
156+
}
157+
return &EnglishData; // Default to English if not found
158+
}
159+
172160
bool NTPClient::isTimeSet() const {
173161
return (this->_lastUpdate != 0); // returns true if the time has been set, else false
174162
}
@@ -179,57 +167,11 @@ unsigned long NTPClient::getEpochTime() const {
179167
((millis() - this->_lastUpdate) / 1000); // Time since last update
180168
}
181169

182-
int NTPClient::getDayOfWeek() const {
183-
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
184-
}
185-
int NTPClient::getHours() const {
186-
return ((this->getEpochTime() % 86400L) / 3600);
187-
}
188-
int NTPClient::getMinutes() const {
189-
return ((this->getEpochTime() % 3600) / 60);
190-
}
191-
int NTPClient::getSeconds() const {
192-
return (this->getEpochTime() % 60);
193-
}
194-
195-
int NTPClient::getDay() const {
196-
long days = this->getEpochTime() / 86400L;
197-
int fullYears = days / 365;
198-
int overDays = days % 365;
199-
200-
// Adjust for leap years
201-
int leapYears = (fullYears + 1) / 4; // +1 because year 0 (1970) is not a leap year
202-
if (leapYears > overDays) {
203-
fullYears--;
204-
}
205-
206-
int currentYear = 1970 + fullYears;
207-
208-
// Check if current year is a leap year
209-
bool thisYearIsLeap = (currentYear % 4 == 0 && (currentYear % 100 != 0 || currentYear % 400 == 0));
210-
211-
// Calculate day of the year
212-
int dayOfYear = (days - ((fullYears * 365) + leapYears)) + 1; // +1 to convert from 0-based to 1-based
213-
214-
int daysInMonth[12] = {31, 28 + thisYearIsLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
215-
216-
int monthDay = dayOfYear;
217-
for (int month = 0; month < 12; month++) {
218-
if (monthDay <= daysInMonth[month]) {
219-
return monthDay; // Correct day of the month
220-
}
221-
monthDay -= daysInMonth[month];
222-
}
223-
224-
return -1; // Error case, should not happen
225-
}
226-
227-
int NTPClient::getMonth() const {
228-
long days = this->getEpochTime() / 86400L; // Total days since epoch
229-
int fullYears = 0;
170+
NTPClient::FullDateComponents NTPClient::calculateFullDateComponents() const {
171+
unsigned long epochTime = this->getEpochTime();
172+
long days = epochTime / 86400L; // Total days since epoch
230173
int year = 1970;
231174

232-
// Correctly account for leap years in the loop
233175
while (days > 365) {
234176
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
235177
if (days > 366) {
@@ -244,42 +186,50 @@ int NTPClient::getMonth() const {
244186
}
245187
}
246188

189+
int dayOfYear = static_cast<int>(days) + 1; // +1 to convert from 0-based to 1-based
247190
bool thisYearIsLeap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
248191
int daysInMonth[12] = {31, 28 + thisYearIsLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
249192

250193
int month = 0;
251194
for (month = 0; month < 12; month++) {
252-
if (days < daysInMonth[month]) {
195+
if (dayOfYear <= daysInMonth[month]) {
253196
break; // Found the current month
254197
}
255-
days -= daysInMonth[month];
198+
dayOfYear -= daysInMonth[month];
256199
}
257200

258-
return month + 1; // Month is 1-based
201+
return {year, month + 1, dayOfYear}; // Month is 1-based
259202
}
260203

261204
int NTPClient::getYear() const {
262-
long days = this->getEpochTime() / 86400L; // Total days since epoch
263-
int year = 1970;
205+
FullDateComponents dateComponents = calculateFullDateComponents();
206+
return dateComponents.year;
207+
}
264208

265-
while (days > 365) {
266-
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
267-
// Leap year
268-
if (days > 366) {
269-
days -= 366;
270-
year += 1;
271-
} else {
272-
// If days <= 366 in a leap year, then we've found the current year
273-
break;
274-
}
275-
} else {
276-
// Not a leap year
277-
days -= 365;
278-
year += 1;
279-
}
280-
}
209+
int NTPClient::getMonth() const {
210+
FullDateComponents dateComponents = calculateFullDateComponents();
211+
return dateComponents.month;
212+
}
213+
214+
int NTPClient::getDay() const {
215+
FullDateComponents dateComponents = calculateFullDateComponents();
216+
return dateComponents.day;
217+
}
281218

282-
return year;
219+
int NTPClient::getDayOfWeek() const {
220+
return (((this->getEpochTime() / 86400L) + 4 ) % 7); // 0 is Sunday
221+
}
222+
223+
int NTPClient::getHours() const {
224+
return ((this->getEpochTime() % 86400L) / 3600);
225+
}
226+
227+
int NTPClient::getMinutes() const {
228+
return ((this->getEpochTime() % 3600) / 60);
229+
}
230+
231+
int NTPClient::getSeconds() const {
232+
return (this->getEpochTime() % 60);
283233
}
284234

285235
String NTPClient::getFormattedDateTime(const String& format) {

NTPClient.h

+33-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88
#define NTP_PACKET_SIZE 48
99
#define NTP_DEFAULT_LOCAL_PORT 1337
1010

11-
struct DateLanguageData {
12-
const char* shortWeekDays[7];
13-
const char* longWeekDays[7];
14-
const char* shortMonths[12];
15-
const char* longMonths[12];
16-
};
17-
1811
class NTPClient {
1912
private:
2013
UDP* _udp;
@@ -35,6 +28,39 @@ class NTPClient {
3528

3629
void sendNTPPacket();
3730

31+
struct DateLanguageData {
32+
const char* shortWeekDays[7];
33+
const char* longWeekDays[7];
34+
const char* shortMonths[12];
35+
const char* longMonths[12];
36+
};
37+
38+
struct FullDateComponents {
39+
int year;
40+
int month;
41+
int day;
42+
};
43+
44+
// Language map
45+
struct LanguageMap {
46+
const char* code;
47+
const DateLanguageData* data;
48+
};
49+
50+
static const DateLanguageData EnglishData;
51+
static const DateLanguageData SpanishData;
52+
static const DateLanguageData PortugueseData;
53+
54+
const LanguageMap languageMap[3] = {
55+
{"en", &EnglishData},
56+
{"es", &SpanishData},
57+
{"pt", &PortugueseData}
58+
};
59+
60+
const int languageMapSize = sizeof(languageMap) / sizeof(languageMap[0]);
61+
const DateLanguageData* findLanguageData(const String& code) const;
62+
FullDateComponents calculateFullDateComponents() const;
63+
3864
public:
3965
NTPClient(UDP& udp);
4066
NTPClient(UDP& udp, long timeOffset);

0 commit comments

Comments
 (0)