Skip to content

Commit

Permalink
#123 automatic daylight saving switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKangs committed Sep 15, 2020
1 parent 5d3ad36 commit f2cb848
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 42 deletions.
2 changes: 1 addition & 1 deletion examples/RTC/BasicRepeatingRTC_DS3231/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'components':[\
{\
'name':'DS3231',\
'params':[11,true]\
'params':[11,false]\
},\
{\
'name':'Interrupt_Manager',\
Expand Down
2 changes: 1 addition & 1 deletion examples/RTC/BasicRepeatingRTC_PCF8523/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'components':[\
{\
'name':'PCF8523',\
'params':[11,true]\
'params':[11,false]\
},\
{\
'name':'Interrupt_Manager',\
Expand Down
2 changes: 1 addition & 1 deletion src/RTC/DS3231.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
///////////////////////////////////////////////////////////////////////////////
Loom_DS3231::Loom_DS3231(
LoomManager* manager,
const TimeZone timezone,
TimeZone timezone,
const bool use_local_time
)
: LoomRTC(manager, "DS3231", Type::DS3231, timezone, use_local_time )
Expand Down
4 changes: 2 additions & 2 deletions src/RTC/DS3231.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class Loom_DS3231 : public LoomRTC

/// Constructor
///
/// @param[in] timezone Set(TimeZone) | <11> | { 0("WAT"), 1("AT"), 2("ADT"), 3("AST"), 4("EDT"), 5("EST"), 6("CDT"), 7("CST"), 8("MDT"), 9("MST"), 10("PDT"), 11("PST"), 12("ALDT"), 13("ALST"), 14("HST"), 15("SST"), 16("GMT"), 17("BST"), 18("CET"), 19("CEST"), 20("EET"), 21("EEST"), 22("BT"), 23("ZP4"), 24("ZP5"), 25("ZP6"), 26("ZP7"), 27("AWST"), 28("AWDT"), 29("ACST"), 30("ACDT"), 31("AEST"), 32("AEDT")} | Which timezone device is in
/// @param[in] timezone Set(TimeZone) | <11> | { 0("WAT"), 1("AT"), 2("ADT"), 3("AST"), 4("EDT"), 5("EST"), 6("CDT"), 7("CST"), 8("MDT"), 9("MST"), 10("PDT"), 11("PST"), 12("AKDT"), 13("AKST"), 14("HST"), 15("SST"), 16("GMT"), 17("BST"), 18("CET"), 19("EET"), 20("EEST"), 21("BRT"), 22("ZP4"), 23("ZP5"), 24("ZP6"), 25("ZP7"), 26("AWST"), 27("ACST"), 28("AEST")} | Which timezone device is in
/// @param[in] use_local_time Bool | <false> | {true, false} | True for local time, false for UTC time
Loom_DS3231(
LoomManager* manager,
const TimeZone timezone = TimeZone::PST,
TimeZone timezone = TimeZone::PST,
const bool use_local_time = false
);

Expand Down
2 changes: 1 addition & 1 deletion src/RTC/PCF8523.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
///////////////////////////////////////////////////////////////////////////////
Loom_PCF8523::Loom_PCF8523(
LoomManager* manager,
const TimeZone timezone,
TimeZone timezone,
const bool use_local_time
)
: LoomRTC(manager, "PCF8523", Type::PCF8523, timezone, use_local_time )
Expand Down
4 changes: 2 additions & 2 deletions src/RTC/PCF8523.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class Loom_PCF8523 : public LoomRTC

/// Constructor
///
/// @param[in] timezone Set(TimeZone) | <11> | { 0("WAT"), 1("AT"), 2("ADT"), 3("AST"), 4("EDT"), 5("EST"), 6("CDT"), 7("CST"), 8("MDT"), 9("MST"), 10("PDT"), 11("PST"), 12("ALDT"), 13("ALST"), 14("HST"), 15("SST"), 16("GMT"), 17("BST"), 18("CET"), 19("CEST"), 20("EET"), 21("EEST"), 22("BT"), 23("ZP4"), 24("ZP5"), 25("ZP6"), 26("ZP7"), 27("AWST"), 28("AWDT"), 29("ACST"), 30("ACDT"), 31("AEST"), 32("AEDT")} | Which timezone device is in
/// @param[in] timezone Set(TimeZone) | <11> | { 0("WAT"), 1("AT"), 2("ADT"), 3("AST"), 4("EDT"), 5("EST"), 6("CDT"), 7("CST"), 8("MDT"), 9("MST"), 10("PDT"), 11("PST"), 12("AKDT"), 13("AKST"), 14("HST"), 15("SST"), 16("GMT"), 17("BST"), 18("CET"), 19("EET"), 20("EEST"), 21("BRT"), 22("ZP4"), 23("ZP5"), 24("ZP6"), 25("ZP7"), 26("AWST"), 27("ACST"), 28("AEST")} | Which timezone device is in
/// @param[in] use_local_time Bool | <false> | {true, false} | True for local time, false for UTC time
Loom_PCF8523(
LoomManager* manager,
const TimeZone timezone = TimeZone::PST,
TimeZone timezone = TimeZone::PST,
const bool use_local_time = false
);

Expand Down
191 changes: 162 additions & 29 deletions src/RTC/RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,23 @@ const float LoomRTC::timezone_adjustment[] =

8 /* PST */,

8 /* AKDT*/,
8 /* AKDT */,

9 /* AKST*/,
9 /* AKST */,

9 /* HADT */,
9 /* HST */,

10 /* HAST */,
11 /* SST */,

0 /* GMT */,

-1 /* BST */,

-1 /* CET */,

-2 /* CEST*/,

-2 /* EET */,

-3 /* EEST*/,
-3 /* EEST */,

-3 /* BRT */,

Expand All @@ -81,15 +79,9 @@ const float LoomRTC::timezone_adjustment[] =

-8 /* AWST*/,

-9 /* AWDT*/,

-9.5 /* ACST*/,

-10.5 /* ACDT*/,

-10 /* AEST*/,

-11 /* AEDT*/
-10 /* AEST*/
};

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -108,27 +100,23 @@ char* LoomRTC::enum_timezone_string(TimeZone t)
case TimeZone::MST : return "MST";
case TimeZone::PDT : return "PDT";
case TimeZone::PST : return "PST";
case TimeZone::ALDT : return "ALDT";
case TimeZone::ALST : return "ALST";
case TimeZone::AKDT : return "AKDT";
case TimeZone::AKST : return "AKST";
case TimeZone::HST : return "HST";
case TimeZone::SST : return "SST";
case TimeZone::GMT : return "GMT";
case TimeZone::BST : return "BST";
case TimeZone::CET : return "CET";
case TimeZone::CEST : return "CEST";
case TimeZone::EET : return "EET";
case TimeZone::EEST : return "EEST";
case TimeZone::BT : return "BT";
case TimeZone::BRT : return "BRT";
case TimeZone::ZP4 : return "ZP4";
case TimeZone::ZP5 : return "ZP5";
case TimeZone::ZP6 : return "ZP6";
case TimeZone::ZP7 : return "ZP7";
case TimeZone::AWST : return "AWST";
case TimeZone::AWDT : return "AWDT";
case TimeZone::ACST : return "ACST";
case TimeZone::ACDT : return "ACDT";
case TimeZone::AEST : return "AEST";
case TimeZone::AEDT : return "AEDT";
}
}

Expand All @@ -137,7 +125,7 @@ LoomRTC::LoomRTC(
LoomManager* manager,
const char* module_name,
const LoomModule::Type module_type,
const TimeZone timezone,
TimeZone timezone,
const bool use_local_time
)
: LoomModule(manager, module_name, module_type )
Expand Down Expand Up @@ -198,6 +186,7 @@ void LoomRTC::print_DateTime(const DateTime time)
///////////////////////////////////////////////////////////////////////////////
void LoomRTC::read_rtc()
{
convert_daylight_to_standard();
get_datestring();
get_timestring();
}
Expand Down Expand Up @@ -305,14 +294,14 @@ void LoomRTC::set_rtc_to_compile_time()
LPrintln("Time set to compile time:");
print_time();

// Adjust to UTC time if enabled
// Adjust to UTC time
if (!use_local_time) convert_local_to_utc();
/*
else {
// Add a function that will do the calculation for daylight saving
// Look at GitHub issue and function of convert_local_to_utc

// Adjust to daylight saving time or standard time if local time is enabled
else{
convert_daylight_to_standard();
}
*/

}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -339,7 +328,151 @@ void LoomRTC::convert_local_to_utc(const bool to_utc)

///////////////////////////////////////////////////////////////////////////////
void LoomRTC::convert_daylight_to_standard(){

/*
if(timezone == TimeZone::WAT || timezone == TimeZone::AT ||
timezone == TimeZone::HST || timezone == TimeZone::BRT ||
timezone == TimeZone::ZP4 || timezone == TimeZone::ZP5 ||
timezone == TimeZone::ZP6 || timezone == TimeZone::ZP7 ||
timezone == TimeZone::AWST || timezone == TimeZone::ACST ||
timezone == TimeZone::AEST || timezone == TimeZone::CET) LPrintln("No daylight saving or summer time in this time zone");
*/
if (timezone == TimeZone::ADT || timezone == TimeZone::AST ||
timezone == TimeZone::EDT || timezone == TimeZone::EST ||
timezone == TimeZone::CDT || timezone == TimeZone::CST ||
timezone == TimeZone::MDT || timezone == TimeZone::MST ||
timezone == TimeZone::PDT || timezone == TimeZone::PST ||
timezone == TimeZone::AKDT || timezone == TimeZone::AKST){
us_daylight_to_standard();
}
else if (timezone == TimeZone::BST || timezone == TimeZone::GMT ||
timezone == TimeZone::EEST || timezone == TimeZone::EET ){
eu_daylight_to_standard();
}

}

///////////////////////////////////////////////////////////////////////////////
void LoomRTC::us_daylight_to_standard(){
DateTime time = now();
if(time.dayOfTheWeek() == 0 && time.month() == 3 &&
time.day() >= 8 && time.day() <= 16 && time.hour() == 2 &&
time.minute() == 0 && time.second() == 0){
print_module_label();
LPrintln("Switch to Daylight Saving Time");
time = time + TimeSpan(0, 1, 0, 0);
_adjust(time);
switch(timezone) {
case TimeZone::AST :
timezone = TimeZone::ADT;
break;

case TimeZone::EST :
timezone = TimeZone::EDT;
break;

case TimeZone::CST :
timezone = TimeZone::CDT;
break;

case TimeZone::MST :
timezone = TimeZone::MDT;
break;

case TimeZone::PST :
timezone = TimeZone::PDT;
break;

case TimeZone::AKST :
timezone = TimeZone::AKDT;
break;

default:
LPrintln("Something went wrong, please submit a Github issue");
}

}
else if(time.dayOfTheWeek() == 0 && time.month() == 11 &&
time.day() >= 1 && time.day() <= 8 && time.hour() == 2 &&
time.minute() == 0 && time.second() == 0){
print_module_label();
LPrintln("Switch to Standard Time");
time = time + TimeSpan(0, -1, 0, 0);
_adjust(time);
switch(timezone) {
case TimeZone::ADT :
timezone = TimeZone::AST;
break;

case TimeZone::EDT :
timezone = TimeZone::EST;
break;

case TimeZone::CDT :
timezone = TimeZone::CST;
break;

case TimeZone::MDT :
timezone = TimeZone::MST;
break;

case TimeZone::PDT :
timezone = TimeZone::PST;
break;

case TimeZone::AKDT :
timezone = TimeZone::AKST;
break;

default:
LPrintln("Something went wrong, please submit a Github issue");
}
}
}

///////////////////////////////////////////////////////////////////////////////
void LoomRTC::eu_daylight_to_standard(){

DateTime time = now();
if(time.dayOfTheWeek() == 0 && time.month() == 3 &&
time.day() >= 25 && time.day() <= 31 && time.hour() == 1 &&
time.minute() == 0 && time.second() == 0){
print_module_label();
LPrintln("Switch to Summer Time");
time = time + TimeSpan(0, 1, 0, 0);
_adjust(time);
switch(timezone) {
case TimeZone::GMT :
timezone = TimeZone::BST;
break;

case TimeZone::EET :
timezone = TimeZone::EEST;
break;

default:
LPrintln("Something went wrong, please submit a Github issue");
}
}
else if(time.dayOfTheWeek() == 0 && time.month() == 10 &&
time.day() >= 25 && time.day() <= 31 && time.hour() == 1 &&
time.minute() == 0 && time.second() == 0){
print_module_label();
LPrintln("Switch to Standard Time");
time = time + TimeSpan(0, -1, 0, 0);
_adjust(time);
switch(timezone) {
case TimeZone::BST :
timezone = TimeZone::GMT;
break;

case TimeZone::EEST :
timezone = TimeZone::EET;
break;

default:
LPrintln("Something went wrong, please submit a Github issue");
}
}
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
16 changes: 11 additions & 5 deletions src/RTC/RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class LoomRTC : public LoomModule

/// Different time zones
enum class TimeZone {
WAT = 0, AT, ADT, AST, EDT, EST, CDT, CST, MDT, MST, PDT, PST, ALDT,
ALST, HST, SST, GMT, BST, CET, CEST, EET, EEST, BT, ZP4, ZP5,
ZP6, ZP7, AWST, AWDT, ACST, ACDT, AEST, AEDT
WAT = 0, AT, ADT, AST, EDT, EST, CDT, CST, MDT, MST, PDT, PST, AKDT,
AKST, HST, SST, GMT, BST, CET, EET, EEST, BRT, ZP4, ZP5,
ZP6, ZP7, AWST, ACST, AEST
};

private:
Expand Down Expand Up @@ -67,7 +67,7 @@ class LoomRTC : public LoomModule
LoomManager* manager,
const char* module_name,
const LoomModule::Type module_type,
const TimeZone timezone,
TimeZone timezone,
const bool use_local_time
);

Expand Down Expand Up @@ -211,9 +211,15 @@ class LoomRTC : public LoomModule
/// @return True if valid
bool rtc_validity_check();


/// Convert time between daylight saving time and standard time
void convert_daylight_to_standard();

/// Convert the time in the US daylight saving and standard
void us_daylight_to_standard();

/// Convert the time in the EU summer time and standard
void eu_daylight_to_standard();

};


0 comments on commit f2cb848

Please sign in to comment.