Add methods for working with Date
This commit is contained in:
parent
9dc9ad184b
commit
2c2ca165d6
@ -134,6 +134,45 @@ String NTPClient::getFormattedTime() {
|
||||
return hoursStr + ":" + minuteStr + ":" + secondStr;
|
||||
}
|
||||
|
||||
int NTPClient::getYear() {
|
||||
time_t rawtime = this->getEpochTime();
|
||||
struct tm * ti;
|
||||
ti = localtime (&rawtime);
|
||||
int year = ti->tm_year + 1900;
|
||||
|
||||
return year;
|
||||
}
|
||||
|
||||
int NTPClient::getMonth() {
|
||||
time_t rawtime = this->getEpochTime();
|
||||
struct tm * ti;
|
||||
ti = localtime (&rawtime);
|
||||
int month = (ti->tm_mon + 1) < 10 ? 0 + (ti->tm_mon + 1) : (ti->tm_mon + 1);
|
||||
|
||||
return month;
|
||||
}
|
||||
|
||||
int NTPClient::getDate() {
|
||||
time_t rawtime = this->getEpochTime();
|
||||
struct tm * ti;
|
||||
ti = localtime (&rawtime);
|
||||
int month = (ti->tm_mday) < 10 ? 0 + (ti->tm_mday) : (ti->tm_mday);
|
||||
|
||||
return month;
|
||||
}
|
||||
|
||||
String NTPClient::getFormattedDate() {
|
||||
int day = this->getDate();
|
||||
int month = this->getMonth();
|
||||
int year = this->getYear();
|
||||
|
||||
String dayStr = day < 10 ? "0" + String(day) : String(day);
|
||||
String monthStr = month < 10 ? "0" + String(month) : String(month);
|
||||
String yearStr = String(year);
|
||||
|
||||
return dayStr + "." + monthStr + "." + yearStr;
|
||||
}
|
||||
|
||||
void NTPClient::end() {
|
||||
this->_udp->stop();
|
||||
|
||||
|
@ -62,6 +62,9 @@ class NTPClient {
|
||||
int getHours();
|
||||
int getMinutes();
|
||||
int getSeconds();
|
||||
int getYear();
|
||||
int getMonth();
|
||||
int getDate();
|
||||
|
||||
/**
|
||||
* Changes the time offset. Useful for changing timezones dynamically
|
||||
@ -78,6 +81,11 @@ class NTPClient {
|
||||
* @return time formatted like `hh:mm:ss`
|
||||
*/
|
||||
String getFormattedTime();
|
||||
|
||||
/**
|
||||
* @return date formatted like `dd.MM.yyyy`
|
||||
*/
|
||||
String getFormattedDate();
|
||||
|
||||
/**
|
||||
* @return time in seconds since Jan. 1, 1970
|
||||
|
Loading…
Reference in New Issue
Block a user