From aaa6d5b292e41c8c0af3dfcb75960d2d589f8df7 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Tue, 6 Feb 2024 22:33:02 -0600 Subject: [PATCH] Add parser for datetime property values --- vcarded.scm | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/vcarded.scm b/vcarded.scm index 53fc978..1845e84 100644 --- a/vcarded.scm +++ b/vcarded.scm @@ -30,7 +30,7 @@ ;; List of all properties with datetime values. (define vcard-datetime-properties - '(ANNIVERSARY BDAY REV TZ)) + '(ANNIVERSARY BDAY REV)) ;; List of all properties with multiple comma-separated values. @@ -45,10 +45,22 @@ 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. ;; ((TEL # #) ;; (ADR # #) ;; …) +;; TODO: Add a parser for the TZ [timezone] property. (define vcard-value-parsers (append (map (lambda (uri-prop) @@ -57,11 +69,17 @@ (string-join sts ";"))) (lambda (url) (uri:uri->string url)))) 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) - (list csv-prop - (lambda (str) (string-split-unescaped str ",")) - (lambda (csv-list) (string-join csv-list ",")))) - vcard-csv-properties) + (list csv-prop + (lambda (str) (string-split-unescaped str ",")) + (lambda (csv-list) (string-join csv-list ",")))) + vcard-csv-properties) (map (lambda (semicolon-prop) (list semicolon-prop (lambda (str) (string-split-unescaped str ";"))