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] 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 +}