For the time conversion, here is something which I find simpler:
when this transformation is applied on the following xml document:
the wanted, correct result is produced:
For am/pm (if my understanding for the edge cases is correct) we add this code:
The complete transformation becomes:
am
pm
and when applied to the same xml document (above) the result is: