-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create EU DST example #308
base: master
Are you sure you want to change the base?
Changes from 1 commit
7ca75cd
e9f4efc
5b98cfd
5b81a04
fc6647c
5b55949
d88958f
56f8a79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||||||||||||||||||
/* EU DST example */ | ||||||||||||||||||||||
/* version 1: dd 23-10-2024 */ | ||||||||||||||||||||||
J-Brinkman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
#include <RTClib.h> // https://github.com/adafruit/rtclib | ||||||||||||||||||||||
RTC_DS3231 rtc; | ||||||||||||||||||||||
//RTC_DS1307 rtc; | ||||||||||||||||||||||
|
||||||||||||||||||||||
#define USEDST true // Use DST (true or false) | ||||||||||||||||||||||
|
||||||||||||||||||||||
DateTime now; | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be local to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is also used in the setup part. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot see the variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that is not wise to do.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Boolean? I am talking about the variable |
||||||||||||||||||||||
|
||||||||||||||||||||||
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 | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have here two calls to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand. It is called only twice. For March as well as for October. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is always called twice. What I mean is that, most of the year, you don't need to call Here is a version of the function (untested) that never calls // Return the given DateTime adjusted for DST.
// The provided DateTime is assumed to be in UTC+01:00 (i.e. CET, with no DST correction).
// The result will be in CET, with the DST correction applied if needed.
DateTime dstclock(DateTime n) {
bool isDst;
if (!USEDST) {
isDst = false;
} else if (n.month() < 3 || (n.month() == 3 && n.day() < 25)) {
isDST = false; // winter time before 25 March
} else if (n.month() == 3) {
// DST starts on last Sunday of March at 02:00 UTC+01:00
uint8_t dow = n.dayOfTheWeek();
isDst = (dow == 0 && n.hour() >= 2) || (dow > 0 && n.day() - dow >= 25);
} else if (n.month() < 10 || (n.month == 10 && n.day() < 25)) {
isDst = true; // summer time before 25 October
} else if (n.month() == 10) {
// DST ends on last Sunday of October at 02:00 UTC+01:00
uint8_t dow = n.dayOfTheWeek();
isDst = (dow == 0 && n.hour() < 2) || (dow > 0 && n.day() - dow < 25);
} else { // n.month() > 10
isDst = false; // winter time after October
}
if (isDst)
n = n + TimeSpan(0, 1, 0, 0);
return n;
} This is more source code but, being an I just noticed that, given that this function expects its input to always be in UTC+01:00 (CET winter time), the DST switch always happens at 02:00. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, makes a lot of sense! Note, however, that the time switch should not happen at 01:00 or 03:00, but always at 02:00. This is because, as you have now documented, the RTC is kept in winter time all year long, so this function expects its parameter |
||||||||||||||||||||||
|
||||||||||||||||||||||
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; | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid code duplication:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not duplicated. For both the begin and the end date the last sunday is relevant. So the function should be called twice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I mean is that DateTime getclock() {
return dstclock(rtc.now());
} |
||||||||||||||||||||||
}; | ||||||||||||||||||||||
|
||||||||||||||||||||||
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 | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not rely on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid point! rtc.adjust(n); should be added to line 36. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DST support v2.txt There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For amending a pull request, you can simply push extra commits to your “patch-1” branch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, that helped! |
||||||||||||||||||||||
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 | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't compile: the class |
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
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 | ||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This deserves a few more explanations, stating for instance that:
The RTC is kept in local winter time. This is not obvious at first sight, and is quite non-standard (common practice is to keep the RTC either in UTC or in local time).
This code is for CET only. It can work across Europe if one is willing to accept the DST change to be one hour off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very relevant suggestions.
Point 1 appears to be the key to make it a very simple procedure.
Point 2 The hour is now set to 1 and 3. This can be adapted to your local time (for instance at 2 and 4) if applicable. Therefore it is an example.
I would love to add these point to a comment in the proposed code, but have problems to find out how. (This is the very first pull request I ever do).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe a comment explaining how to do so would be of some value.