Skip to content

Commit 1fab9d7

Browse files
committed
NTPClient now returns date, year, month and day.
To get day of week now use getDayOfWeek()
1 parent 531eff3 commit 1fab9d7

File tree

3 files changed

+116
-3
lines changed

3 files changed

+116
-3
lines changed

NTPClient.cpp

+96-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ unsigned long NTPClient::getEpochTime() const {
136136
((millis() - this->_lastUpdate) / 1000); // Time since last update
137137
}
138138

139-
int NTPClient::getDay() const {
139+
int NTPClient::getDayOfWeek() const {
140140
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
141141
}
142142
int NTPClient::getHours() const {
@@ -149,6 +149,97 @@ int NTPClient::getSeconds() const {
149149
return (this->getEpochTime() % 60);
150150
}
151151

152+
int NTPClient::getDay() const {
153+
154+
long days = this->getEpochTime() / 86400L;
155+
int fullYears = days / 365;
156+
int overDays = days % 365;
157+
158+
int leapYears = (fullYears - 2) / 4;
159+
if (leapYears > overDays) {
160+
fullYears--;
161+
}
162+
163+
int currentYear = 1970 + fullYears;
164+
165+
int thisYearIsLeap = currentYear % 4 == 0 ? 1 : 0;
166+
167+
int dayOfYear = (days - leapYears) % ( 365 + thisYearIsLeap);
168+
if(dayOfYear == 0) {
169+
dayOfYear = 365 + thisYearIsLeap;
170+
}
171+
172+
int daysInMonth[12] = {31, 28 + thisYearIsLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
173+
174+
for( int month = 0; month < 12; month++) {
175+
if(dayOfYear < daysInMonth[month]) {
176+
return dayOfYear;
177+
} else {
178+
dayOfYear -= daysInMonth[month];
179+
}
180+
}
181+
182+
return -1;
183+
}
184+
185+
int NTPClient::getMonth() const {
186+
187+
long days = this->getEpochTime() / 86400L;
188+
int fullYears = days / 365;
189+
int overDays = days % 365;
190+
191+
int leapYears = (fullYears - 2) / 4;
192+
if (leapYears > overDays) {
193+
fullYears--;
194+
}
195+
196+
int currentYear = 1970 + leapYears;
197+
198+
int thisYearIsLeap = currentYear % 4 == 0 ? 1 : 0;
199+
200+
int dayOfYear = (days - leapYears) % ( 365 + thisYearIsLeap);
201+
if(dayOfYear == 0) {
202+
dayOfYear = 365 + thisYearIsLeap;
203+
}
204+
205+
int daysInMonth[12] = {31, 28 + thisYearIsLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
206+
207+
for( int month = 0; month < 12; month++) {
208+
if(dayOfYear < daysInMonth[month]) {
209+
return month + 1;
210+
} else {
211+
dayOfYear -= daysInMonth[month];
212+
}
213+
}
214+
215+
return -1;
216+
}
217+
218+
int NTPClient::getYear() const {
219+
long days = this->getEpochTime() / 86400L;
220+
int fullYears = days / 365;
221+
int overDays = days % 365;
222+
223+
int leapYears = (fullYears - 2) / 4;
224+
225+
if (leapYears > overDays) {
226+
fullYears--;
227+
}
228+
return 1970 + fullYears;
229+
}
230+
231+
String NTPClient::getFormattedDate() const {
232+
String yearStr = String(this->getYear());
233+
234+
unsigned int month = this->getMonth();
235+
String monthStr = month < 10 ? "0" + String(month) : String(month);
236+
237+
unsigned int day = this->getDay();
238+
String dayStr = day < 10 ? "0" + String(day) : String(day);
239+
240+
return yearStr + "-" + monthStr + "-" + dayStr;
241+
}
242+
152243
String NTPClient::getFormattedTime() const {
153244
unsigned long rawTime = this->getEpochTime();
154245
unsigned long hours = (rawTime % 86400L) / 3600;
@@ -163,6 +254,10 @@ String NTPClient::getFormattedTime() const {
163254
return hoursStr + ":" + minuteStr + ":" + secondStr;
164255
}
165256

257+
String NTPClient::getFormattedDateTime() const {
258+
return this->getFormattedDate() + "T" + this->getFormattedTime();
259+
}
260+
166261
void NTPClient::end() {
167262
this->_udp->stop();
168263

NTPClient.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ class NTPClient {
8181
*/
8282
bool isTimeSet() const;
8383

84-
int getDay() const;
8584
int getHours() const;
8685
int getMinutes() const;
8786
int getSeconds() const;
87+
int getDayOfWeek() const;
88+
int getDay() const;
89+
int getMonth() const;
90+
int getYear() const;
8891

8992
/**
9093
* Changes the time offset. Useful for changing timezones dynamically
@@ -97,11 +100,21 @@ class NTPClient {
97100
*/
98101
void setUpdateInterval(unsigned long updateInterval);
99102

103+
/**
104+
* @return date formatted like `YYYY-MM-DD`
105+
*/
106+
String getFormattedDate() const;
107+
100108
/**
101109
* @return time formatted like `hh:mm:ss`
102110
*/
103111
String getFormattedTime() const;
104112

113+
/**
114+
* @return datetime formatted like `YYYY-MM-DDTHH:mm:ss`
115+
*/
116+
String getFormattedDateTime() const;
117+
105118
/**
106119
* @return time in seconds since Jan. 1, 1970
107120
*/

keywords.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ end KEYWORD2
1313
update KEYWORD2
1414
forceUpdate KEYWORD2
1515
isTimeSet KEYWORD2
16-
getDay KEYWORD2
1716
getHours KEYWORD2
1817
getMinutes KEYWORD2
1918
getSeconds KEYWORD2
19+
getDayOfWeek KEYWORD2
20+
getDay KEYWORD2
21+
getMonth KEYWORD2
22+
getYear KEYWORD2
23+
getFormattedDate KEYWORD2
2024
getFormattedTime KEYWORD2
25+
getFormattedDateTime KEYWORD2
2126
getEpochTime KEYWORD2
2227
setTimeOffset KEYWORD2
2328
setUpdateInterval KEYWORD2

0 commit comments

Comments
 (0)