The conversion in getrtc simply adds 12 if PM is set.
However, in 12h mode, midnight is represented as hour=12 pm=0, which is converted to 24h mode as 12.
Likewise, noon is represented as hour=12, pm=1, which is converted to 24.
So, a special case is needed if hour==12: Set hour to 0 if pm==0, otherwise leave unchanged.
setrtc has the same problem in reverse.