-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Automatic DST support #3072
Automatic DST support #3072
Changes from 1 commit
eae76b9
dd19387
37c3280
d861ee8
6ad6cc3
48b5d0a
3e23e62
de00fc5
bd17c41
3a34ba8
baa9b4d
5078fe3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,6 @@ | |
#include "drivers/time.h" | ||
|
||
#define UNIX_REFERENCE_YEAR 1970 | ||
#define UNIX_REFERENCE_DOW 3 | ||
#define MILLIS_PER_SECOND 1000 | ||
|
||
// rtcTime_t when the system was started. | ||
|
@@ -53,6 +52,7 @@ PG_REGISTER_WITH_RESET_TEMPLATE(timeConfig_t, timeConfig, PG_TIME_CONFIG, 1); | |
PG_RESET_TEMPLATE(timeConfig_t, timeConfig, | ||
.tz_offset = 0, | ||
.tz_automatic_dst = false, | ||
.tz_dst_country = DST_EU, | ||
); | ||
|
||
static rtcTime_t dateTimeToRtcTime(dateTime_t *dt) | ||
|
@@ -122,15 +122,69 @@ static bool rtcIsDateTimeValid(dateTime_t *dateTime) | |
(dateTime->millis <= 999); | ||
} | ||
|
||
static int lastSundayOfMonth(int currentYear, int wantedMonth) { | ||
int days[] = {31,29,31,30,31,30,31,31,30,31,30,31}; | ||
int m, y = currentYear, w; | ||
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 line seems very hairy. |
||
days[1] -= (y % 4) || (!(y % 100) && (y % 400)); | ||
w = y * 365 + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + 6; | ||
|
||
for(m = 0; m < 12; m++) { | ||
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. Space between |
||
w = (w + days[m]) % 7; | ||
if (m == wantedMonth - 1) { | ||
return days[m] + (w < 5 ? -2 : 5) - w; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
static int nthSundayOfMonth(int lastSunday, int nth) { | ||
while (lastSunday > 7 * nth) { | ||
lastSunday -= 7; | ||
} | ||
return lastSunday; | ||
} | ||
|
||
static bool isDST(rtcTime_t t) { | ||
dateTime_t dateTime; | ||
rtcTimeToDateTime(&dateTime, t); | ||
if (dateTime.month < 3 || dateTime.month > 11) { return false; } | ||
if (dateTime.month > 3 && dateTime.month < 11) { return true; } | ||
uint8_t dow = (uint8_t) (((t / MILLIS_PER_SECOND) / 86400) + UNIX_REFERENCE_DOW) % 7; | ||
int previousSunday = dateTime.day - dow; | ||
if (dateTime.month == 3) { return previousSunday >= 8; } | ||
return previousSunday <= 0; | ||
int lastSunday; | ||
switch (timeConfig()->tz_dst_country) { | ||
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. Once you make
|
||
case DST_USA: // begins at 2:00 a.m. on the second Sunday of March and ends at 2:00 a.m. on the first Sunday of November | ||
if (dateTime.month < 3 || dateTime.month > 11) { return false; } | ||
if (dateTime.month > 3 && dateTime.month < 11) { return true; } | ||
lastSunday = lastSundayOfMonth(dateTime.year, dateTime.month); | ||
if (dateTime.month == 3) { | ||
int secondSunday = nthSundayOfMonth(lastSunday, 2); | ||
if (dateTime.day == secondSunday) { | ||
return dateTime.hours >= 2; | ||
} | ||
return dateTime.day > secondSunday; | ||
} | ||
if (dateTime.month == 11) { | ||
int firstSunday = nthSundayOfMonth(lastSunday, 1); | ||
if (dateTime.day == firstSunday) { | ||
return dateTime.hours < 2; | ||
} | ||
return dateTime.day < firstSunday; | ||
} | ||
break; | ||
case DST_EU: // begins at 1:00 a.m. on the last Sunday of March and ends at 1:00 a.m. on the last Sunday of October | ||
if (dateTime.month < 3 || dateTime.month > 10) { return false; } | ||
if (dateTime.month > 3 && dateTime.month < 10) { return true; } | ||
lastSunday = lastSundayOfMonth(dateTime.year, dateTime.month); | ||
if (dateTime.day < lastSunday) { return !(dateTime.month == 3); } | ||
if (dateTime.day > lastSunday) { return !(dateTime.month == 3); } | ||
if (dateTime.day == lastSunday) { | ||
if (dateTime.month == 3) { | ||
return dateTime.hours >= 1; | ||
} | ||
if (dateTime.month == 10) { | ||
return dateTime.hours < 1; | ||
} | ||
} | ||
break; | ||
} | ||
return false; | ||
} | ||
|
||
static void dateTimeWithOffset(dateTime_t *dateTimeOffset, dateTime_t *dateTimeInitial, int16_t minutes, bool automatic_dst) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,9 +39,15 @@ typedef uint32_t timeUs_t; | |
|
||
static inline timeDelta_t cmpTimeUs(timeUs_t a, timeUs_t b) { return (timeDelta_t)(a - b); } | ||
|
||
typedef enum { | ||
DST_EU, | ||
DST_USA, | ||
} tz_dst_country_e; | ||
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. Name this |
||
|
||
typedef struct timeConfig_s { | ||
int16_t tz_offset; // Offset from UTC in minutes, might be positive or negative | ||
bool tz_automatic_dst; // Automatically handle DST or ignore it | ||
tz_dst_country_e tz_dst_country; | ||
} timeConfig_t; | ||
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. Rather than 2 variables, use just one uint8_t named |
||
|
||
PG_DECLARE(timeConfig_t, timeConfig); | ||
|
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.
Indentation and formatting need some work in all this function. Opening brace for functions should be in the next line. Indent using 4 spaces and add a space before and after
,
as well as arithmetic operators.