PlatformIO Community

Time function error


#1

There is a function

void updateTime()
{
long curEpoch = localEpoc + ((millis() - localMillisAtUpdate) / 1000);
long epoch = round(curEpoch + 3600 * utcOffset + 86400L) % 86400L;
h = ((epoch % 86400L) / 3600) % 24;
m = (epoch % 3600) / 60;
s = epoch % 60;
}

Board - esp32doit-devkit-v1

such a mistake: invalid operands of types ‘double’ and ‘long int’ to binary ‘operator%’
On other platforms it works.


#2

Will return float according to http://www.cplusplus.com/reference/cmath/round/. So you need to cast that expression to a long.


#3

Yes, indeed I did:
long epoch = (long)(round(curEpoch + 3600 * utcOffset + 86400L)) % 86400L;
But for example in Arduino IDE - there was no such problem. Why?


#4

After commit https://github.com/espressif/arduino-esp32/commit/1e4bf14a3eef4894f65783b4940d98777d5e2263 this code should fail in the Arduino IDE, too. Before that, round was the macro which already did (long) conversion (#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))).