]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/datetimes.lisp
Implement XTM association and role core with original conformance evidence
[topic-maps.git] / src / tmdm / datetimes.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (defun datetime-datatype () "http://www.w3.org/2001/XMLSchema#dateTime")
4
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))))))
38
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))))
43
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"))
49 (let ((carry t))
50 (loop for i downfrom (1- (length digits)) to 0 while carry do
51 (cond (negative
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)))
59
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")))))
70
71 (defun previous-datetime-year (year)
72 "Inverse of NEXT-DATETIME-YEAR, using character arithmetic and no year zero."
73 (labels ((negate (text)
74 (if (char= #\- (char text 0)) (subseq text 1)
75 (concatenate 'string "-" text))))
76 (negate (next-datetime-year (negate year)))))
77
78 (defun previous-datetime-date (date)
79 "Previous day under the existing Date validator's calendar contract."
80 (let* ((dash (position #\- date :start (if (char= #\- (char date 0)) 1 0)))
81 (year (subseq date 0 dash))
82 (month (parse-integer date :start (1+ dash) :end (+ dash 3)))
83 (day (parse-integer date :start (+ dash 4) :end (+ dash 6))))
84 (cond ((> day 1) (format nil "~A-~2,'0D-~2,'0D" year month (1- day)))
85 ((= month 1) (concatenate 'string (previous-datetime-year year) "-12-31"))
86 (t (loop for last-day downfrom 31 to 28
87 for candidate = (format nil "~A-~2,'0D-~2,'0D" year (1- month) last-day)
88 when (date-lexical-p candidate) return candidate)))))
89
90 (defun datetime-offset-minutes (zone)
91 "Signed whole minutes of an already validated XSD timezone; NIL if absent."
92 (cond ((zerop (length zone)) nil)
93 ((string= zone "Z") 0)
94 (t (* (if (char= #\- (char zone 0)) -1 1)
95 (+ (* 60 (parse-integer zone :start 1 :end 3))
96 (parse-integer zone :start 4 :end 6))))))
97
98 (defun canonical-datetime-lexical-form (value)
99 "Pure XSD 1.0 UTC canonical lexical form, preserving an absent timezone.
100 Second value :CANONICAL, an invalid diagnostic, :UNSUPPORTED-BCE-TIMEZONE-ROLLOVER,
101 or :UNSUPPORTED-LEAP-SECOND-CONTEXT. Never change model data/retained handles.
102 Noncandidate leap dates retain Slice 10's D.1 following-minute interpretation."
103 (check-type value string)
104 (multiple-value-bind (parts reason) (datetime-parts value)
105 (unless parts (return-from canonical-datetime-lexical-form (values nil reason)))
106 (let* ((date (getf parts :date)) (hour (getf parts :hour)) (minute (getf parts :minute))
107 (second (getf parts :second)) (fraction (string-right-trim "0" (getf parts :fraction)))
108 (zone (getf parts :zone)) (zoned (plusp (length zone)))
109 (offset (datetime-offset-minutes zone)) (original-date date))
110 ;; Do not infer the UTC context of a local leap second. Zero/absent-zone
111 ;; behavior below is exactly the pre-existing Slice 10 contract.
112 (when (and offset (not (zerop offset)) (= second 60))
113 (return-from canonical-datetime-lexical-form (values nil :unsupported-leap-second-context)))
114 (when (= hour 24) (setf hour 0 date (next-datetime-date date)))
115 (when (and offset (not (zerop offset)))
116 ;; Only the hour/minute fields are small integers. Years and fractions
117 ;; stay character sequences; no float, system clock or local timezone.
118 (multiple-value-bind (days minutes) (floor (- (+ (* 60 hour) minute) offset) 1440)
119 (setf hour (floor minutes 60) minute (mod minutes 60)
120 date (case days (-1 (previous-datetime-date date)) (1 (next-datetime-date date)) (otherwise date))))
121 (when (and (not (string= date original-date))
122 (or (char= #\- (char original-date 0)) (char= #\- (char date 0))))
123 (return-from canonical-datetime-lexical-form (values nil :unsupported-bce-timezone-rollover))))
124 (when (= second 60)
125 (let* ((md (subseq date (- (length date) 5)))
126 (candidate (member md '("03-31" "06-30" "09-30" "12-31") :test #'string=)))
127 (cond ((and zoned candidate (= hour 23) (= minute 59)))
128 ((member md '("03-31" "04-01" "06-30" "07-01" "09-30" "10-01" "12-31" "01-01") :test #'string=)
129 (return-from canonical-datetime-lexical-form (values nil :unsupported-leap-second-context)))
130 (t (setf second 0) (incf minute)
131 (when (= minute 60) (setf minute 0) (incf hour))
132 (when (= hour 24) (setf hour 0 date (next-datetime-date date)))))))
133 (values (format nil "~AT~2,'0D:~2,'0D:~2,'0D~A~A" date hour minute second
134 (if (zerop (length fraction)) "" (concatenate 'string "." fraction))
135 (if zoned "Z" "")) :canonical))))