It’s interesting how the experienced programmers at StackExchange see this problem as a simple overflow problem.
An example of overflow is when an odometer reaches it’s maximum and resets to zero. For a typical clock the highest number it can reach is 12 so the next increment after 12 is when the overflow condition is created.
If the switch between am/pm is intended to indicate an overflow condition exists than that doesn’t happen till after midnight or after noontime.
The other issue is that for the most part the ancients had no concept or symbol for zero.
Once the switch is made to a zero-based 24-hr system those problems are solved.