From 7ca75cd97398b0cb57f41feff6b981f7ece10dbb Mon Sep 17 00:00:00 2001 From: J-Brinkman <77969387+J-Brinkman@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:47:04 +0200 Subject: [PATCH 1/8] Create EU DST example As discussed in Issue #307 --- examples/EU DST example | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/EU DST example diff --git a/examples/EU DST example b/examples/EU DST example new file mode 100644 index 00000000..7fdf22b3 --- /dev/null +++ b/examples/EU DST example @@ -0,0 +1,52 @@ +/* EU DST example */ +/* version 1: dd 23-10-2024 */ + +#include // https://github.com/adafruit/rtclib +RTC_DS3231 rtc; +//RTC_DS1307 rtc; + +#define USEDST true // Use DST (true or false) + +DateTime now; + +DateTime dstclock(DateTime n) { // Return the given (DST adjusted) date and time according to DST settings (aespecially for date/time checking) + + DateTime b, e; + + b = DateTime(n.year(), 3, 31, 3, 0, 0); b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 1, 0, 0); // Last sunday in march 1:00 + e = DateTime(n.year(), 10, 31, 3, 0, 0); e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 3, 0, 0); // Last sunday in October 3:00 + + if (USEDST && (n > b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust standard time if within summertime + return n; +}; + +DateTime getclock() { // Retrieve the (DST adjusted) date and time + + DateTime n, b, e; + + n = rtc.now(); + b = DateTime(n.year(), 3, 31, 3, 0, 0); b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 1, 0, 0); // Last sunday in march 1:00 + e = DateTime(n.year(), 10, 31, 3, 0, 0); e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 3, 0, 0); // Last sunday in October 3:00 + + if (USEDST && (n > b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust 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 && (rtc.now() != getclock())) n = n - TimeSpan(0,1,0,0); // if summertime then adjust to the standard time or + if (USEDST && (rtc.now() != dstclock(rtc.now()))) n = n - TimeSpan(0,1,0,0); // if summertime then adjust to the standard time or + 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 +} + +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 and do date/time checking +} From e9f4efced4d33432e753e520a9f92ef3828a2028 Mon Sep 17 00:00:00 2001 From: J-Brinkman <77969387+J-Brinkman@users.noreply.github.com> Date: Mon, 14 Oct 2024 08:23:20 +0200 Subject: [PATCH 2/8] Update EU DST example Version 3 with all comments processed --- examples/EU DST example | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/examples/EU DST example b/examples/EU DST example index 7fdf22b3..1465eac7 100644 --- a/examples/EU DST example +++ b/examples/EU DST example @@ -1,5 +1,9 @@ /* EU DST example */ -/* version 1: dd 23-10-2024 */ +/* 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. +// The DST time can be changed if required, by choosing a different hour in the DateTime function (4th parameter). #include // https://github.com/adafruit/rtclib RTC_DS3231 rtc; @@ -7,16 +11,18 @@ RTC_DS3231 rtc; #define USEDST true // Use DST (true or false) -DateTime now; +DateTime now; -DateTime dstclock(DateTime n) { // Return the given (DST adjusted) date and time according to DST settings (aespecially for date/time checking) +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, 3, 0, 0); b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 1, 0, 0); // Last sunday in march 1:00 - e = DateTime(n.year(), 10, 31, 3, 0, 0); e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 3, 0, 0); // Last sunday in October 3:00 + 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 + 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 (USEDST && (n > b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust standard time if within summertime + if (USEDST && (n > b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust to standard time if within summertime return n; }; @@ -25,28 +31,29 @@ DateTime getclock() { // Retrieve the (DST adjusted) date and time DateTime n, b, e; n = rtc.now(); - b = DateTime(n.year(), 3, 31, 3, 0, 0); b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 1, 0, 0); // Last sunday in march 1:00 - e = DateTime(n.year(), 10, 31, 3, 0, 0); e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 3, 0, 0); // Last sunday in October 3:00 + 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 + 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 (USEDST && (n > b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust standard time if within summertime + 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 && (rtc.now() != getclock())) n = n - TimeSpan(0,1,0,0); // if summertime then adjust to the standard time or - if (USEDST && (rtc.now() != dstclock(rtc.now()))) n = n - TimeSpan(0,1,0,0); // if summertime then adjust to the standard time or + 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.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 } 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 and do date/time checking +// now = dstclock(rtc.now()); // read the time from the RTC and adjust for DST } From 5b98cfda86d8ab97ae9ff2383fc0a43cf5db908a Mon Sep 17 00:00:00 2001 From: J-Brinkman <77969387+J-Brinkman@users.noreply.github.com> Date: Mon, 14 Oct 2024 12:43:35 +0200 Subject: [PATCH 3/8] Update EU DST example This time it compiles :-) and a DST demonstration on the serial port is added. --- examples/EU DST example | 45 ++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/examples/EU DST example b/examples/EU DST example index 1465eac7..7228f44a 100644 --- a/examples/EU DST example +++ b/examples/EU DST example @@ -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. @@ -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; @@ -32,9 +33,9 @@ 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; @@ -42,18 +43,44 @@ DateTime getclock() { // Retrieve the (DST adjusted) date and time 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 + } } From 5b81a04d0ffd8b19375b3bf77b439d5460fa7fb5 Mon Sep 17 00:00:00 2001 From: J-Brinkman <77969387+J-Brinkman@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:24:31 +0200 Subject: [PATCH 4/8] Create DST_Europe.ino Minor improvements, combined with new filename and directory --- examples/DST_Europe/DST_Europe.ino | 96 ++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 examples/DST_Europe/DST_Europe.ino diff --git a/examples/DST_Europe/DST_Europe.ino b/examples/DST_Europe/DST_Europe.ino new file mode 100644 index 00000000..7bd7b298 --- /dev/null +++ b/examples/DST_Europe/DST_Europe.ino @@ -0,0 +1,96 @@ +/* European Daylight Saving Time (DST) example */ + +// The Real Time Clock 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. +// The DST time can be changed if required, by choosing a different hour in the DateTime function (4th parameter). + +#include // https://github.com/adafruit/rtclib +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 (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 (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; +}; + +DateTime getclock() { // Retrieve the (DST adjusted) date and time + + DateTime n, b, e; + + n = rtc.now(); + b = DateTime(n.year(), 3, 31, 1, 0, 0); // Begin of DST: set on March 31 1:000 (am) + 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 (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 + 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 + + // 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 + + // show standard (winter) time + now = rtc.now(); // retreive the winter time + Serial.print(" Actual standard time: "); + Serial.print(now.timestamp(DateTime::TIMESTAMP_DATE)); Serial.print(" "); // print the date + Serial.println(now.timestamp(DateTime::TIMESTAMP_TIME)); // print the time + + // show DST corrected time (only when in DST period, otherwise this equals the standard time) + Serial.print("Actual DST corrected time: "); + now = getclock(); // get DST corrected time + Serial.print(now.timestamp(DateTime::TIMESTAMP_DATE)); Serial.print(" "); // print the date + Serial.print(now.timestamp(DateTime::TIMESTAMP_TIME)); Serial.println(""); // print the time + Serial.println(""); + delay(5000); + + Serial.println("Demonstration of the DST activation"); + Serial.println("-----------------------------------"); + Serial.println(" DST starts at: 2024-03-31 01:00:00"); + Serial.println("First DST time: 2024-03-31 02:00:01"); + Serial.println(""); + delay(5000); + setclock(DateTime(2024, 3, 31, 0, 59, 45)); // Set the clock 15 seconds 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 + if (lastsec != now.second()) { // if a new second + Serial.print(now.timestamp(DateTime::TIMESTAMP_DATE)); Serial.print(" "); // print the date + Serial.print(now.timestamp(DateTime::TIMESTAMP_TIME)); Serial.println(""); // print the time + lastsec = now.second(); // store the current second + } +} From fc6647cadf192da257a84148e9db236b85a30313 Mon Sep 17 00:00:00 2001 From: J-Brinkman <77969387+J-Brinkman@users.noreply.github.com> Date: Mon, 14 Oct 2024 17:26:20 +0200 Subject: [PATCH 5/8] Delete examples/EU DST example Replaced by newer verion on other location --- examples/EU DST example | 86 ----------------------------------------- 1 file changed, 86 deletions(-) delete mode 100644 examples/EU DST example diff --git a/examples/EU DST example b/examples/EU DST example deleted file mode 100644 index 7228f44a..00000000 --- a/examples/EU DST example +++ /dev/null @@ -1,86 +0,0 @@ -/* EU DST example */ - -// 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. -// The DST time can be changed if required, by choosing a different hour in the DateTime function (4th parameter). - -#include // https://github.com/adafruit/rtclib -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 (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 (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; -}; - -DateTime getclock() { // Retrieve the (DST adjusted) date and time - - DateTime n, b, e; - - n = rtc.now(); - b = DateTime(n.year(), 3, 31, 1, 0, 0); // Begin of DST: set on March 31 1:000 (am) - 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 (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 - 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 - - // 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 - if (lastsec != now.second()) { // if a new second - Serial.println(now.timestamp()); // print the time - lastsec = now.second(); // store the current second - } -} From 5b55949afb4b67b2cc22c66e594d02bc5aa7b42f Mon Sep 17 00:00:00 2001 From: J-Brinkman <77969387+J-Brinkman@users.noreply.github.com> Date: Mon, 14 Oct 2024 17:39:40 +0200 Subject: [PATCH 6/8] Update DST_Europe.ino I sincerely hope all the comments are processed this time. --- examples/DST_Europe/DST_Europe.ino | 40 +++++++++++------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/examples/DST_Europe/DST_Europe.ino b/examples/DST_Europe/DST_Europe.ino index 7bd7b298..f76ac1c3 100644 --- a/examples/DST_Europe/DST_Europe.ino +++ b/examples/DST_Europe/DST_Europe.ino @@ -1,7 +1,7 @@ -/* European Daylight Saving Time (DST) example */ +/* European Daylight Saving Time (DST) */ // The Real Time Clock 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. +// This code is for a DST from Last sunday in March 2:00 (am) local time until Last sunday in October 3:00 (am) local time. // The DST time can be changed if required, by choosing a different hour in the DateTime function (4th parameter). #include // https://github.com/adafruit/rtclib @@ -18,27 +18,17 @@ DateTime dstclock(DateTime n) { // Return the given (DST adjusted) date and time DateTime b, e; - b = DateTime(n.year(), 3, 31, 1, 0, 0); // Begin of DST: set on March 31 1:000 (am) - 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 (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 + b = DateTime(n.year(), 3, 31, 2, 0, 0); // Begin of DST: set on March 31 2:00 (am) + if (n.month() == 3) b = DateTime(n.year(), 3, 31 - b.dayOfTheWeek(), 2, 0, 0); // Begin of DST: adjusted to last sunday in March 2:00 (am) when actual month is March + e = DateTime(n.year(), 10, 31, 2, 0, 0); // End of DST: set on October 31 2:00 (am) + if (n.month() == 10) e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 2, 0, 0); // End of DST: adjusted to last sunday in October 2: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 + if (USEDST && (n >= b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust to standard time if within summertime return n; }; DateTime getclock() { // Retrieve the (DST adjusted) date and time - - DateTime n, b, e; - - n = rtc.now(); - b = DateTime(n.year(), 3, 31, 1, 0, 0); // Begin of DST: set on March 31 1:000 (am) - 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 (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; + return dstclock(rtc.now()); }; void setclock(DateTime n) { // if the clock is set during summertime then adjust the clock to standard time @@ -55,14 +45,13 @@ void setup() { // 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 // show standard (winter) time - now = rtc.now(); // retreive the winter time + now = rtc.now(); // retreive the standard time Serial.print(" Actual standard time: "); Serial.print(now.timestamp(DateTime::TIMESTAMP_DATE)); Serial.print(" "); // print the date Serial.println(now.timestamp(DateTime::TIMESTAMP_TIME)); // print the time @@ -75,19 +64,18 @@ void setup() { Serial.println(""); delay(5000); - Serial.println("Demonstration of the DST activation"); - Serial.println("-----------------------------------"); - Serial.println(" DST starts at: 2024-03-31 01:00:00"); - Serial.println("First DST time: 2024-03-31 02:00:01"); + Serial.println("- Demonstration of the DST activation -"); + Serial.println("---------------------------------------"); + Serial.println("Last standard time: 2024-03-31 01:59:59"); + Serial.println(" First DST time: 2024-03-31 03:00:00"); Serial.println(""); delay(5000); - setclock(DateTime(2024, 3, 31, 0, 59, 45)); // Set the clock 15 seconds before DST starts + setclock(DateTime(2024, 3, 31, 1, 59, 45)); // Set the clock 15 seconds 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 if (lastsec != now.second()) { // if a new second Serial.print(now.timestamp(DateTime::TIMESTAMP_DATE)); Serial.print(" "); // print the date Serial.print(now.timestamp(DateTime::TIMESTAMP_TIME)); Serial.println(""); // print the time From d88958fe4a9bee9fef5c5d2eae95b6ff515ddfda Mon Sep 17 00:00:00 2001 From: J-Brinkman <77969387+J-Brinkman@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:55:30 +0200 Subject: [PATCH 7/8] Update DST_Europe.ino Improved comments and replacment of getclock procedure by a define statement. Because improving code readability with a #define is more elegant than with an extra function that consumes memory and cycles. --- examples/DST_Europe/DST_Europe.ino | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/DST_Europe/DST_Europe.ino b/examples/DST_Europe/DST_Europe.ino index f76ac1c3..aea4a04a 100644 --- a/examples/DST_Europe/DST_Europe.ino +++ b/examples/DST_Europe/DST_Europe.ino @@ -10,6 +10,7 @@ RTC_DS3231 rtc; #define USEDST true // Use DST (true or false) #define BAUD 57600 // Baud rate for the serial monitor +#define getclock() dstclock(rtc.now()) // Improve code readability: replace getclock() with dstclock(rtc.now()) DateTime now; int lastsec; @@ -23,17 +24,13 @@ DateTime dstclock(DateTime n) { // Return the given (DST adjusted) date and time e = DateTime(n.year(), 10, 31, 2, 0, 0); // End of DST: set on October 31 2:00 (am) if (n.month() == 10) e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 2, 0, 0); // End of DST: adjusted to last sunday in October 2: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 + if (USEDST && (n >= b) && (n < e)) n = n + TimeSpan(0,1,0,0); // adjust to standard time if DST is used and within summertime return n; }; -DateTime getclock() { // Retrieve the (DST adjusted) date and time - return dstclock(rtc.now()); -}; - 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 DST is used and within summertime then adjust to the standard time rtc.adjust(n); // Set the clock to standard time }; @@ -41,7 +38,7 @@ 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.isrunning()) setclock(DateTime(F(__DATE__), F(__TIME__))); // Set date and time for use with the DS1307 RTC // initialise the serial port Serial.begin(BAUD); @@ -56,7 +53,7 @@ void setup() { Serial.print(now.timestamp(DateTime::TIMESTAMP_DATE)); Serial.print(" "); // print the date Serial.println(now.timestamp(DateTime::TIMESTAMP_TIME)); // print the time - // show DST corrected time (only when in DST period, otherwise this equals the standard time) + // show the DST corrected time (only when in DST period, otherwise this equals the standard time) Serial.print("Actual DST corrected time: "); now = getclock(); // get DST corrected time Serial.print(now.timestamp(DateTime::TIMESTAMP_DATE)); Serial.print(" "); // print the date @@ -64,6 +61,7 @@ void setup() { Serial.println(""); delay(5000); + // show the DST activation Serial.println("- Demonstration of the DST activation -"); Serial.println("---------------------------------------"); Serial.println("Last standard time: 2024-03-31 01:59:59"); @@ -75,7 +73,7 @@ void setup() { } void loop() { - now = getclock(); // read the time from the RTC and adjust for DST or + now = getclock(); // read the time from the RTC and adjust for DST if (lastsec != now.second()) { // if a new second Serial.print(now.timestamp(DateTime::TIMESTAMP_DATE)); Serial.print(" "); // print the date Serial.print(now.timestamp(DateTime::TIMESTAMP_TIME)); Serial.println(""); // print the time From 56f8a790f74ac372164f146d50e1d59e32a99eac Mon Sep 17 00:00:00 2001 From: J-Brinkman <77969387+J-Brinkman@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:23:10 +0200 Subject: [PATCH 8/8] Update DST_Europe.ino with enhancd dstclock function The DSTclock procedure is made visible so that it can be used to convert time from and to DST time. However, if the need exists to convert the actual time to standard time, this procedure did not function correctly. This is improved in this new version. --- examples/DST_Europe/DST_Europe.ino | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/DST_Europe/DST_Europe.ino b/examples/DST_Europe/DST_Europe.ino index aea4a04a..d45a5f83 100644 --- a/examples/DST_Europe/DST_Europe.ino +++ b/examples/DST_Europe/DST_Europe.ino @@ -8,14 +8,16 @@ RTC_DS3231 rtc; //RTC_DS1307 rtc; -#define USEDST true // Use DST (true or false) #define BAUD 57600 // Baud rate for the serial monitor -#define getclock() dstclock(rtc.now()) // Improve code readability: replace getclock() with dstclock(rtc.now()) +#define USEDST true // Use DST (true or false) +#define StoA 1 // Standard (stored) time to actual time +#define AtoS 2 // Actual time to standard (stored) time +#define getclock() dstclock(rtc.now(), 1) // Improve code readability: replace getclock() with dstclock(rtc.now(), StoA) 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 dstclock(DateTime n, int type) { // Return the given (DST adjusted) date and time according to DST settings (for extensive date and time calculations) DateTime b, e; @@ -24,14 +26,15 @@ DateTime dstclock(DateTime n) { // Return the given (DST adjusted) date and time e = DateTime(n.year(), 10, 31, 2, 0, 0); // End of DST: set on October 31 2:00 (am) if (n.month() == 10) e = DateTime(n.year(), 10, 31 - e.dayOfTheWeek(), 2, 0, 0); // End of DST: adjusted to last sunday in October 2: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 DST is used and within summertime + if (USEDST) { + if ((type == StoA) && (n >= b) && (n < e)) n = n + TimeSpan(0,1,0,0); else // adjust standard (winter) to actual time if DST is used and within summertime + if ((type == AtoS) && (n >= b) && (n < e)) n = n - TimeSpan(0,1,0,0); // adjust actual to standard (winter) time if DST is used and 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 DST is used and within summertime then adjust to the standard time - rtc.adjust(n); // Set the clock to standard time + rtc.adjust(dstclock(n, AtoS)); // Set the clock to standard time }; void setup() {