Add parser for datetime property values

This commit is contained in:
Jaidyn Ann 2024-02-06 22:33:02 -06:00
parent 79f8c34c6c
commit aaa6d5b292

View File

@ -30,7 +30,7 @@
;; List of all properties with datetime values. ;; List of all properties with datetime values.
(define vcard-datetime-properties (define vcard-datetime-properties
'(ANNIVERSARY BDAY REV TZ)) '(ANNIVERSARY BDAY REV))
;; List of all properties with multiple comma-separated values. ;; List of all properties with multiple comma-separated values.
@ -45,10 +45,22 @@
SOUND SOURCE TEL UID URL)) SOUND SOURCE TEL UID URL))
;; Should parse any truncated & reduced-accuracy ISO 8601 datetime.
;; … right now, it only parses a few possibilities.
(define (string->any-date str)
(let* [(ymd "~Y~m~d")
(hms "~H~M~S")
(ymd-hms (string-join (list ymd hms) "T"))]
(or (ignore-error (string->date str ymd-hms) #f)
(ignore-error (string->date str ymd) #f)
(ignore-error (string->date str hms) #f))))
;; A list of the parser/serializer functions for each vcard property. ;; A list of the parser/serializer functions for each vcard property.
;; ((TEL #<procedure> #<procedure>) ;; ((TEL #<procedure> #<procedure>)
;; (ADR #<procedure> #<procedure>) ;; (ADR #<procedure> #<procedure>)
;; …) ;; …)
;; TODO: Add a parser for the TZ [timezone] property.
(define vcard-value-parsers (define vcard-value-parsers
(append (append
(map (lambda (uri-prop) (map (lambda (uri-prop)
@ -57,6 +69,12 @@
(string-join sts ";"))) (string-join sts ";")))
(lambda (url) (uri:uri->string url)))) (lambda (url) (uri:uri->string url))))
vcard-url-properties) vcard-url-properties)
(map (lambda (date-prop)
(list date-prop
string->any-date
(lambda (datetime)
(date->string datetime "~Y~m~dT~H~M~S~z"))))
vcard-datetime-properties)
(map (lambda (csv-prop) (map (lambda (csv-prop)
(list csv-prop (list csv-prop
(lambda (str) (string-split-unescaped str ",")) (lambda (str) (string-split-unescaped str ","))