-
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,6 +33,7 @@ | |
#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. | ||
|
@@ -51,6 +52,7 @@ PG_REGISTER_WITH_RESET_TEMPLATE(timeConfig_t, timeConfig, PG_TIME_CONFIG, 0); | |
|
||
PG_RESET_TEMPLATE(timeConfig_t, timeConfig, | ||
.tz_offset = 0, | ||
.tz_automatic_dst = false, | ||
); | ||
|
||
static rtcTime_t dateTimeToRtcTime(dateTime_t *dt) | ||
|
@@ -120,14 +122,26 @@ static bool rtcIsDateTimeValid(dateTime_t *dateTime) | |
(dateTime->millis <= 999); | ||
} | ||
|
||
static void dateTimeWithOffset(dateTime_t *dateTimeOffset, dateTime_t *dateTimeInitial, int16_t minutes) | ||
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; | ||
} | ||
|
||
static void dateTimeWithOffset(dateTime_t *dateTimeOffset, dateTime_t *dateTimeInitial, int16_t minutes, bool automatic_dst) | ||
{ | ||
rtcTime_t initialTime = dateTimeToRtcTime(dateTimeInitial); | ||
rtcTime_t offsetTime = rtcTimeMake(rtcTimeGetSeconds(&initialTime) + minutes * 60, rtcTimeGetMillis(&initialTime)); | ||
if (automatic_dst && isDST(offsetTime)) { offsetTime += 3600; } | ||
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. Please, align the
(this applies to more places in the diff, but no reason to repeat the same comment in all of them) |
||
rtcTimeToDateTime(dateTimeOffset, offsetTime); | ||
} | ||
|
||
static bool dateTimeFormat(char *buf, dateTime_t *dateTime, int16_t offset) | ||
static bool dateTimeFormat(char *buf, dateTime_t *dateTime, int16_t offset, bool automatic_dst) | ||
{ | ||
dateTime_t local; | ||
|
||
|
@@ -139,7 +153,7 @@ static bool dateTimeFormat(char *buf, dateTime_t *dateTime, int16_t offset) | |
if (offset != 0) { | ||
tz_hours = offset / 60; | ||
tz_minutes = ABS(offset % 60); | ||
dateTimeWithOffset(&local, dateTime, offset); | ||
dateTimeWithOffset(&local, dateTime, offset, automatic_dst); | ||
dateTime = &local; | ||
} | ||
|
||
|
@@ -176,17 +190,17 @@ uint16_t rtcTimeGetMillis(rtcTime_t *t) | |
|
||
bool dateTimeFormatUTC(char *buf, dateTime_t *dt) | ||
{ | ||
return dateTimeFormat(buf, dt, 0); | ||
return dateTimeFormat(buf, dt, 0, false); | ||
} | ||
|
||
bool dateTimeFormatLocal(char *buf, dateTime_t *dt) | ||
{ | ||
return dateTimeFormat(buf, dt, timeConfig()->tz_offset); | ||
return dateTimeFormat(buf, dt, timeConfig()->tz_offset, timeConfig()->tz_automatic_dst); | ||
} | ||
|
||
void dateTimeUTCToLocal(dateTime_t *utcDateTime, dateTime_t *localDateTime) | ||
{ | ||
dateTimeWithOffset(localDateTime, utcDateTime, timeConfig()->tz_offset); | ||
dateTimeWithOffset(localDateTime, utcDateTime, timeConfig()->tz_offset, timeConfig()->tz_automatic_dst); | ||
} | ||
|
||
bool dateTimeSplitFormatted(char *formatted, char **date, char **time) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ static inline timeDelta_t cmpTimeUs(timeUs_t a, timeUs_t b) { return (timeDelta_ | |
|
||
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 | ||
} 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.
You're adding fields to the parameter group. Please also increase version number in the declaration
PG_REGISTER_WITH_RESET_TEMPLATE
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.
Rookie mistake, my bad.
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.
No worries, I keep forgetting to do this myself 😉
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.
Is there any more work left to be done on this? Sorry to be bothering, it seems that I cannot manually mark this request as solved.