It's the first time I use 64-bit time_t
structure to do operations with time. The compiler is the TI's C Compiler for CC3220SF.
Here my function that should return the seconds to the next event:
#define ACQ_INTERVAL 1 // in minutes
time_t _lastAcquisition;
time_t _timeToAcquire(void)
{
time_t now = _getEpoch(); // from internal RTC
if (_lastAcquisition == 0) _lastAcquisition = now;
time_t diff = now - _lastAcquisition; // seconds since last acquisition
time_t next = ACQ_INTERVAL * 60;
UART_PRINT("Interval is %lld s\r\n", next);
UART_PRINT("Current epoch is %lld, last acquisition was %lld, next one in %lld s\r\n", now, _lastAcquisition, next - diff);
return next - diff; // seconds to next acquisition
}
UART_PRINT
is just a wrapper for printf.
Here the _getEpoch()
function:
time_t _getEpoch(void)
{
_i16 ret;
_u8 pConfigOpt = SL_DEVICE_GENERAL_DATE_TIME;
_u16 pConfigLen = sizeof(SlDateTime_t);
SlDateTime_t dateTime = {0};
ret = sl_DeviceGet(SL_DEVICE_GENERAL, &pConfigOpt, &pConfigLen, (unsigned char *) &dateTime);
ASSERT_ON_ERROR(ret);
struct tm t;
time_t t_of_day;
t.tm_year = dateTime.tm_year - 1900;
t.tm_mon = dateTime.tm_mon - 1;
t.tm_mday = dateTime.tm_day;
t.tm_hour = dateTime.tm_hour;
t.tm_min = dateTime.tm_min;
t.tm_sec = dateTime.tm_sec;
t.tm_isdst = -1;
t_of_day = mktime(&t);
return t_of_day;
}
Usually it works fine, but sometimes I get something weird like this output:
Interval is 60 s
Current epoch is 1511088032, last acquisition was 1511086500, next one in 18446744073709550144 s
Obviously there's something wrong. The difference should be 1532. Furthermore the huge number seems something like 2^64 - x. But x would be 1742... and I don't understand where it might come from.
I double checked the definition of time_t
:
typedef long long __time64_t;
#if defined(_TARGET_DEFAULTS_TO_TIME64) || (__TI_TIME_USES_64) && __TI_TIME_USES_64)
typedef __time64_t time_t;
#else
typedef __time32_t time_t;
#endif
the __TI_TIME_USES_64
is defined and set to 1 (the IDE shows me the first section of #if
is the one enabled).