Skip to content

Commit d5a676a

Browse files
committed
Return a valid localtime only if timezone is configured and initialize private varibles in constructor
1 parent 4bbe087 commit d5a676a

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/utility/time/TimeService.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ time_t cvt_time(char const * time);
4444
**************************************************************************************/
4545

4646
static time_t const EPOCH_AT_COMPILE_TIME = cvt_time(__DATE__);
47+
static time_t const EPOCH = 0;
4748

4849
/**************************************************************************************
4950
* CTOR/DTOR
@@ -54,6 +55,9 @@ TimeService::TimeService()
5455
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
5556
, _is_rtc_configured(false)
5657
#endif
58+
, _is_tz_configured(false)
59+
, _timezone_offset(0)
60+
, _timezone_dst_until(0)
5761
{
5862

5963
}
@@ -100,12 +104,18 @@ void TimeService::setTimeZoneData(long offset, unsigned long dst_until)
100104
if(_timezone_dst_until != dst_until)
101105
DEBUG_DEBUG("ArduinoIoTCloudTCP::%s tz_dst_unitl: [%ul]", __FUNCTION__, dst_until);
102106
_timezone_dst_until = dst_until;
107+
108+
_is_tz_configured = true;
103109
}
104110

105111
unsigned long TimeService::getLocalTime()
106112
{
107113
unsigned long utc = getTime();
108-
return utc + _timezone_offset;
114+
if(_is_tz_configured) {
115+
return utc + _timezone_offset;
116+
} else {
117+
return EPOCH;
118+
}
109119
}
110120

111121
unsigned long TimeService::getTimeFromString(const String& input)

src/utility/time/TimeService.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ class TimeService
5959
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
6060
bool _is_rtc_configured;
6161
#endif
62+
bool _is_tz_configured;
63+
long _timezone_offset;
64+
unsigned long _timezone_dst_until;
6265

6366
unsigned long getRemoteTime();
64-
6567
static bool isTimeValid(unsigned long const time);
6668

67-
private:
68-
long _timezone_offset;
69-
unsigned long _timezone_dst_until;
70-
7169
};
7270

7371
TimeService* ArduinoIoTCloudTimeService();

0 commit comments

Comments
 (0)