]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/tmdm/datetimes.lisp
feat(xtm): support resource references on name variants
[topic-maps.git] / src / tmdm / datetimes.lisp
index 581d05b23d7279f382e28a8e842dcb20cf060896..df123ccf908e39d0acace1d79c4f40a3c2591550 100644 (file)
@@ -68,20 +68,59 @@ Return components and NIL, or NIL and a specific diagnostic. Reuses Date validat
         (if (< month 12) (format nil "~A-~2,'0D-01" year (1+ month))
             (concatenate 'string (next-datetime-year year) "-01-01")))))
 
+(defun previous-datetime-year (year)
+  "Inverse of NEXT-DATETIME-YEAR, using character arithmetic and no year zero."
+  (labels ((negate (text)
+             (if (char= #\- (char text 0)) (subseq text 1)
+                 (concatenate 'string "-" text))))
+    (negate (next-datetime-year (negate year)))))
+
+(defun previous-datetime-date (date)
+  "Previous day under the existing Date validator's calendar contract."
+  (let* ((dash (position #\- date :start (if (char= #\- (char date 0)) 1 0)))
+         (year (subseq date 0 dash))
+         (month (parse-integer date :start (1+ dash) :end (+ dash 3)))
+         (day (parse-integer date :start (+ dash 4) :end (+ dash 6))))
+    (cond ((> day 1) (format nil "~A-~2,'0D-~2,'0D" year month (1- day)))
+          ((= month 1) (concatenate 'string (previous-datetime-year year) "-12-31"))
+          (t (loop for last-day downfrom 31 to 28
+                   for candidate = (format nil "~A-~2,'0D-~2,'0D" year (1- month) last-day)
+                   when (date-lexical-p candidate) return candidate)))))
+
+(defun datetime-offset-minutes (zone)
+  "Signed whole minutes of an already validated XSD timezone; NIL if absent."
+  (cond ((zerop (length zone)) nil)
+        ((string= zone "Z") 0)
+        (t (* (if (char= #\- (char zone 0)) -1 1)
+              (+ (* 60 (parse-integer zone :start 1 :end 3))
+                 (parse-integer zone :start 4 :end 6))))))
+
 (defun canonical-datetime-lexical-form (value)
-  "Pure XSD 1.0 canonical lexical form for absent/zero timezone.
-Second value :CANONICAL, invalid diagnostic, :UNSUPPORTED-TIMEZONE, or
-:UNSUPPORTED-LEAP-SECOND-CONTEXT. Never change the model/retained handles.
-Noncandidate leap dates follow D.1's following-minute interpretation."
+  "Pure XSD 1.0 UTC canonical lexical form, preserving an absent timezone.
+Second value :CANONICAL, an invalid diagnostic, :UNSUPPORTED-BCE-TIMEZONE-ROLLOVER,
+or :UNSUPPORTED-LEAP-SECOND-CONTEXT. Never change model data/retained handles.
+Noncandidate leap dates retain Slice 10's D.1 following-minute interpretation."
   (check-type value string)
   (multiple-value-bind (parts reason) (datetime-parts value)
     (unless parts (return-from canonical-datetime-lexical-form (values nil reason)))
     (let* ((date (getf parts :date)) (hour (getf parts :hour)) (minute (getf parts :minute))
            (second (getf parts :second)) (fraction (string-right-trim "0" (getf parts :fraction)))
-           (zone (getf parts :zone)) (zoned (plusp (length zone))))
-      (unless (member zone '("" "Z" "+00:00" "-00:00") :test #'string=)
-        (return-from canonical-datetime-lexical-form (values nil :unsupported-timezone)))
+           (zone (getf parts :zone)) (zoned (plusp (length zone)))
+           (offset (datetime-offset-minutes zone)) (original-date date))
+      ;; Do not infer the UTC context of a local leap second. Zero/absent-zone
+      ;; behavior below is exactly the pre-existing Slice 10 contract.
+      (when (and offset (not (zerop offset)) (= second 60))
+        (return-from canonical-datetime-lexical-form (values nil :unsupported-leap-second-context)))
       (when (= hour 24) (setf hour 0 date (next-datetime-date date)))
+      (when (and offset (not (zerop offset)))
+        ;; Only the hour/minute fields are small integers. Years and fractions
+        ;; stay character sequences; no float, system clock or local timezone.
+        (multiple-value-bind (days minutes) (floor (- (+ (* 60 hour) minute) offset) 1440)
+          (setf hour (floor minutes 60) minute (mod minutes 60)
+                date (case days (-1 (previous-datetime-date date)) (1 (next-datetime-date date)) (otherwise date))))
+        (when (and (not (string= date original-date))
+                   (or (char= #\- (char original-date 0)) (char= #\- (char date 0))))
+          (return-from canonical-datetime-lexical-form (values nil :unsupported-bce-timezone-rollover))))
       (when (= second 60)
         (let* ((md (subseq date (- (length date) 5)))
                (candidate (member md '("03-31" "06-30" "09-30" "12-31") :test #'string=)))