Add methods for working with Date

This commit is contained in:
iPhosgen 2018-05-18 09:52:51 +03:00
parent 9dc9ad184b
commit 2c2ca165d6
2 changed files with 47 additions and 0 deletions

View File

@ -134,6 +134,45 @@ String NTPClient::getFormattedTime() {
return hoursStr + ":" + minuteStr + ":" + secondStr; 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() { void NTPClient::end() {
this->_udp->stop(); this->_udp->stop();

View File

@ -62,6 +62,9 @@ class NTPClient {
int getHours(); int getHours();
int getMinutes(); int getMinutes();
int getSeconds(); int getSeconds();
int getYear();
int getMonth();
int getDate();
/** /**
* Changes the time offset. Useful for changing timezones dynamically * Changes the time offset. Useful for changing timezones dynamically
@ -78,6 +81,11 @@ class NTPClient {
* @return time formatted like `hh:mm:ss` * @return time formatted like `hh:mm:ss`
*/ */
String getFormattedTime(); String getFormattedTime();
/**
* @return date formatted like `dd.MM.yyyy`
*/
String getFormattedDate();
/** /**
* @return time in seconds since Jan. 1, 1970 * @return time in seconds since Jan. 1, 1970