Time function error

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.

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

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?

After commit cores: replace max, min, round macros with imports from std (#1783) · espressif/arduino-esp32@1e4bf14 · GitHub 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))).

1 Like