I'm developing a arduino based system, which includes a alarm system.

Searching on the Internet, I found these lybraries "Time.h" and "TimeAlarms.h".

In the requirements, it says: "Time does not require any special hardware. Internally, Time depends upon Arduino's millis() function to keep track to elasped time."

Considering Arduino's millis() rollover after 49.7 days, how can I ensure these lybraries are handling the rollover well?

I don't think that waiting 49.7 days to test it is the better way.


As long as the date is before 2038, and your code calls one of the time functions at least once every 49 days, rollover won't affect you.

Internally the library uses Unix epoch time. Every time you call a function it checks to see if 1 or more seconds have past since the last time it was called and then updates the epoch time accordingly. Only for this check it uses millis(). And in only checks the difference between the last call's millis-value and the current millis-value, and rollover has no effect on time differences (as long as they are less that 49 days).


You should simply buy a real time clock such as the common DS3231. They work well and don't have any problems with resting or continuous data reading. It also dictates the date!

Hope I helped :)


I have a lengthy post about this.

To avoid going to the link if you don't want to, the simple answer is to subtract when calculating time intervals, eg.

unsigned long startTime = millis ();
unsigned long interval = 60000;


if (millis () - startTime >= interval)
  // do something

This always works! - even after 49.7 days - because of the way that unsigned arithmetic works.

The only time it would fail is if you needed to time an interval of over 49.7 days - in which case a real-time clock is an obvious answer.

Another example:

startedFeedingFish = millis ();


if (millis () - startedFeedingFish >= 20000)  // feed them for 20 seconds
  // stop feeding the fish

Not the answer you're looking for? Browse other questions tagged or ask your own question.