unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* How do you format date-times in RFC 3339 style?
@ 2019-02-11 19:02 sirgazil
  2019-02-11 21:39 ` Nala Ginrut
  0 siblings, 1 reply; 13+ messages in thread
From: sirgazil @ 2019-02-11 19:02 UTC (permalink / raw)
  To: Guile User

Hi,

I'm generating atom feeds from SXML that don't validate because of the 
date-time format I'm using:

 > (use-modules (srfi srfi-19))
 > (date->string (current-date) "~Y-~m-~dT~H:~M.~S~z")
$1 = "2019-02-11T13:42.57-0500"

String $1 is close to the RFC 3339 style,¹ which is the required format 
for atom feeds, but the time zone offset should be -05:00, and Guile's 
SRFI-19 ~z time zone escape is RFC 822 style, which formats the time 
zone offset as -0500.

What should I do to get the offset in the required format?


Thanks,


_____

1. http://www.faqs.org/rfcs/rfc3339.html


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





^ permalink raw reply	[flat|nested] 13+ messages in thread
* How do you format date-times in RFC 3339 style?
@ 2019-02-12 18:29 tantalum
  0 siblings, 0 replies; 13+ messages in thread
From: tantalum @ 2019-02-12 18:29 UTC (permalink / raw)
  To: sirgazil; +Cc: guile-user

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



^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: How do you format date-times in RFC 3339 style?
@ 2019-02-12 18:33 tantalum
  2019-02-13 14:21 ` sirgazil
  0 siblings, 1 reply; 13+ messages in thread
From: tantalum @ 2019-02-12 18:33 UTC (permalink / raw)
  To: sirgazil; +Cc: Guile user

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



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2019-02-14 21:13 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-02-11 19:02 How do you format date-times in RFC 3339 style? sirgazil
2019-02-11 21:39 ` Nala Ginrut
2019-02-11 21:40   ` Nala Ginrut
2019-02-12 14:33   ` sirgazil
2019-02-12 16:18     ` Nala Ginrut
2019-02-12 17:33       ` sirgazil
2019-02-12 16:50     ` Ricardo Wurmus
2019-02-12 18:15       ` sirgazil
  -- strict thread matches above, loose matches on Subject: below --
2019-02-12 18:29 tantalum
2019-02-12 18:33 tantalum
2019-02-13 14:21 ` sirgazil
2019-02-14 19:26   ` tantalum
2019-02-14 21:13     ` Ricardo Wurmus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).