Commits

Miki Tebeka  committed 737ef65

Starting

  • Participants

Comments (0)

Files changed (6)

+syntax: glob
+
+target
+README.html

File .lein-failures

+#{}
+strftime
+========
+
+`strftime`_ implementation for Clojure
+
+.. _`strftime`: http://docs.python.org/2/library/time.html#time.strftime
+
+Usage
+=====
+::
+
+    (use 'strftime)
+    (strftime "%Y/%m/%d" (Date.)) ; "2012/12/19"
+
+Contact
+=======
+https://bitbucket.org/tebeka/clj-strftime
+(defproject strftime "0.1.0-SNAPSHOT"
+  :description "strftime implementation (see http://bit.ly/strftime-py"
+  :url "https://bitbucket.org/tebeka/clj-strftime"
+  :license {:name "Eclipse Public License"
+            :url "http://www.eclipse.org/legal/epl-v10.html"}
+  :dependencies [[org.clojure/clojure "1.4.0"]])

File src/strftime.clj

+(ns strftime
+  #^{ :author "Miki Tebeka <miki.tebeka@gmail.com>"
+      :doc "strftime for Clojure"}
+  (:import java.util.Date
+           java.util.Calendar))
+
+(defmulti strftime 
+  "Format time t according to format string fmt."
+  (fn [fmt t] (class t)))
+
+(defmethod strftime Date
+  [fmt t]
+  (let [fmt (.replaceAll fmt "%([a-zA-Z])" "%1\\$t$1")]
+    (format fmt t)))
+
+(defmethod strftime Long
+  [fmt t]
+  (strftime fmt (Date. t)))
+
+(defmethod strftime Calendar
+  [fmt t]
+  (strftime fmt (.getTime t)))

File test/strftime_test.clj

+(ns strftime-test
+  (:use clojure.test
+        strftime)
+  (:import java.util.Date
+           java.util.Calendar))
+
+; Months are 0 based, go figure ...
+(def c (doto (Calendar/getInstance) (.set 2012 10 10 9 8 0)))
+(def result "2012/11/10 09:08")
+(def fmt "%Y/%m/%d %H:%M")
+
+(deftest calendar-test
+  (testing "Calendar"
+    (is (= (strftime fmt c) result))))
+
+(deftest date-test
+  (testing "Calendar"
+    (is (= (strftime fmt (.getTime c)) result))))
+
+(deftest date-long
+  (testing "long"
+    (is (= (strftime fmt (.getTimeInMillis c)) result))))