Skip to content

Commit

Permalink
Update EU DST example
Browse files Browse the repository at this point in the history
This time it compiles :-) and a DST demonstration on the serial port is added.
  • Loading branch information
J-Brinkman authored Oct 14, 2024
1 parent e9f4efc commit 5b98cfd
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions examples/EU DST example
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* EU DST example */
/* version 3: dd 14-10-2024 */

// The RTC is kept in local winter time (standard time). This approach is key for the simplicity of this solution.
// This code is for a DST from Last sunday in March 1:00 (am) local time until Last sunday in October 3:00 (am) local time.
Expand All @@ -10,17 +9,19 @@ RTC_DS3231 rtc;
//RTC_DS1307 rtc;

#define USEDST true // Use DST (true or false)
#define BAUD 57600 // Baud rate for the serial monitor

DateTime now;
int lastsec;

DateTime dstclock(DateTime n) { // Return the given (DST adjusted) date and time according to DST settings (for extensive date and time calculations)

DateTime b, e;

b = DateTime(n.year(), 3, 31, 1, 0, 0); // Begin of DST: set on March 31 1:000 (am)
if (month(n) = 3) b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 1, 0, 0); // Begin of DST: adjusted to last sunday in March 1:00 (am) when actual month is March
if (n.month() == 3) b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 1, 0, 0); // Begin of DST: adjusted to last sunday in March 1:00 (am) when actual month is March
e = DateTime(n.year(), 10, 31, 3, 0, 0); // End of DST: set on October 31 3:00 (am)
if (month(n) = 10) e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 3, 0, 0); // End of DST: adjusted to last sunday in October 3:00 (am) when actual month is October
if (n.month() == 10) e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 3, 0, 0); // End of DST: adjusted to last sunday in October 3:00 (am) when actual month is October

if (USEDST && (n > b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust to standard time if within summertime
return n;
Expand All @@ -32,28 +33,54 @@ DateTime getclock() { // Retrieve the (DST adjusted) date and time

n = rtc.now();
b = DateTime(n.year(), 3, 31, 1, 0, 0); // Begin of DST: set on March 31 1:000 (am)
if (month(n) = 3) b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 1, 0, 0); // Begin of DST: adjusted to last sunday in March 1:00 (am) when actual month is March
if (n.month() == 3) b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 1, 0, 0); // Begin of DST: adjusted to last sunday in March 1:00 (am) when actual month is March
e = DateTime(n.year(), 10, 31, 3, 0, 0); // End of DST: set on October 31 3:00 (am)
if (month(n) = 10) e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 3, 0, 0); // End of DST: adjusted to last sunday in October 3:00 (am) when actual month is October
if (n.month() == 10) e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 3, 0, 0); // End of DST: adjusted to last sunday in October 3:00 (am) when actual month is October

if (USEDST && (n > b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust to standard time if within summertime
return n;
};

void setclock(DateTime n) { // if the clock is set during summertime then adjust the clock to standard time

if (USEDST && (n != dstclock(n)) n = n - TimeSpan(0,1,0,0); // if summertime then adjust to the standard time
if (USEDST && (n != dstclock(n))) n = n - TimeSpan(0,1,0,0); // if summertime then adjust to the standard time
rtc.adjust(n); // Set the clock to standard time
};

void setup() {
// initialise the rtc
rtc.begin();
// if (rtc.lostPower()) setclock(DateTime(F(__DATE__), F(__TIME__))); // Set date and time for use with the DS3231 RTC
if (!rtc.isrunning()) setclock(DateTime(F(__DATE__), F(__TIME__))); // Set date and time for use with the DS1307 RTC
if (rtc.lostPower()) setclock(DateTime(F(__DATE__), F(__TIME__))); // Set date and time for use with the DS3231 RTC
// if (!rtc.isrunning()) setclock(DateTime(F(__DATE__), F(__TIME__))); // Set date and time for use with the DS1307 RTC

// initialise the serial port
Serial.begin(BAUD);
Wire.begin();
Serial.println(""); Serial.println(""); Serial.println("---------------- New start ------------------");;

// Start the DST demonstration
setclock(DateTime(F(__DATE__), F(__TIME__))); // Set the clock to compile date and time
Serial.print(" Actual standard time:");
now = rtc.now(); Serial.println(now.timestamp()); // show standard (winter) time
Serial.print("Actual DST corrected time:");
now = getclock(); Serial.println(now.timestamp()); // show DST corrected time (only when in DST period, otherwise this equals the standard time)
Serial.println("");
delay(5000);
Serial.println("Demonstration of the DST activation");
Serial.println("-----------------------------------");
Serial.println(" DST starts at: 2024-03-31T01:00:00");
Serial.println("First DST time: 2024-03-31T02:00:01");
Serial.println("");
delay(5000);
setclock(DateTime(2024, 3, 31, 0, 59, 45)); // Set the time 15 soconds before DST starts
lastsec = now.second();
}

void loop() {
now = getclock(); // read the time from the RTC and adjust for DST or
// now = dstclock(rtc.now()); // read the time from the RTC and adjust for DST
// now = dstclock(rtc.now()); // read the time from the RTC and adjust for DST
if (lastsec != now.second()) { // if a new second
Serial.println(now.timestamp()); // print the time
lastsec = now.second(); // store the current second
}
}

0 comments on commit 5b98cfd

Please # to comment.