1 (in-package #:topic-maps.tmdm)
3 (defun datetime-datatype () "http://www.w3.org/2001/XMLSchema#dateTime")
5 (defun datetime-parts (value)
6 "XSD 1.0 Second Edition 3.2.7/D.1 lexical validation, not value equality.
7 Return components and NIL, or NIL and a specific diagnostic. Reuses Date validation."
8 (let* ((text (string-trim '(#\Space #\Tab #\Return #\Newline) value))
9 (tee (position #\T text)))
10 (labels ((bad (reason) (return-from datetime-parts (values nil reason)))
11 (digits (s) (and (plusp (length s)) (every (lambda (c) (find c "0123456789")) s)))
12 (two (s i) (+ (* 10 (position (char s i) "0123456789"))
13 (position (char s (1+ i)) "0123456789"))))
14 (unless tee (bad :invalid-datetime-syntax))
15 (let* ((date (subseq text 0 tee)) (time (subseq text (1+ tee))))
16 (multiple-value-bind (a date-end end reason) (date-lexical-bounds date)
17 (unless (and a (= date-end end)) (bad (or reason :invalid-datetime-date))))
18 (unless (and (>= (length time) 8) (digits (subseq time 0 2)) (char= #\: (char time 2))
19 (digits (subseq time 3 5)) (char= #\: (char time 5)) (digits (subseq time 6 8)))
20 (bad :invalid-datetime-time))
21 (let* ((zone-start (position-if (lambda (c) (find c "Z+-")) time :start 8))
22 (fraction (subseq time 8 zone-start)) (zone (if zone-start (subseq time zone-start) ""))
23 (hour (two time 0)) (minute (two time 3)) (second (two time 6)))
24 (unless (or (zerop (length fraction))
25 (and (> (length fraction) 1) (char= #\. (char fraction 0)) (digits (subseq fraction 1))))
26 (bad :invalid-datetime-fraction))
27 (unless (date-lexical-p (concatenate 'string date zone)) (bad :invalid-datetime-timezone))
28 (unless (<= hour 24) (bad :invalid-datetime-hour))
29 (unless (<= minute 59) (bad :invalid-datetime-minute))
30 ;; XSD 1.0 D.1 permits whole seconds 60; XSD 1.1 rules are not substituted.
31 (unless (<= second 60) (bad :invalid-datetime-second))
32 (when (and (= hour 24)
33 (or (plusp minute) (plusp second)
34 (and (plusp (length fraction)) (find-if-not (lambda (c) (char= c #\0)) (subseq fraction 1)))))
35 (bad :invalid-datetime-midnight))
36 (values (list :date date :hour hour :minute minute :second second
37 :fraction (if (zerop (length fraction)) "" (subseq fraction 1)) :zone zone) nil))))))
39 (defun datetime-lexical-p (value)
40 "Validate the supported XSD 1.0 lexical space, including offset/second-60 syntax."
41 (check-type value string)
42 (not (null (datetime-parts value))))
44 (defun next-datetime-year (year)
45 "Character arithmetic: arbitrary-length lexical year, skipping XSD 1.0 year zero."
46 (let* ((negative (char= #\- (char year 0)))
47 (digits (copy-seq (if negative (subseq year 1) year))))
48 (when (and negative (string= digits "0001")) (return-from next-datetime-year "0001"))
50 (loop for i downfrom (1- (length digits)) to 0 while carry do
52 (if (char= #\0 (char digits i)) (setf (char digits i) #\9)
53 (setf (char digits i) (code-char (1- (char-code (char digits i)))) carry nil)))
54 ((char= #\9 (char digits i)) (setf (char digits i) #\0))
55 (t (setf (char digits i) (code-char (1+ (char-code (char digits i)))) carry nil))))
56 (when carry (setf digits (concatenate 'string "1" digits))))
57 (loop while (and (> (length digits) 4) (char= #\0 (char digits 0))) do (setf digits (subseq digits 1)))
58 (concatenate 'string (if negative "-" "") digits)))
60 (defun next-datetime-date (date)
61 "Next Gregorian day using Slice 9 validity and character year rollover."
62 (let* ((dash (position #\- date :start (if (char= #\- (char date 0)) 1 0)))
63 (year (subseq date 0 dash))
64 (month (+ (* 10 (position (char date (1+ dash)) "0123456789")) (position (char date (+ dash 2)) "0123456789")))
65 (day (+ (* 10 (position (char date (+ dash 4)) "0123456789")) (position (char date (+ dash 5)) "0123456789")))
66 (candidate (format nil "~A-~2,'0D-~2,'0D" year month (1+ day))))
67 (if (date-lexical-p candidate) candidate
68 (if (< month 12) (format nil "~A-~2,'0D-01" year (1+ month))
69 (concatenate 'string (next-datetime-year year) "-01-01")))))
71 (defun canonical-datetime-lexical-form (value)
72 "Pure XSD 1.0 canonical lexical form for absent/zero timezone.
73 Second value :CANONICAL, invalid diagnostic, :UNSUPPORTED-TIMEZONE, or
74 :UNSUPPORTED-LEAP-SECOND-CONTEXT. Never change the model/retained handles.
75 Noncandidate leap dates follow D.1's following-minute interpretation."
76 (check-type value string)
77 (multiple-value-bind (parts reason) (datetime-parts value)
78 (unless parts (return-from canonical-datetime-lexical-form (values nil reason)))
79 (let* ((date (getf parts :date)) (hour (getf parts :hour)) (minute (getf parts :minute))
80 (second (getf parts :second)) (fraction (string-right-trim "0" (getf parts :fraction)))
81 (zone (getf parts :zone)) (zoned (plusp (length zone))))
82 (unless (member zone '("" "Z" "+00:00" "-00:00") :test #'string=)
83 (return-from canonical-datetime-lexical-form (values nil :unsupported-timezone)))
84 (when (= hour 24) (setf hour 0 date (next-datetime-date date)))
86 (let* ((md (subseq date (- (length date) 5)))
87 (candidate (member md '("03-31" "06-30" "09-30" "12-31") :test #'string=)))
88 (cond ((and zoned candidate (= hour 23) (= minute 59)))
89 ((member md '("03-31" "04-01" "06-30" "07-01" "09-30" "10-01" "12-31" "01-01") :test #'string=)
90 (return-from canonical-datetime-lexical-form (values nil :unsupported-leap-second-context)))
91 (t (setf second 0) (incf minute)
92 (when (= minute 60) (setf minute 0) (incf hour))
93 (when (= hour 24) (setf hour 0 date (next-datetime-date date)))))))
94 (values (format nil "~AT~2,'0D:~2,'0D:~2,'0D~A~A" date hour minute second
95 (if (zerop (length fraction)) "" (concatenate 'string "." fraction))
96 (if zoned "Z" "")) :canonical))))