Skip to content

Commit

Permalink
Add onCheckTimeZoneOffset() method to SystemClock
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 authored and mikee47 committed Jun 19, 2024
1 parent 6a219c2 commit 3c84c9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Sming/Core/SystemClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ time_t SystemClockClass::now(TimeZone timeType) const
uint32_t systemTime = RTC.getRtcSeconds();

if(timeType == eTZ_Local) {
if(checkTimeZoneOffset) {
checkTimeZoneOffset(systemTime);
}
systemTime += zoneInfo.offsetSecs();
}

Expand All @@ -41,7 +44,10 @@ bool SystemClockClass::setTime(time_t time, TimeZone timeType)

String SystemClockClass::getSystemTimeString(TimeZone timeType) const
{
time_t systemTime = RTC.getRtcSeconds();
time_t systemTime = now(eTZ_UTC);
if(checkTimeZoneOffset) {
checkTimeZoneOffset(systemTime);
}

if(timeType == eTZ_UTC) {
DateTime dt(systemTime);
Expand Down
18 changes: 18 additions & 0 deletions Sming/Core/SystemClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ enum TimeZone {
class SystemClockClass
{
public:
/**
* @brief Optional callback which can be used to automate handling of timezone changes
* @param systemTime The current system time
*
* Although the timezone offset only changes twice a year, when it does change the
* corresponding offset should be updated promptly.
*
* `setTimeZoneOffset()` is typically called when updating via real-time clock chip or NTP,
* but this can still leave a considerable gap during which the local time will be wrong.
*
* The callback should check the provided systemTime and determine if the time zone offset
* requires changing and, if so, call `setTimeZoneOffset()`.
* One way to make this efficient is to cache the `time_t` value for the *next* change,
* rather than attemtpting to compute the offset on every call.
*/
using CheckTimeZoneOffset = Delegate<void(time_t systemTime)>;

/** @brief Get the current date and time
* @param timeType Time zone to use (UTC / local)
* @retval DateTime Current date and time
Expand Down Expand Up @@ -118,6 +135,7 @@ class SystemClockClass
}

private:
CheckTimeZoneOffset checkTimeZoneOffset;
DateTime::ZoneInfo zoneInfo;
bool timeSet = false;
};
Expand Down

0 comments on commit 3c84c9f

Please sign in to comment.