Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added methods for working with Date component #60

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,45 @@ String NTPClient::getFormattedTime() const {
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();

Expand Down
18 changes: 13 additions & 5 deletions NTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ class NTPClient {
*/
bool forceUpdate();

int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
int getDay();
int getHours();
int getMinutes();
int getSeconds();
int getYear();
int getMonth();
int getDate();

/**
* Changes the time offset. Useful for changing timezones dynamically
Expand All @@ -84,7 +87,12 @@ class NTPClient {
/**
* @return time formatted like `hh:mm:ss`
*/
String getFormattedTime() const;
String getFormattedTime();

/**
* @return date formatted like `dd.MM.yyyy`
*/
String getFormattedDate();

/**
* @return time in seconds since Jan. 1, 1970
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void loop() {
timeClient.update();

Serial.println(timeClient.getFormattedTime());
Serial.println(timeClient.getFormattedDate());

delay(1000);
}
Expand Down