# Screw this - algorithm to find the difference in seconds between two 24hr times

I just cannot come up with a simple algorithm to find the difference in seconds between two 24 hour times.

The roll over at midnight is an impossible barrier to avoid an algorithm that does not involve a massive list if if statements covering all the possibilities.

Even converting the two times to seconds does not solve the problem - the roll over at midnight still gets in the way.

Does anyone know how to do it?

Keep track of the date too?

Convert to seconds, subtract start from end. If it's negative, that means it rolled over, so add 24 hrs worth of seconds to it.

DrAzzy:
Convert to seconds, subtract start from end. If it's negative, that means it rolled over, so add 24 hrs worth of seconds to it.

For example
Early Time A = 22:40:10
Later Time B = 01:45:20

The difference is 3:05:10

OR (3 x 3600) + (5 x 60) + 10 = 11110 Seconds

Time A = (22 x 3600) + (40 x 60) + 10 = 81610 Seconds
Time B = (1 x 3600) + (45 x 60) + 20 = 6320 Seconds

Difference B - A = 6320 - 81610 = -75290 Seconds

Its a negative value

So
Difference = -75290 + (24 * 3600) = -75290 + 86400 = 11110 Seconds

Tom..... Thanks folks - just couldn't get my head around the seconds thing.

be aware that the type for the total_seconds should be an int32_t (long) or uint32_t (unsigned long)

als a day has 86400 seconds and that does not fit in a 16 bit int

Can it happen that First and second measurements are on different days?

e.g. first = 23:50:00
second = 00:10:00

(diff should be 20 minutes)

robtillaart:
Can it happen that First and second measurements are on different days?

e.g. first = 23:50:00
second = 00:10:00

(diff should be 20 minutes)

See my example, 2 different days.
Tom.. OOPS missed that one :-[

boylesg:
I just cannot come up with a simple algorithm to find the difference in seconds between two 24 hour times.

The roll over at midnight is an impossible barrier to avoid an algorithm that does not involve a massive list if if statements covering all the possibilities.

Even converting the two times to seconds does not solve the problem - the roll over at midnight still gets in the way.

Does anyone know how to do it?

@boylesg, this is probably the reason the computing world uses timestamps and not clock face time... it is then simple subtraction.

take a look at my DailyTimer library, which makes extensive use of your problem using a method to convert any time to a timestamp:

``````time_t DailyTimer::tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss)
{
tmElements_t tmSet;
tmSet.Year = YYYY - 1970;
tmSet.Month = MM;
tmSet.Day = DD;
tmSet.Hour = hh;
tmSet.Minute = mm;
tmSet.Second = ss;
return makeTime(tmSet);
}
``````

robtillaart:
be aware that the type for the total_seconds should be an int32_t (long) or uint32_t (unsigned long)

als a day has 86400 seconds and that does not fit in a 16 bit int

Can it happen that First and second measurements are on different days?

e.g. first = 23:50:00
second = 00:10:00

(diff should be 20 minutes)

The assumption, for the time being, is that the first time is always less than the second time regardless of midnight roll over.