I looked through this thread, and was amazed that nobody implemented a correct, lossless implementation. Generally, when you write a 12-hour time, you should include "AM" or "PM", because otherwise you loose information. Thus, I'm adding my solution:
sub to12h {
local $_=shift;
return (12, "PM") if $_==0;
return ($_, "AM") if $_<=12;
return ($_-12, "PM")
}
for (0..23) {
print join " ", to12h($_), "\n";
}
Of course, if you /want/ to loose information, just use the first element of the return value.
Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).