-
Notifications
You must be signed in to change notification settings - Fork 724
Create EU DST example #308
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
Open
J-Brinkman
wants to merge
8
commits into
adafruit:master
Choose a base branch
from
J-Brinkman:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7ca75cd
Create EU DST example
J-Brinkman e9f4efc
Update EU DST example
J-Brinkman 5b98cfd
Update EU DST example
J-Brinkman 5b81a04
Create DST_Europe.ino
J-Brinkman fc6647c
Delete examples/EU DST example
J-Brinkman 5b55949
Update DST_Europe.ino
J-Brinkman d88958f
Update DST_Europe.ino
J-Brinkman 56f8a79
Update DST_Europe.ino with enhancd dstclock function
J-Brinkman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* 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 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 <RTClib.h> // 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, 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 | ||
| 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 | ||
| 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); | ||
| 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 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 | ||
|
|
||
| // 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("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, 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 | ||
| 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 | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Contrary to what the comment says, the lines below are not conditioned on the clock being in DST period.
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.
----------------- New start ------------------
Actual standard time: 2024-10-14 16:21:11
Actual DST corrected time: 2024-10-14 17:21:11 <- corrected because in DST
----------------- New start ------------------
Actual standard time: 2024-11-14 16:21:11
Actual DST corrected time: 2024-11-14 16:21:11 <- not corrected because not in DST