Skip to content

Commit 9888a03

Browse files
committed
added the fullformatedtime capability
1 parent 1aea292 commit 9888a03

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

NTPClient.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,66 @@ void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
210210
randomSeed(analogRead(0));
211211
this->_port = random(minValue, maxValue);
212212
}
213+
214+
215+
void NTPClient::setEpochTime(unsigned long secs) {
216+
this->_currentEpoc = secs;
217+
}
218+
219+
String NTPClient::getFullFormattedTime(unsigned long secs)
220+
{
221+
time_t rawtime = this->getEpochTime();
222+
struct tm *ti;
223+
ti = localtime(&rawtime);
224+
225+
uint16_t year = ti->tm_year + 1900;
226+
String yearStr = String(year);
227+
228+
uint8_t month = ti->tm_mon + 1;
229+
String monthStr = month < 10 ? "0" + String(month) : String(month);
230+
231+
uint8_t day = ti->tm_mday;
232+
String dayStr = day < 10 ? "0" + String(day) : String(day);
233+
234+
uint8_t hours = ti->tm_hour;
235+
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
236+
237+
uint8_t minutes = ti->tm_min;
238+
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
239+
240+
uint8_t seconds = ti->tm_sec;
241+
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
242+
243+
return yearStr + "-" + monthStr + "-" + dayStr + "T" +
244+
hoursStr + ":" + minuteStr + ":" + secondStr + "Z";
245+
}
246+
247+
int NTPClient::getYear()
248+
{
249+
time_t rawtime = this->getEpochTime();
250+
struct tm *ti;
251+
ti = localtime(&rawtime);
252+
int year = ti->tm_year + 1900;
253+
254+
return year;
255+
}
256+
257+
int NTPClient::getMonth()
258+
{
259+
time_t rawtime = this->getEpochTime();
260+
struct tm *ti;
261+
ti = localtime(&rawtime);
262+
int month = (ti->tm_mon + 1) < 10 ? 0 + (ti->tm_mon + 1) : (ti->tm_mon + 1);
263+
264+
return month;
265+
}
266+
267+
int NTPClient::getDate()
268+
{
269+
time_t rawtime = this->getEpochTime();
270+
struct tm *ti;
271+
ti = localtime(&rawtime);
272+
int month = (ti->tm_mday) < 10 ? 0 + (ti->tm_mday) : (ti->tm_mday);
273+
274+
return month;
275+
}

NTPClient.h

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

84-
int getDay() const;
85-
int getHours() const;
86-
int getMinutes() const;
87-
int getSeconds() const;
88-
84+
int getDay();
85+
int getHours();
86+
int getMinutes();
87+
int getSeconds();
88+
int getYear();
89+
int getMonth();
90+
int getDate();
8991
/**
9092
* Changes the time offset. Useful for changing timezones dynamically
9193
*/
@@ -107,6 +109,9 @@ class NTPClient {
107109
*/
108110
unsigned long getEpochTime() const;
109111

112+
void setEpochTime(unsigned long secs);
113+
114+
String getFullFormattedTime(unsigned long secs = 0);
110115
/**
111116
* Stops the underlying UDP client
112117
*/

0 commit comments

Comments
 (0)