From 8f6de1affe7030110c0982074dac28fa6cf7ab0c Mon Sep 17 00:00:00 2001 From: Marcus Kammer Date: Mon, 12 Aug 2024 18:28:17 +0200 Subject: [PATCH] Check for valid date --- src/main.lisp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main.lisp b/src/main.lisp index 764e1e2..60cf3b9 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -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))))))