Add HHMMSS and HHMMSSmmm

pull/8387/head
Ben Yorke 2023-01-20 12:53:24 +01:00
parent b6c00254e8
commit ba68a368f4
2 changed files with 100 additions and 14 deletions

View File

@ -50,30 +50,41 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
"y" t/years)] "y" t/years)]
(offset-fn (offset-fn relative-date (offset-unit-fn offset-amount))))) (offset-fn (offset-fn relative-date (offset-unit-fn offset-amount)))))
(defn get-offset-hh-mm (defn get-ts-units
"There are currently 4 time suffixes being used in inputs: "There are currently several time suffixes being used in inputs:
- ms: milliseconds, will return a time relative to the direction the date is being adjusted - ms: milliseconds, will return a time relative to the direction the date is being adjusted
- start: will return the time at the start of the day [00:00:00] - start: will return the time at the start of the day [00:00:00.000]
- end: will return the time at the end of the day [24:00:00] - end: will return the time at the end of the day [23:59:59.999]
- HHMM: will return the specified time at the turn of the minute [HH:MM:00]" - HHMM: will return the specified time at the turn of the minute [HH:MM:00.000]
- HHMMSS: will return the specified time at the turm of the second [HH:MM:SS.000]
- HHMMSSmmm: will return the specified time at the turn of the millisecond [HH:MM:SS.mmm]
The latter three will be capped to the maximum allowed for each unit so they will always be valid times"
[offset-direction offset-time] [offset-direction offset-time]
(case offset-time (case offset-time
"ms" (if (= offset-direction "+") [23 59 59 999] [0 0 0 0]) "ms" (if (= offset-direction "+") [23 59 59 999] [0 0 0 0])
"start" [0 0 0 0] "start" [0 0 0 0]
"end" [23 59 59 999] "end" [23 59 59 999]
;; if it's not a matching string, then assume it is HHMM ;; if it's not a matching string, then assume it is HHMM
[(parse-long (subs offset-time 0 2)) (parse-long (subs offset-time 2 4)) 0 0])) (let [[h1 h2 m1 m2 s1 s2 ms1 ms2 ms3] (str offset-time "000000000")]
[(min 23 (parse-long (str h1 h2)))
(min 59 (parse-long (str m1 m2)))
(min 59 (parse-long (str s1 s2)))
(min 999 (parse-long (str ms1 ms2 ms3)))])))
(let [[h1 h2 m1 m2 s1 s2 ms1 ms2 ms3] (str "235959" "0000000000")]
[(parse-long (str h1 h2)) (parse-long (str m1 m2)) (parse-long (str s1 s2)) (parse-long (str ms1 ms2 ms3))])
(defn keyword-input-dispatch [input] (defn keyword-input-dispatch [input]
(cond (cond
(#{:current-page :current-block :parent-block :today :yesterday :tomorrow :right-now-ms} input) input (#{:current-page :current-block :parent-block :today :yesterday :tomorrow :right-now-ms} input) input
(re-find #"^[+-]\d+[dwmy]?$" (name input)) :relative-date (re-find #"^[+-]\d+[dwmy]?$" (name input)) :relative-date
(re-find #"^[+-]\d+[dwmy]-(ms|start|end|\d\d\d\d)?$" (name input)) :relative-date-time (re-find #"^[+-]\d+[dwmy]-(ms|start|end|\d{2}|\d{4}|\d{6}|\d{9})?$" (name input)) :relative-date-time
(= :start-of-today-ms input) :today-time (= :start-of-today-ms input) :today-time
(= :end-of-today-ms input) :today-time (= :end-of-today-ms input) :today-time
(re-find #"^today-(start|end|\d\d\d\d)$" (name input)) :today-time (re-find #"^today-(start|end|\d{2}|\d{4}|\d{6}|\d{9})$" (name input)) :today-time
(re-find #"^\d+d(-before|-after|-before-ms|-after-ms)?$" (name input)) :DEPRECATED-relative-date)) (re-find #"^\d+d(-before|-after|-before-ms|-after-ms)?$" (name input)) :DEPRECATED-relative-date))
@ -108,7 +119,7 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
(let [[hh mm ss ms] (case input (let [[hh mm ss ms] (case input
:start-of-today-ms [0 0 0 0] :start-of-today-ms [0 0 0 0]
:end-of-today-ms [23 59 59 999] :end-of-today-ms [23 59 59 999]
(get-offset-hh-mm nil (subs (name input) 6)))] (get-ts-units nil (subs (name input) 6)))]
(date-at-local-ms (t/today) hh mm ss ms))) (date-at-local-ms (t/today) hh mm ss ms)))
;; relative-date returns a YYYMMDD string ;; relative-date returns a YYYMMDD string
@ -121,9 +132,9 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
;; relative-date-time returns an epoch int ;; relative-date-time returns an epoch int
(defmethod resolve-keyword-input :relative-date-time [_ input _] (defmethod resolve-keyword-input :relative-date-time [_ input _]
(let [relative-to (get-relative-date input) (let [relative-to (get-relative-date input)
[_ offset-direction offset offset-unit offset-time] (re-find #"^([+-])(\d+)([dwmy])-(ms|start|end|\d\d\d\d)$" (name input)) [_ offset-direction offset offset-unit ts] (re-find #"^([+-])(\d+)([dwmy])-(ms|start|end|\d{2,9})$" (name input))
offset-date (get-offset-date relative-to offset-direction offset offset-unit) offset-date (get-offset-date relative-to offset-direction offset offset-unit)
[hh mm ss ms] (get-offset-hh-mm offset-direction offset-time)] [hh mm ss ms] (get-ts-units offset-direction ts)]
(date-at-local-ms offset-date hh mm ss ms))) (date-at-local-ms offset-date hh mm ss ms)))
(defmethod resolve-keyword-input :DEPRECATED-relative-date [db input opts] (defmethod resolve-keyword-input :DEPRECATED-relative-date [db input opts]

View File

@ -254,6 +254,84 @@ created-at:: %s"
[(<= ?timestamp ?end)]]})))) [(<= ?timestamp ?end)]]}))))
":today-HHMM and :today-HHMM resolve to correct datetime range") ":today-HHMM and :today-HHMM resolve to correct datetime range")
(is (= ["today"]
(sort
(map #(-> % :block/content string/split-lines first)
(custom-query {:inputs [:today-115959 :today-120001]
:query '[:find (pull ?b [*])
:in $ ?start ?end
:where
[?b :block/content]
[?b :block/created-at ?timestamp]
[(>= ?timestamp ?start)]
[(<= ?timestamp ?end)]]}))))
":today-HHMMSS and :today-HHMMSS resolve to correct datetime range")
(is (= ["today"]
(sort
(map #(-> % :block/content string/split-lines first)
(custom-query {:inputs [:today-115959999 :today-120000001]
:query '[:find (pull ?b [*])
:in $ ?start ?end
:where
[?b :block/content]
[?b :block/created-at ?timestamp]
[(>= ?timestamp ?start)]
[(<= ?timestamp ?end)]]}))))
":today-HHMMSSmmm and :today-HHMMSSmmm resolve to correct datetime range")
(is (= ["today" "tonight"]
(sort
(map #(-> % :block/content string/split-lines first)
(custom-query {:inputs [:today-1199 :today-9901]
:query '[:find (pull ?b [*])
:in $ ?start ?end
:where
[?b :block/content]
[?b :block/created-at ?timestamp]
[(>= ?timestamp ?start)]
[(<= ?timestamp ?end)]]}))))
":today-HHMM and :today-HHMM resolve to valid datetime ranges")
(is (= ["+1d" "tonight"]
(sort
(map #(-> % :block/content string/split-lines first)
(custom-query {:inputs [:-0d-1201 :+1d-2359]
:query '[:find (pull ?b [*])
:in $ ?start ?end
:where
[?b :block/content]
[?b :block/created-at ?timestamp]
[(>= ?timestamp ?start)]
[(<= ?timestamp ?end)]]}))))
":-XT-HHMM and :+XT-HHMM resolve to correct datetime range")
(is (= ["+1d" "tonight"]
(sort
(map #(-> % :block/content string/split-lines first)
(custom-query {:inputs [:-0d-120001 :+1d-235959]
:query '[:find (pull ?b [*])
:in $ ?start ?end
:where
[?b :block/content]
[?b :block/created-at ?timestamp]
[(>= ?timestamp ?start)]
[(<= ?timestamp ?end)]]}))))
":-XT-HHMMSS and :+XT-HHMMSS resolve to correct datetime range")
(is (= ["+1d" "tonight"]
(sort
(map #(-> % :block/content string/split-lines first)
(custom-query {:inputs [:-0d-120000001 :+1d-235959999]
:query '[:find (pull ?b [*])
:in $ ?start ?end
:where
[?b :block/content]
[?b :block/created-at ?timestamp]
[(>= ?timestamp ?start)]
[(<= ?timestamp ?end)]]}))))
":-XT-HHMMSSmmm and :+XT-HHMMSSmmm resolve to correct datetime range")
(is (= ["+1d" "tonight"] (is (= ["+1d" "tonight"]
(sort (sort
(map #(-> % :block/content string/split-lines first) (map #(-> % :block/content string/split-lines first)
@ -267,9 +345,6 @@ created-at:: %s"
[(<= ?timestamp ?end)]]})))) [(<= ?timestamp ?end)]]}))))
":-XT-HHMM and :+XT-HHMM resolve to correct datetime range")) ":-XT-HHMM and :+XT-HHMM resolve to correct datetime range"))
(deftest resolve-input-for-relative-date-queries (deftest resolve-input-for-relative-date-queries
(load-test-files [{:file/content "- -1y" :file/path "journals/2022_01_01.md"} (load-test-files [{:file/content "- -1y" :file/path "journals/2022_01_01.md"}
{:file/content "- -1m" :file/path "journals/2022_12_01.md"} {:file/content "- -1m" :file/path "journals/2022_12_01.md"}