@@ -136,7 +136,7 @@ unsigned long NTPClient::getEpochTime() const {
136
136
((millis () - this ->_lastUpdate ) / 1000 ); // Time since last update
137
137
}
138
138
139
- int NTPClient::getDay () const {
139
+ int NTPClient::getDayOfWeek () const {
140
140
return (((this ->getEpochTime () / 86400L ) + 4 ) % 7 ); // 0 is Sunday
141
141
}
142
142
int NTPClient::getHours () const {
@@ -149,6 +149,97 @@ int NTPClient::getSeconds() const {
149
149
return (this ->getEpochTime () % 60 );
150
150
}
151
151
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
+
152
243
String NTPClient::getFormattedTime () const {
153
244
unsigned long rawTime = this ->getEpochTime ();
154
245
unsigned long hours = (rawTime % 86400L ) / 3600 ;
@@ -163,6 +254,10 @@ String NTPClient::getFormattedTime() const {
163
254
return hoursStr + " :" + minuteStr + " :" + secondStr;
164
255
}
165
256
257
+ String NTPClient::getFormattedDateTime () const {
258
+ return this ->getFormattedDate () + " T" + this ->getFormattedTime ();
259
+ }
260
+
166
261
void NTPClient::end () {
167
262
this ->_udp ->stop ();
168
263
0 commit comments