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

NTPClient now returns date, year, month and day. #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .codespellrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See: https://github.com/codespell-project/codespell#using-a-config-file
[codespell]
# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here:
ignore-words-list = ,
ignore-words-list = leapyears
check-filenames =
check-hidden =
skip = ./.git
97 changes: 96 additions & 1 deletion NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ unsigned long NTPClient::getEpochTime() const {
((millis() - this->_lastUpdate) / 1000); // Time since last update
}

int NTPClient::getDay() const {
int NTPClient::getDayOfWeek() const {
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
}
int NTPClient::getHours() const {
Expand All @@ -149,6 +149,97 @@ int NTPClient::getSeconds() const {
return (this->getEpochTime() % 60);
}

int NTPClient::getDay() const {

long days = this->getEpochTime() / 86400L;
int fullYears = days / 365;
int overDays = days % 365;

int leapYears = (fullYears - 2) / 4;
if (leapYears > overDays) {
fullYears--;
}

int currentYear = 1970 + fullYears;

int thisYearIsLeap = currentYear % 4 == 0 ? 1 : 0;

int dayOfYear = (days - leapYears) % ( 365 + thisYearIsLeap);
if(dayOfYear == 0) {
dayOfYear = 365 + thisYearIsLeap;
}

int daysInMonth[12] = {31, 28 + thisYearIsLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

for( int month = 0; month < 12; month++) {
if(dayOfYear < daysInMonth[month]) {
return dayOfYear;
} else {
dayOfYear -= daysInMonth[month];
}
}

return -1;
}

int NTPClient::getMonth() const {

long days = this->getEpochTime() / 86400L;
int fullYears = days / 365;
int overDays = days % 365;

int leapYears = (fullYears - 2) / 4;
if (leapYears > overDays) {
fullYears--;
}

int currentYear = 1970 + leapYears;

int thisYearIsLeap = currentYear % 4 == 0 ? 1 : 0;

int dayOfYear = (days - leapYears) % ( 365 + thisYearIsLeap);
if(dayOfYear == 0) {
dayOfYear = 365 + thisYearIsLeap;
}

int daysInMonth[12] = {31, 28 + thisYearIsLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

for( int month = 0; month < 12; month++) {
if(dayOfYear < daysInMonth[month]) {
return month + 1;
} else {
dayOfYear -= daysInMonth[month];
}
}

return -1;
}

int NTPClient::getYear() const {
long days = this->getEpochTime() / 86400L;
int fullYears = days / 365;
int overDays = days % 365;

int leapYears = (fullYears - 2) / 4;
Copy link

@poveden poveden Mar 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getYear() fails for all days that match December 31st, except for the year 1970 and those where the next year is a leap one (brute-tested using Excel):

Code 1970/12/31 1971/12/31 1972/12/31
days 364 729 (365+364) 1095 (365+365+365)
fullYears = days / 365 0 (364 / 365) 1 (729 / 365) 3 (1095 / 365)
overDays = days % 365 364 (364 % 365) 364 (729 % 365) 0 (1095 % 365)
leapYears = (fullYears - 2) / 4 0 ((0 − 2) / 4) 0 ((1 − 2) / 4) 0 ((3 − 2) / 4)
if (leapYears > overDays) fullYears-- 0 (No change) 1 (No change) 3 (No change)
return 1970 + fullYears 1970 (1970 + 0) 1971 (1970 + 1) 1973 (1970 + 3)

Fortunately, it can be easily fixed:

Suggested change
int leapYears = (fullYears - 2) / 4;
int leapYears = (fullYears + 1) / 4;


if (leapYears > overDays) {
fullYears--;
}
return 1970 + fullYears;
}

String NTPClient::getFormattedDate() const {
String yearStr = String(this->getYear());

unsigned int month = this->getMonth();
String monthStr = month < 10 ? "0" + String(month) : String(month);

unsigned int day = this->getDay();
String dayStr = day < 10 ? "0" + String(day) : String(day);

return yearStr + "-" + monthStr + "-" + dayStr;
}

String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime();
unsigned long hours = (rawTime % 86400L) / 3600;
Expand All @@ -163,6 +254,10 @@ String NTPClient::getFormattedTime() const {
return hoursStr + ":" + minuteStr + ":" + secondStr;
}

String NTPClient::getFormattedDateTime() const {
return this->getFormattedDate() + "T" + this->getFormattedTime();
}

void NTPClient::end() {
this->_udp->stop();

Expand Down
15 changes: 14 additions & 1 deletion NTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ class NTPClient {
*/
bool isTimeSet() const;

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

/**
* Changes the time offset. Useful for changing timezones dynamically
Expand All @@ -97,11 +100,21 @@ class NTPClient {
*/
void setUpdateInterval(unsigned long updateInterval);

/**
* @return date formatted like `YYYY-MM-DD`
*/
String getFormattedDate() const;

/**
* @return time formatted like `hh:mm:ss`
*/
String getFormattedTime() const;

/**
* @return datetime formatted like `YYYY-MM-DDTHH:mm:ss`
*/
String getFormattedDateTime() const;

/**
* @return time in seconds since Jan. 1, 1970
*/
Expand Down
7 changes: 6 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ end KEYWORD2
update KEYWORD2
forceUpdate KEYWORD2
isTimeSet KEYWORD2
getDay KEYWORD2
getHours KEYWORD2
getMinutes KEYWORD2
getSeconds KEYWORD2
getDayOfWeek KEYWORD2
getDay KEYWORD2
getMonth KEYWORD2
getYear KEYWORD2
getFormattedDate KEYWORD2
getFormattedTime KEYWORD2
getFormattedDateTime KEYWORD2
getEpochTime KEYWORD2
setTimeOffset KEYWORD2
setUpdateInterval KEYWORD2
Expand Down