Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |