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.
0.01 * m + h
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?