unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Zefram <zefram@fysh.org>
To: 21907@debbugs.gnu.org
Subject: bug#21907: date->string duff ISO 8601 zone format
Date: Wed, 19 Apr 2017 22:21:46 +0100	[thread overview]
Message-ID: <20170419212146.GB912@fysh.org> (raw)
In-Reply-To: <20151113145913.GQ13455@fysh.org>

[-- Attachment #1: Type: text/plain, Size: 781 bytes --]

A sequence of two patches is attached.  The first fixes the ~2/~4 bug,
signalling an error for any unrepresentable offset.

The second is a bonus patch, which fixes related problems in ~z, the
RFC 822 zone format specifier.  Prior to the patch, ~z outputs "Z" for
UT, which would be correct for ISO 8601 format but is deprecated (along
with all the other single-letter syntax) for RFC 822.  The patch changes
that to the approved "+0000".  ~z also had exactly the same problems as
~2/~4 regarding unrepresentable offsets, so the patch fixes them in the
same way.

I could report the ~z problems in a separate ticket if you like.
Beware that the second of these patches has some textual dependence on
the first, so trying to handle them separately might just be confusing.

-zefram

[-- Attachment #2: 0001-fix-SRFI-19-s-ISO-8601-zone-output-formats.patch --]
[-- Type: text/x-diff, Size: 4220 bytes --]

From e6db0e40e5464591df204f9d07e66b3d7853c0d7 Mon Sep 17 00:00:00 2001
From: Zefram <zefram@fysh.org>
Date: Wed, 19 Apr 2017 21:50:39 +0100
Subject: [PATCH 1/2] fix SRFI-19's ISO 8601 zone output formats

The ISO 8601 timezone formats offered by SRFI-19's date->string function,
in the ~2 and ~4 format specifiers, were erroneously in the basic format
despite juxtaposition with extended-format date and time.  Fix that by
switching them to extended format.  This incidentally means that the
ISO 8601 zone format is no longer implemented as identical to the RFC
822 zone format (~z), so stop documenting them in terms of ~z.

The same format specifiers also made too much of an attempt to display
zone offsets that are not representable in ISO 8601 format.  They would
truncate an offset that is not an integral number of minutes, thus
producing inaccurate output.  The truncation of an offset in the range
(-60, 0) yielded a non-conforming "-0000".  An offset of 100 hours or more
(in either direction) resulted in non-conforming extra digits.  In all of
these cases, signal as an error that the zone offset is not representable.
---
 doc/ref/srfi-modules.texi |  4 ++--
 module/srfi/srfi-19.scm   | 22 +++++++++++++++++++---
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/doc/ref/srfi-modules.texi b/doc/ref/srfi-modules.texi
index ec3bb20..da7850f 100644
--- a/doc/ref/srfi-modules.texi
+++ b/doc/ref/srfi-modules.texi
@@ -2818,9 +2818,9 @@ with locale decimal point, eg.@: @samp{5.2}
 @item @nicode{~z} @tab time zone, RFC-822 style
 @item @nicode{~Z} @tab time zone symbol (not currently implemented)
 @item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
-@item @nicode{~2} @tab ISO-8601 time+zone, @samp{~H:~M:~S~z}
+@item @nicode{~2} @tab ISO-8601 time+zone, @samp{~3} plus zone
 @item @nicode{~3} @tab ISO-8601 time, @samp{~H:~M:~S}
-@item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~H:~M:~S~z}
+@item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~5} plus zone
 @item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~H:~M:~S}
 @end multitable
 @end defun
diff --git a/module/srfi/srfi-19.scm b/module/srfi/srfi-19.scm
index f09ec7a..ed88242 100644
--- a/module/srfi/srfi-19.scm
+++ b/module/srfi/srfi-19.scm
@@ -152,7 +152,6 @@
 (define locale-date-time-format "~a ~b ~d ~H:~M:~S~z ~Y")
 (define locale-short-date-format "~m/~d/~y")
 (define locale-time-format "~H:~M:~S")
-(define iso-8601-date-time-format "~Y-~m-~dT~H:~M:~S~z")
 
 ;;-- Miscellaneous Constants.
 ;;-- only the tai-epoch-in-jd might need changing if
@@ -970,6 +969,21 @@
         (display (padding hours #\0 2) port)
         (display (padding minutes #\0 2) port))))
 
+(define (iso-8601-tz-print offset port)
+  (let* ((neg? (negative? offset))
+	 (all-secs (abs offset))
+	 (seconds (remainder all-secs 60))
+	 (all-mins (quotient all-secs 60))
+	 (minutes (remainder all-mins 60))
+	 (hours (quotient all-mins 60)))
+    (if (or (not (= seconds 0)) (> hours 99))
+      (time-error 'date-printer 'unrepresentable-zone-offset offset)
+      (begin
+	(display (if neg? #\- #\+) port)
+        (display (padding hours #\0 2) port)
+	(display #\: port)
+        (display (padding minutes #\0 2) port)))))
+
 ;; A table of output formatting directives.
 ;; the first time is the format char.
 ;; the second is a procedure that takes the date, a padding character
@@ -1119,11 +1133,13 @@
    (cons #\1 (lambda (date pad-with port)
                (display (date->string date "~Y-~m-~d") port)))
    (cons #\2 (lambda (date pad-with port)
-               (display (date->string date "~H:~M:~S~z") port)))
+               (display (date->string date "~3") port)
+               (iso-8601-tz-print (date-zone-offset date) port)))
    (cons #\3 (lambda (date pad-with port)
                (display (date->string date "~H:~M:~S") port)))
    (cons #\4 (lambda (date pad-with port)
-               (display (date->string date "~Y-~m-~dT~H:~M:~S~z") port)))
+               (display (date->string date "~5") port)
+               (iso-8601-tz-print (date-zone-offset date) port)))
    (cons #\5 (lambda (date pad-with port)
                (display (date->string date "~Y-~m-~dT~H:~M:~S") port)))))
 
-- 
2.1.4


[-- Attachment #3: 0002-fix-SRFI-19-s-RFC-822-zone-output-format.patch --]
[-- Type: text/x-diff, Size: 2933 bytes --]

From 3c78db919ba705a5e3f1b1ac919a473b709d26c6 Mon Sep 17 00:00:00 2001
From: Zefram <zefram@fysh.org>
Date: Wed, 19 Apr 2017 22:06:35 +0100
Subject: [PATCH 2/2] fix SRFI-19's RFC 822 zone output format

The RFC 822 timezone format offered by SRFI-19's date->string function,
the ~z format specifier, was using the deprecated "Z" format for +0000.
Change it to the non-deprecated "+0000".

The same format specifier also made too much of an attempt to display zone
offsets that are not representable in RFC 822 format.  It would truncate
an offset that is not an integral number of minutes, thus producing
inaccurate output.  The truncation of an offset in the range (-60, 0)
yielded a non-conforming "-0000".  An offset of 100 hours or more (in
either direction) resulted in non-conforming extra digits.  In all of
these cases, signal as an error that the zone offset is not representable.
---
 module/srfi/srfi-19.scm | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/module/srfi/srfi-19.scm b/module/srfi/srfi-19.scm
index ed88242..4b8445f 100644
--- a/module/srfi/srfi-19.scm
+++ b/module/srfi/srfi-19.scm
@@ -953,21 +953,24 @@
 ;; FIXME: mkoeppe: Put a symbolic time zone in the date structs.
 ;; Print it here instead of the numerical offset if available.
 (define (locale-print-time-zone date port)
-  (tz-printer (date-zone-offset date) port))
+  (rfc-822-tz-print (date-zone-offset date) port))
 
 (define (locale-am-string/pm hr)
   (if (> hr 11) (locale-pm-string) (locale-am-string)))
 
-(define (tz-printer offset port)
-  (cond
-   ((= offset 0) (display "Z" port))
-   ((negative? offset) (display "-" port))
-   (else (display "+" port)))
-  (if (not (= offset 0))
-      (let ((hours   (abs (quotient offset (* 60 60))))
-            (minutes (abs (quotient (remainder offset (* 60 60)) 60))))
+(define (rfc-822-tz-print offset port)
+  (let* ((neg? (negative? offset))
+	 (all-secs (abs offset))
+	 (seconds (remainder all-secs 60))
+	 (all-mins (quotient all-secs 60))
+	 (minutes (remainder all-mins 60))
+	 (hours (quotient all-mins 60)))
+    (if (or (not (= seconds 0)) (> hours 99))
+      (time-error 'date-printer 'unrepresentable-zone-offset offset)
+      (begin
+	(display (if neg? #\- #\+) port)
         (display (padding hours #\0 2) port)
-        (display (padding minutes #\0 2) port))))
+        (display (padding minutes #\0 2) port)))))
 
 (define (iso-8601-tz-print offset port)
   (let* ((neg? (negative? offset))
@@ -1127,7 +1130,7 @@
    (cons #\Y (lambda (date pad-with port)
                (display (date-year date) port)))
    (cons #\z (lambda (date pad-with port)
-               (tz-printer (date-zone-offset date) port)))
+               (rfc-822-tz-print (date-zone-offset date) port)))
    (cons #\Z (lambda (date pad-with port)
                (locale-print-time-zone date port)))
    (cons #\1 (lambda (date pad-with port)
-- 
2.1.4


      reply	other threads:[~2017-04-19 21:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-13 14:59 bug#21907: date->string duff ISO 8601 zone format Zefram
2017-04-19 21:21 ` Zefram [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170419212146.GB912@fysh.org \
    --to=zefram@fysh.org \
    --cc=21907@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).