Add function getYears, getMonths, getDays

This commit is contained in:
thaianhtaivn 2023-01-12 11:04:58 +07:00
parent 62fafd8eca
commit 22fff3757e
2 changed files with 24 additions and 0 deletions

View File

@ -148,6 +148,27 @@ int NTPClient::getMinutes() const {
int NTPClient::getSeconds() const {
return (this->getEpochTime() % 60);
}
int NTPClient::getYears() const {
time_t rawtime = this->getEpochTime();
struct tm* ti;
ti = localtime(&rawtime);
int year = ti->tm_year + 1900;
return year;
}
int NTPClient::getMonths() const {
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::getDays() const {
time_t rawtime = this->getEpochTime();
struct tm* ti;
ti = localtime(&rawtime);
int day = (ti->tm_mday) < 10 ? 0 + (ti->tm_mday) : (ti->tm_mday);
return day;
}
String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime();

View File

@ -85,6 +85,9 @@ class NTPClient {
int getHours() const;
int getMinutes() const;
int getSeconds() const;
int getYears() const;
int getMonths() const;
int getDays() const;
/**
* Changes the time offset. Useful for changing timezones dynamically