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: sirgazil
Subject: Re: How do you format date-times in RFC 3339 style?
Date: Wed, 13 Feb 2019 09:21:43 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0

Hi, tantalum.


El 12/02/19 a las 1:33 p. m., tantalum escribió:
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

From these tests, I think this could work for me, thanks.


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

You know what would be great? If you packaged "sph-lib" for GNU Guix :) That way I could easily install it and your library would be visible in the list of packages in Guile website.


--
Luis Felipe López Acevedo
http://sirgazil.bitbucket.io/





reply via email to

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