guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do you format date-times in RFC 3339 style?


From: tantalum
Subject: Re: How do you format date-times in RFC 3339 style?
Date: Tue, 12 Feb 2019 18:33:14 +0000
User-agent: Posteo Webmail

i once wrote a rfc3339 reader/writer and worked through some gotchas, maybe the code contains hints that are helpful. for example, here is a function that converts from a timestamp to a rfc3339 date string:

  (define* (utc->rfc3339 a #:optional (offset 0) (seconds-fraction 0))
    "integer:posix-time -> string"
    (let
      ( (date-time
          (let (t (gmtime (+ a offset)))
            (apply
              (l (y m d h mi s)
                (string-append y "-"
                  m "-"
                  d "T"
                  h ":"
                  mi ":"
                  s
                  (if (zero? seconds-fraction) ""
(string-append "." (number->string seconds-fraction)))))
              (map number->padded-string
                (list (+ 1900 (tm:year t)) (+ 1 (tm:mon t))
                  (tm:mday t) (tm:hour t) (tm:min t) (tm:sec t))))))
        (offset
          (if (zero? offset) "Z"
            (apply
              (l (sign numbers)
(string-append sign (string-join (map number->padded-string numbers) ":"))) (let* ((hms (drop-right (utc-duration->hms offset) 1)) (hours (first hms))) (if (any negative? hms) (list "-" (map (l (a) (* -1 a)) hms)) (list "+" hms)))))))
      (string-append date-time offset)))

l: lambda, first: car

the code passes these tests (examples of valid strings):
https://github.com/sph-mn/sph-lib/blob/master/modules/test/module/sph/time/rfc3339.scm

for reference, the complete code: https://github.com/sph-mn/sph-lib/blob/master/modules/sph/time/rfc3339.scm



reply via email to

[Prev in Thread] Current Thread [Next in Thread]