I'm trying to use an if statement to check when the time goes past a specific point and run a specific function. To do this I'm looking to convert time into a double and running something along the lines of if(timeCurrent >= 8.30). I seem to however have an issue with the conversion part, normally I'm aware the standard practice in C++ would be to do the following:

DateTime now = rtc.now();
static char buffer[80];

sprintf(buffer, "%02d.%02d", now.hour(), now.minute());
String str(buffer);
double timeCurrent = strtod(str);

However, this doesn't work for Arduino. What can be done to create the same desired function?

  • this may help ... you made no mention of any research that you may have done ... playground.arduino.cc/Code/Time
    – jsotola
    May 24, 2018 at 22:35
  • convert buffer to float, not String. but there are more elegant end efficient solutions. if you really want float then 0.01 * m + h
    – Juraj
    May 25, 2018 at 3:51
  • There is really no need to take the extra effort in converting first to char buffer, then String and then double. Just convert the ints from the RTC directly to a float like juraj wrote. And I doubt, that your code is a good practice in C++ either
    – chrisl
    May 25, 2018 at 7:26
  • I'm aware the standard practice in C++ would be to do the following... Umm... no, it certainly would not. What would give you that impression?
    – Majenko
    May 25, 2018 at 9:35

1 Answer 1


Don't do that. Converting a time to double is highly inefficient. Moreover, your approach suffers from a rollover issue every 24h.

If you just want to compare the time of day with 8:30, you can do it in a very straightforward way:

if (now.hour() > 8 || (now.hour() == 8 && now.minute() >= 30))

If the long if annoys you, you can put the comparison logic inside a static inline function. If you really want to combine hour and minute into a single number, then convert to an integer rather than a double:

if (now.hour() * 100 + now.minute() >= 830)

This can be made more efficient if you think in binary rather than decimal: you can build a 16-bit number with one byte holding the hours and the other byte holding the minutes:

if ((now.hour() << 8) | now.minute() >= (8 << 8) | 30)

Again, you may encapsulate the ((a)<<8|(b)) in a macro or static inline function if it helps with readability. If you want to compare to an absolute time, your best option is to build a DateTime object representing the deadline, and compare with now as:

// Assume timeout should be today at 8:30.
DateTime timeout(now.year(), now.month(), now.day(), 8, 30, 0);
if (now.secondstime() >= timeout.secondstime())

Here the secondstime() method is used to convert the DateTime into a scalar number (a 32-bit integer of type time_t) that can be easily compared and is far more efficient than using floats.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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