Time library and millis() rollover


I am new to arduino and to C like programming language so if it’s a stupid question, sorry!!

Based on the code below I would conclude that after a rollover of millis() it is always lower that prevMillis and the nextSynctime will always be lower than the systime. My conclusion is that systime would stay the same am I correct?

time_t now(){
  while( millis() - prevMillis >= 1000){      
    prevMillis += 1000;	
    sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift     
  if(nextSyncTime <= sysTime){
	if(getTimePtr != 0){
	  time_t t = getTimePtr();
      if( t != 0)
        Status = (Status == timeNotSet) ?  timeNotSet : timeNeedsSync;        
  return sysTime;

I would excpect something like:

If (millis() < prevMillis) {
prevMillis = 0;
nextSyncTime = sysTime; 

This would invoke an update e.g. from NTP.



while( millis() - prevMillis >= 1000){

if prevMillis is declared a unsigned long this code works because the result will underflow and become bigger than 1000 again if a rollover occurs.

Time library : http://arduino.cc/playground/Code/Time

If you still want to handle potential rollovers for the millis() and micros() functions, but don’t need the other functions of the Time library, there’s also TimingRollover. It’s more of a snippet of code than a library and therefore will take-up less space on the IC chip.

Edit: The linked code is a similar idea to what pylon posted, but it uses an If statement and there’s more explanation for the math involved.

Thanks for the clarification!