Convert h,m,s to seconds and subtract. Handle the special case when future is past midnight. In that case, add the differences of each time from midnight.
// Helper function
uint32_t hms_to_seconds(int hour, int minutes, int seconds)
{
return hour * 3600 + minutes * 60 + seconds;
}
uint32_t GetSeconds(int hour_now, int minutes_now, int seconds_now,
int hour_future, int minutes_future, int seconds_future)
{
uint32_t now = hms_to_seconds(hour_now, minutes_now, seconds_now);
uint32_t future = hms_to_seconds(hour_future, minutes_future, seconds_future;
// Future is not past midnight
if (now < future) {
return future - now;
}
// Future is past midnight
else {
uint32_t midnight = hms_to_seconds(24, 0 , 0);
return (midnight - now) + future;
}
}
If the future time is more than 24 hours out, this function won't work and would require a date parameter.