From 68658309b1ac8a257a5bee4724cb0a411c81caa3 Mon Sep 17 00:00:00 2001 From: isharasampath Date: Wed, 18 Mar 2020 17:30:29 +0530 Subject: [PATCH 1/2] Added two new methods to return Date and Time as a struct and get formatted Date Time string with a given format --- NTPClient.cpp | 17 +++++++++++++++++ NTPClient.h | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/NTPClient.cpp b/NTPClient.cpp index fffe105..b1e6763 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -145,6 +145,23 @@ int NTPClient::getSeconds() const { return (this->getEpochTime() % 60); } +DateTime NTPClient::getDateTime() const { + struct tm * ts; + time_t rawTime = this->getEpochTime(); + ts = localtime(&rawTime); + DateTime dt = {ts->tm_sec, ts->tm_min, ts->tm_hour, ts->tm_mday, (ts->tm_mon + 1), (ts->tm_year + 1900)}; + return dt; +} + +String NTPClient::formatDateTime(const char* fmt) const { + struct tm * ts; + time_t rawTime = this->getEpochTime(); + ts = localtime(&rawTime); + char buf[64]; + strftime(buf, sizeof(buf), fmt, ts); + return String(buf); +} + String NTPClient::getFormattedTime() const { unsigned long rawTime = this->getEpochTime(); unsigned long hours = (rawTime % 86400L) / 3600; diff --git a/NTPClient.h b/NTPClient.h index 20ec43b..76d9e3e 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -3,11 +3,21 @@ #include "Arduino.h" #include +#include #define SEVENZYYEARS 2208988800UL #define NTP_PACKET_SIZE 48 #define NTP_DEFAULT_LOCAL_PORT 1337 +struct DateTime { + int dt_seconds; + int dt_minutes; + int dt_hours; + int dt_date; + int dt_month; + int dt_year; +}; + class NTPClient { private: UDP* _udp; @@ -74,6 +84,17 @@ class NTPClient { int getMinutes() const; int getSeconds() const; + /** + * Get date time as a astuct which contains + * Year, Month, Date, Hours, Minutes, Seconds + */ + DateTime getDateTime() const; + + /** + * Format the date time to a string with a given format (Ex: %Y/%m/%d %H:%M:%S) + */ + String formatDateTime(const char* fmt) const; + /** * Changes the time offset. Useful for changing timezones dynamically */ From 708e8a7983470a2e354bb79f2b75a53c2e1be59e Mon Sep 17 00:00:00 2001 From: isharasampath Date: Thu, 19 Mar 2020 18:43:59 +0530 Subject: [PATCH 2/2] Added review comments. --- NTPClient.cpp | 4 ++-- NTPClient.h | 6 +++--- keywords.txt | 2 ++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index b1e6763..23c03f1 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -153,12 +153,12 @@ DateTime NTPClient::getDateTime() const { return dt; } -String NTPClient::formatDateTime(const char* fmt) const { +String NTPClient::getFormattedDateTime(const char* dateTimeFormat) const { struct tm * ts; time_t rawTime = this->getEpochTime(); ts = localtime(&rawTime); char buf[64]; - strftime(buf, sizeof(buf), fmt, ts); + strftime(buf, sizeof(buf), dateTimeFormat, ts); return String(buf); } diff --git a/NTPClient.h b/NTPClient.h index 76d9e3e..638b449 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -85,15 +85,15 @@ class NTPClient { int getSeconds() const; /** - * Get date time as a astuct which contains + * Get date time as a struct which contains * Year, Month, Date, Hours, Minutes, Seconds */ DateTime getDateTime() const; /** - * Format the date time to a string with a given format (Ex: %Y/%m/%d %H:%M:%S) + * Return the date time as a String with the given format (Ex: %Y/%m/%d %H:%M:%S) */ - String formatDateTime(const char* fmt) const; + String getFormattedDateTime(const char* dateTimeFormat) const; /** * Changes the time offset. Useful for changing timezones dynamically diff --git a/keywords.txt b/keywords.txt index 6f7bc6f..0a0a366 100644 --- a/keywords.txt +++ b/keywords.txt @@ -21,3 +21,5 @@ getEpochTime KEYWORD2 setTimeOffset KEYWORD2 setUpdateInterval KEYWORD2 setPoolServerName KEYWORD2 +getDateTime KEYWORD2 +getFormattedDateTime KEYWORD2