I'm writing a program on a raspberry pi, the millis function has been working correctly for days, today it started giving negative value, how can I fix it?
Apparently he tries to convert the timeval struct into a scalar milliseconds value.
...because tv_sec (seconds) is multiplied by 1000 and tv_usec (microseconds) is divided by 1000.
Even if time is counted in milliseconds (from Epoch), then a signedint64_t should be way large enough to not "wrap around" into negative area – anytime soon. So I suspect something with the computation goes wrong.
Provided that tmillis_t is really an alias for int64_t (why not use just int64_t?), try this conversion: return (((tmillis_t)tp.tv_sec) * 1000LL) + (((tmillis_t)tp.tv_usec) / 1000LL);