0

I have a clock time in form of HH.MM.SS (hours, minutes, sec), I need to create a way to convert it to one number so that I can determent which time is bigger. for example 00.00.00 is the smallest 23.59.59 is the biggest

11.03.50 > 11.02.57 etc..

I thought to do the following

hours*10000+minutes*100+sec but im not sure it works for all cases... how can i prove it works?

Jack M
  • 27,819
  • 7
  • 63
  • 129

2 Answers2

4

Just convert the total time to seconds. The most seconds is clearly the "largest" time.

$$ T=3600h+60m+s $$

Rocket Man
  • 2,433
1

If all you need to do is compare two times in HH:MM:SS format, say $(h,m,s)$ and $(h',m',s')$, then you do not need to convert to seconds. You can just use the lexicographical ordering:

\begin{align*} (h,m,s) <_{\text{lex}} (h',m',s') \iff &h < h', \text{ or } \\ &h = h' \mathbin{\And} m < m', \text{ or } \\ &h = h' \mathbin{\And} m = m' \mathbin{\And} s < s'. \end{align*}

This might be more efficient that converting to seconds if each triple $(h,m,s)$ is only involved in one comparison.

Trevor Wilson
  • 16,989