Skip to content
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
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 52 additions & 1 deletion src/Timezone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include <avr/eeprom.h>
#endif

#ifdef ESP_PLATFORM
#include <Preferences.h>
#endif

/*----------------------------------------------------------------------*
* Create a Timezone object from the given time change rules. *
*----------------------------------------------------------------------*/
Expand All @@ -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. *
Expand All @@ -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. *
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion src/Timezone.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ 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);
bool utcIsDST(time_t utc);
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);
Expand All @@ -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