Skip to content

Commit

Permalink
Added time types (1 test pending)
Browse files Browse the repository at this point in the history
  • Loading branch information
cblop committed Dec 20, 2017
1 parent b69230f commit 05a39f3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/tableschema_clj/types/datetime.clj
@@ -1,12 +1,9 @@
(ns tableschema-clj.types.datetime
(:require [clojure.spec.alpha :as s]
[tableschema-clj.types.date :refer [date-formatter]]
[tableschema-clj.types.time :refer [time-formatter]]
[java-time :refer [local-date-time formatter]]))

(defn time-formatter [fmt]
(-> fmt
(clojure.string/replace #"%H" "HH")
(clojure.string/replace #"%M" "mm")))

(defn datetime-formatter [fmt]
(try
Expand Down
42 changes: 42 additions & 0 deletions src/tableschema_clj/types/time.clj
@@ -0,0 +1,42 @@
(ns tableschema-clj.types.time
(:require [clojure.spec.alpha :as s]
[java-time :refer [local-time]]))

(def iso-time-regex #"(0[1-9]|1[012]):([0-5][0-9]):([0-5][0-9])")

(defn iso-time-to-time [iso-time]
(local-time iso-time))

(defn parse-time-string [time]
(try
(local-time time)
(catch Exception e false)))

(defn time-formatter [fmt]
(try
(-> fmt
(clojure.string/replace #"%H" "HH")
(clojure.string/replace #"%M" "mm"))
(catch Exception e false)))

(defn make-time [fmt time]
(try
(if (s/valid? ::time-type time) time
(local-time (time-formatter fmt) time))
(catch Exception e false)))

(s/def ::time-formatter-pair (fn [{:keys [format value]}] (make-time format value)))

(s/def ::time-type #(= (type %) java.time.LocalTime))
(s/def ::time-string #(parse-time-string %))
(s/def ::iso-time-string (s/and string? #(re-matches iso-time-regex %)))

(defn cast-time [format value]
(case format
"default" (cond (s/valid? ::time-type value) value
(s/valid? ::iso-time-string value) (iso-time-to-time value)
:else ::s/invalid)
"any" (cond (s/valid? ::time-type value) value
(s/valid? ::time-string value) (parse-time-string value)
:else ::s/invalid)
(if (s/valid? ::time-formatter-pair {:format format :value value}) (make-time format value) ::s/invalid)))
32 changes: 32 additions & 0 deletions test/tableschema_clj/types/time_test.clj
@@ -0,0 +1,32 @@
(ns tableschema-clj.types.time-test
(:require [tableschema-clj.types.time :refer :all]
[java-time :refer [local-time]]
[clojure.test :refer :all]))

(defonce INVALID :clojure.spec.alpha/invalid)

(deftest test-cast-time
(are [format value result] (= (cast-time format value) result)
"default" (local-time 6) (local-time 6)
"default" "06:00:00" (local-time 6)
"default" "09:00" INVALID
"default" "3 am" INVALID
"default" "3.00" INVALID
"default" "invalid" INVALID
"default" true INVALID
"default" "" INVALID
"any" "06:00:00" (local-time 6)
;; "any" "3:00 am" (local-time 3)
"any" "some night" INVALID
"any" "invalid" INVALID
"any" true INVALID
"any" "" INVALID
"%H:%M" (local-time 6) (local-time 6)
"%H:%M" "06:00" (local-time 6)
"%M:%H" "06:50" INVALID
"%H:%M" "3:00 am" INVALID
"%H:%M" "some night" INVALID
"%H:%M" "invalid" INVALID
"%H:%M" true INVALID
"%H:%M" "" INVALID
"invalid" "" INVALID))

0 comments on commit 05a39f3

Please sign in to comment.