/* EU DST example */ /* version 2: dd 23-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 local time until Last sunday in October 3:00 local time. This can be changed if required. #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 (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 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, 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 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 } 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 calculations }