diff --git a/library.properties b/library.properties index 983999e..e6188f0 100644 --- a/library.properties +++ b/library.properties @@ -6,5 +6,5 @@ sentence=Arduino library to facilitate time zone conversions and automatic dayli paragraph=The primary aim of the Timezone library is to convert Universal Coordinated Time (UTC) to the correct local time, whether it is daylight saving time (a.k.a. summer time) or standard time. category=Timing url=https://github.com/JChristensen/Timezone -architectures=avr -depends=Time +architectures=avr,esp32 +depends=Time \ No newline at end of file diff --git a/src/Timezone.cpp b/src/Timezone.cpp index fbf4fdf..73a9472 100644 --- a/src/Timezone.cpp +++ b/src/Timezone.cpp @@ -12,6 +12,10 @@ #include #endif +#ifdef ESP_PLATFORM + #include +#endif + /*----------------------------------------------------------------------* * Create a Timezone object from the given time change rules. * *----------------------------------------------------------------------*/ @@ -31,7 +35,7 @@ Timezone::Timezone(TimeChangeRule stdTime) initTimeChanges(); } -#ifdef __AVR__ +#ifdef AVR /*----------------------------------------------------------------------* * Create a Timezone object from time change rules stored in EEPROM * * at the given address. * @@ -42,6 +46,13 @@ Timezone::Timezone(int address) } #endif +#ifdef ESP_PLATFORM +Timezone::Timezone(char* key) +{ + readRules(key); +} +#endif + /*----------------------------------------------------------------------* * Convert the given UTC time to local time, standard or * * daylight time, as appropriate. * @@ -241,3 +252,43 @@ void Timezone::writeRules(int address) } #endif + +#ifdef ESP_PLATFORM +/*----------------------------------------------------------------------* + * Read the daylight and standard time rules from Flash at * + * the given key. * + *----------------------------------------------------------------------*/ +void Timezone::readRules(char* key) +{ + Preferences preferences; + preferences.begin("timezone", true); + + // Read m_dst and m_std values from preferences + if (preferences.getBytes((String(key) + "_dst").c_str(), &m_dst, sizeof(m_dst)) == 0) { + Serial.println("Error: DST rule not found in preferences."); + } + if (preferences.getBytes((String(key) + "_std").c_str(), &m_std, sizeof(m_std)) == 0) { + Serial.println("Error: Standard rule not found in preferences."); + } + + preferences.end(); + + initTimeChanges(); +} + +/*----------------------------------------------------------------------* + * Write the daylight and standard time rules to Flash at * + * the given key. * + *----------------------------------------------------------------------*/ +void Timezone::writeRules(char* key) +{ + Preferences preferences; + preferences.begin("timezone", false); + + // Write m_dst and m_std values to preferences + preferences.putBytes((String(key) + "_dst").c_str(), &m_dst, sizeof(m_dst)); + preferences.putBytes((String(key) + "_std").c_str(), &m_std, sizeof(m_std)); + + preferences.end(); +} +#endif \ No newline at end of file diff --git a/src/Timezone.h b/src/Timezone.h index 8d353ec..2781066 100644 --- a/src/Timezone.h +++ b/src/Timezone.h @@ -38,6 +38,7 @@ class Timezone Timezone(TimeChangeRule dstStart, TimeChangeRule stdStart); Timezone(TimeChangeRule stdTime); Timezone(int address); + Timezone(char* key); time_t toLocal(time_t utc); time_t toLocal(time_t utc, TimeChangeRule **tcr); time_t toUTC(time_t local); @@ -45,7 +46,9 @@ class Timezone bool locIsDST(time_t local); void setRules(TimeChangeRule dstStart, TimeChangeRule stdStart); void readRules(int address); + void readRules(char* key); void writeRules(int address); + void writeRules(char* key); private: void calcTimeChanges(int yr); @@ -58,4 +61,4 @@ class Timezone time_t m_dstLoc; // dst start for given/current year, given in local time time_t m_stdLoc; // std time start for given/current year, given in local time }; -#endif +#endif \ No newline at end of file