Add question for start and end dates

This commit is contained in:
2024-08-02 10:12:18 +02:00
parent 2018b9f7d1
commit cd7b40b5dc
2 changed files with 28 additions and 2 deletions

View File

@@ -51,7 +51,7 @@
:else (let [script (last (eval-file template-file))
i18n (:i18n script)
nights (util/ask-int (:nights? i18n))
nights (util/ask-nights (:nights? i18n) (:start-date? i18n) (:end-date? i18n))
store (:store script)
profile ((:profile script) nights)]
(spit output-file (str (core/produce store profile nights i18n) "\n"))))))

View File

@@ -1,5 +1,9 @@
(ns packer.util (:gen-class))
(import '(java.time.temporal ChronoUnit)
'(java.time LocalDate)
'(java.time.format DateTimeFormatter))
(defn ask
([question prompt] (print (str question prompt))
(flush)
@@ -21,3 +25,25 @@
(if (.contains "yn" answer)
(= "y" answer)
(recur question))))
(defn parse-date [date]
(let [now (LocalDate/now)
parts (into [] (map #(format "%02d" (Integer/parseInt %)) (clojure.string/split date #"\W")))
formatter (DateTimeFormatter/ofPattern "dd-MM-yyyy")
month (String/format "%02d" (into-array Integer [(.getMonthValue now)]))
year (.toString (.getYear now))
full-date (case (count parts)
1 (str (parts 0) "-" month "-" year)
2 (str (parts 0) "-" (parts 1) "-" year)
3 (str (parts 0) "-" (parts 1) "-" (parts 2))
(throw (Exception. (str "Invalid date format: " date))))]
(LocalDate/parse full-date formatter)))
(defn count-nights [start end]
(.between ChronoUnit/DAYS (parse-date start) (parse-date end)))
(defn ask-nights [nightsQuestion startQuestion endQuestion]
(let [start (ask startQuestion)
end (when (not (empty? start)) (ask endQuestion))
nights (when (empty? start) (ask-int nightsQuestion))]
(if (empty? start) nights (count-nights start end))))