From 977db3799cafbbdec3751112dfe3234e443a81c1 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sun, 15 Oct 2023 11:58:10 +0200 Subject: dates: conform RFC-3339 standard --- src/dates.scm | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/dates.scm b/src/dates.scm index e9c9b0c..a70aa01 100644 --- a/src/dates.scm +++ b/src/dates.scm @@ -1,4 +1,5 @@ (define-module (src dates) + #:use-module (ice-9 format) #:use-module (srfi srfi-1) #:use-module (srfi srfi-19) #:export (string/ISO->date @@ -12,7 +13,26 @@ (string->date string "~Y-~m-~d")) (define (date->string/RFC3339 date) - (date->string date "~4")) + (call-with-output-string + (lambda (p) + (let* ((offset (abs (date-zone-offset date))) + (rem-h-seconds (modulo offset (* 60 60))) + (offset-hours (floor (/ (- offset rem-h-seconds) 60 60))) + (rem-m-seconds (modulo rem-h-seconds 60)) + (offset-minutes (floor (/ (- rem-h-seconds rem-m-seconds) 60)))) + (format + p + "~4,'0d-~2,'0d-~2,'0dT~2,'0d:~2,'0d:~2,'0d~:[Z~;~c~2,'0d:~2,'0d~]" + (date-year date) + (date-month date) + (date-day date) + (date-hour date) + (date-minute date) + (date-second date) + (not (= 0 (date-zone-offset date))) + (if (< 0 (date-zone-offset date)) #\+ #\-) + offset-hours + offset-minutes))))) (define unix-date (string/ISO->date "1970-01-01")) -- cgit v1.2.3