Check for valid date

This commit is contained in:
Marcus Kammer 2024-08-12 18:28:17 +02:00
parent fe5aaa9b0d
commit 8f6de1affe

View file

@ -270,3 +270,27 @@ Returns:
(build-str-name name)
"-"
(remove-special-chars (build-str-value-prop value))))
(defun is-leap-year (year)
(and (zerop (mod year 4))
(or (not (zerop (mod year 100)))
(zerop (mod year 400)))))
(defun days-in-month (month year)
(case month
((1 3 5 7 8 10 12) 31)
((4 6 9 11) 30)
(2 (if (is-leap-year year) 29 28))
(otherwise 0)))
(defun valid-date-string-p (date-string)
(and (= (length date-string) 10)
(char= (char date-string 4) #\-)
(char= (char date-string 7) #\-)
(let* ((year (parse-integer date-string :start 0 :end 4 :junk-allowed t))
(month (parse-integer date-string :start 5 :end 7 :junk-allowed t))
(day (parse-integer date-string :start 8 :end 10 :junk-allowed t)))
(and year month day
(>= year 1)
(<= 1 month 12)
(<= 1 day (days-in-month month year))))))