Skip to content

Commit 3c06977

Browse files
committed
Check Timezone offset boundaries
1 parent fa22ff8 commit 3c06977

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/utility/time/TimeService.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ TimeServiceClass::TimeServiceClass()
103103
: _con_hdl(nullptr)
104104
, _is_rtc_configured(false)
105105
, _is_tz_configured(false)
106-
, _timezone_offset(0)
106+
, _timezone_offset(24 * 60 * 60)
107107
, _timezone_dst_until(0)
108108
, _last_sync_tick(0)
109109
, _sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms)
@@ -184,11 +184,13 @@ void TimeServiceClass::setSyncFunction(syncTimeFunctionPtr sync_func)
184184

185185
void TimeServiceClass::setTimeZoneData(long offset, unsigned long dst_until)
186186
{
187-
if(_timezone_offset != offset || _timezone_dst_until != dst_until) {
188-
DEBUG_DEBUG("TimeServiceClass::%s offset: %d dst_unitl %ul", __FUNCTION__, offset, dst_until);
189-
_timezone_offset = offset;
190-
_timezone_dst_until = dst_until;
191-
_is_tz_configured = true;
187+
if(isTimeZoneOffsetValid(offset) && isTimeValid(dst_until)) {
188+
if(_timezone_offset != offset || _timezone_dst_until != dst_until) {
189+
DEBUG_DEBUG("TimeServiceClass::%s offset: %d dst_unitl %u", __FUNCTION__, offset, dst_until);
190+
_timezone_offset = offset;
191+
_timezone_dst_until = dst_until;
192+
_is_tz_configured = true;
193+
}
192194
}
193195
}
194196

@@ -312,6 +314,12 @@ bool TimeServiceClass::isTimeValid(unsigned long const time)
312314
return (time > EPOCH_AT_COMPILE_TIME);
313315
}
314316

317+
bool TimeServiceClass::isTimeZoneOffsetValid(long const offset)
318+
{
319+
/* UTC offset can go from +14 to -12 hours */
320+
return ((offset < (14 * 60 * 60)) && (offset > (-12 * 60 * 60)));
321+
}
322+
315323
void TimeServiceClass::initRTC()
316324
{
317325
#if defined (ARDUINO_ARCH_SAMD)

src/utility/time/TimeService.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class TimeServiceClass
7474
void initRTC();
7575
void setRTC(unsigned long time);
7676
unsigned long getRTC();
77+
static bool isTimeValid(unsigned long const time);
78+
static bool isTimeZoneOffsetValid(long const offset);
7779

7880
};
7981

0 commit comments

Comments
 (0)