Skip to content

Commit

Permalink
Made member functions const so they can be called on const instances
Browse files Browse the repository at this point in the history
  • Loading branch information
John Auld committed Oct 2, 2024
1 parent 1cf8d5a commit c541a0f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
44 changes: 22 additions & 22 deletions ESP32Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ESP32Time::ESP32Time(long offset){
@param ms
microseconds (optional)
*/
void ESP32Time::setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms) {
void ESP32Time::setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms) const {
// seconds, minute, hour, day, month, year $ microseconds(optional)
// ie setTime(20, 34, 8, 1, 4, 2021) = 8:34:20 1/4/2021
struct tm t = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // Initalize to all 0's
Expand All @@ -84,7 +84,7 @@ void ESP32Time::setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms)
@param tm
time struct
*/
void ESP32Time::setTimeStruct(tm t) {
void ESP32Time::setTimeStruct(tm t) const {
time_t timeSinceEpoch = mktime(&t);
setTime(timeSinceEpoch, 0);
}
Expand All @@ -96,7 +96,7 @@ void ESP32Time::setTimeStruct(tm t) {
@param ms
microseconds (optional)
*/
void ESP32Time::setTime(unsigned long epoch, int ms) {
void ESP32Time::setTime(unsigned long epoch, int ms) const {
struct timeval tv;
if (epoch > 2082758399){
overflow = true;
Expand All @@ -112,7 +112,7 @@ void ESP32Time::setTime(unsigned long epoch, int ms) {
/*!
@brief get the internal RTC time as a tm struct
*/
tm ESP32Time::getTimeStruct(){
tm ESP32Time::getTimeStruct() const {
struct tm timeinfo;
time_t now;
time(&now);
Expand Down Expand Up @@ -140,7 +140,7 @@ tm ESP32Time::getTimeStruct(){
true = Long date format
false = Short date format
*/
String ESP32Time::getDateTime(bool mode){
String ESP32Time::getDateTime(bool mode) const {
struct tm timeinfo = getTimeStruct();
char s[51];
if (mode)
Expand All @@ -160,7 +160,7 @@ String ESP32Time::getDateTime(bool mode){
true = Long date format
false = Short date format
*/
String ESP32Time::getTimeDate(bool mode){
String ESP32Time::getTimeDate(bool mode) const {
struct tm timeinfo = getTimeStruct();
char s[51];
if (mode)
Expand All @@ -177,7 +177,7 @@ String ESP32Time::getTimeDate(bool mode){
/*!
@brief get the time as an Arduino String object
*/
String ESP32Time::getTime(){
String ESP32Time::getTime() const {
struct tm timeinfo = getTimeStruct();
char s[51];
strftime(s, 50, "%H:%M:%S", &timeinfo);
Expand All @@ -190,7 +190,7 @@ String ESP32Time::getTime(){
time format
http://www.cplusplus.com/reference/ctime/strftime/
*/
String ESP32Time::getTime(String format){
String ESP32Time::getTime(String format) const {
struct tm timeinfo = getTimeStruct();
char s[128];
char c[128];
Expand All @@ -205,7 +205,7 @@ String ESP32Time::getTime(String format){
true = Long date format
false = Short date format
*/
String ESP32Time::getDate(bool mode){
String ESP32Time::getDate(bool mode) const {
struct tm timeinfo = getTimeStruct();
char s[51];
if (mode)
Expand All @@ -222,7 +222,7 @@ String ESP32Time::getDate(bool mode){
/*!
@brief get the current milliseconds as unsigned long
*/
unsigned long ESP32Time::getMillis(){
unsigned long ESP32Time::getMillis() const {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec/1000;
Expand All @@ -231,7 +231,7 @@ unsigned long ESP32Time::getMillis(){
/*!
@brief get the current microseconds as unsigned long
*/
unsigned long ESP32Time::getMicros(){
unsigned long ESP32Time::getMicros() const {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec;
Expand All @@ -240,15 +240,15 @@ unsigned long ESP32Time::getMicros(){
/*!
@brief get the current epoch seconds as unsigned long
*/
unsigned long ESP32Time::getEpoch(){
unsigned long ESP32Time::getEpoch() const {
struct tm timeinfo = getTimeStruct();
return mktime(&timeinfo);
}

/*!
@brief get the current epoch seconds as unsigned long from the rtc without offset
*/
unsigned long ESP32Time::getLocalEpoch(){
unsigned long ESP32Time::getLocalEpoch() const {
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long epoch = tv.tv_sec;
Expand All @@ -261,15 +261,15 @@ unsigned long ESP32Time::getLocalEpoch(){
/*!
@brief get the current seconds as int
*/
int ESP32Time::getSecond(){
int ESP32Time::getSecond() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_sec;
}

/*!
@brief get the current minutes as int
*/
int ESP32Time::getMinute(){
int ESP32Time::getMinute() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_min;
}
Expand All @@ -280,7 +280,7 @@ int ESP32Time::getMinute(){
true = 24 hour mode (0-23)
false = 12 hour mode (1-12)
*/
int ESP32Time::getHour(bool mode){
int ESP32Time::getHour(bool mode) const {
struct tm timeinfo = getTimeStruct();
if (mode)
{
Expand Down Expand Up @@ -311,7 +311,7 @@ int ESP32Time::getHour(bool mode){
true = lowercase
false = uppercase
*/
String ESP32Time::getAmPm(bool lowercase){
String ESP32Time::getAmPm(bool lowercase) const {
struct tm timeinfo = getTimeStruct();
if (timeinfo.tm_hour >= 12)
{
Expand Down Expand Up @@ -340,39 +340,39 @@ String ESP32Time::getAmPm(bool lowercase){
/*!
@brief get the current day as int (1-31)
*/
int ESP32Time::getDay(){
int ESP32Time::getDay() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_mday;
}

/*!
@brief get the current day of week as int (0-6)
*/
int ESP32Time::getDayofWeek(){
int ESP32Time::getDayofWeek() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_wday;
}

/*!
@brief get the current day of year as int (0-365)
*/
int ESP32Time::getDayofYear(){
int ESP32Time::getDayofYear() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_yday;
}

/*!
@brief get the current month as int (0-11)
*/
int ESP32Time::getMonth(){
int ESP32Time::getMonth() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_mon;
}

/*!
@brief get the current year as int
*/
int ESP32Time::getYear(){
int ESP32Time::getYear() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_year+1900;
}
44 changes: 22 additions & 22 deletions ESP32Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ class ESP32Time {
public:
ESP32Time();
ESP32Time(long offset);
void setTime(unsigned long epoch = 1609459200, int ms = 0); // default (1609459200) = 1st Jan 2021
void setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms = 0);
void setTimeStruct(tm t);
tm getTimeStruct();
String getTime(String format);
void setTime(unsigned long epoch = 1609459200, int ms = 0) const; // default (1609459200) = 1st Jan 2021
void setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms = 0) const;
void setTimeStruct(tm t) const;
tm getTimeStruct() const;
String getTime(String format) const;

String getTime();
String getDateTime(bool mode = false);
String getTimeDate(bool mode = false);
String getDate(bool mode = false);
String getAmPm(bool lowercase = false);
String getTime() const;
String getDateTime(bool mode = false) const;
String getTimeDate(bool mode = false) const;
String getDate(bool mode = false) const;
String getAmPm(bool lowercase = false) const;

unsigned long getEpoch();
unsigned long getMillis();
unsigned long getMicros();
int getSecond();
int getMinute();
int getHour(bool mode = false);
int getDay();
int getDayofWeek();
int getDayofYear();
int getMonth();
int getYear();
unsigned long getEpoch() const;
unsigned long getMillis() const;
unsigned long getMicros() const;
int getSecond() const;
int getMinute() const;
int getHour(bool mode = false) const;
int getDay() const;
int getDayofWeek() const;
int getDayofYear() const;
int getMonth() const;
int getYear() const;

long offset = 0;
unsigned long getLocalEpoch();
unsigned long getLocalEpoch() const;



Expand Down

0 comments on commit c541a0f

Please sign in to comment.