My Solution to the Changing Times Challenge

Last week, I posted an EmacsGolf challenge on changing times to 24-hour notation. Most of the solutions were much like mine: they used query-replace-regexp with a complicated regular expression and an even more complex replacement expression. One trick that most responders missed was using \#n to get the nth subexpression as a number. That avoids needing to call string-to-int or something similar.

That said, my solution was to call query-replace-regexp with

\([0-9][0-9]?!\):\([0-9][0-9]!\)\([ap]!\)

as the regular expression to search for and

\,(format "%02d:%s" (if (equal \3 "p") (+ \#1 12) \#1) \2)

as the replacement expression.

David Ongaro had a really great (and surprising) solution leveraging the date handling functions of Emacs Calc. It’s nice because it avoids the horrendous regular expression that the rest of us used and simplified the replacement. Very clever.

This entry was posted in General and tagged . Bookmark the permalink.