From 22fff3757e5fe35f5ae79673ef50c2ab0dc3df20 Mon Sep 17 00:00:00 2001 From: thaianhtaivn Date: Thu, 12 Jan 2023 11:04:58 +0700 Subject: [PATCH] Add function getYears, getMonths, getDays --- NTPClient.cpp | 21 +++++++++++++++++++++ NTPClient.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/NTPClient.cpp b/NTPClient.cpp index b435855..13780de 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -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(); diff --git a/NTPClient.h b/NTPClient.h index a31d32f..cc26b78 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -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