Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created November 25, 2015 02:27
Show Gist options
  • Select an option

  • Save chrisguitarguy/66e08fdc84979a3ce324 to your computer and use it in GitHub Desktop.

Select an option

Save chrisguitarguy/66e08fdc84979a3ce324 to your computer and use it in GitHub Desktop.
(require '[clojure.string :as string])
(defn- str->int [s]
(Integer/parseInt s))
(defn- parse-time [timestr]
(let [[hours mins secs-and-ampm] (string/split timestr #":" 3)
secs (subs secs-and-ampm 0 2)
ampm (subs secs-and-ampm 2)]
{:hours (str->int hours)
:minutes (str->int mins)
:seconds (str->int secs)
:ampm (string/lower-case ampm)}))
(defn- am? [tm]
(= "am" (:ampm tm)))
(defn- pm? [tm]
(= "pm" (:ampm tm)))
(defn- format-time [tm]
(format "%02d:%02d:%02d" (:hours tm) (:minutes tm) (:seconds tm)))
(defn print-time [tm]
(println (format-time tm)))
(defn -main [& args]
(let [tm (parse-time (read-line))
hours (:hours tm)]
(cond
(and (am? tm) (= 12 hours)) (print-time (assoc tm :hours 0))
(and (pm? tm) (< hours 12)) (print-time (assoc tm :hours (+ hours 12)))
:else (print-time tm))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment