unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#21907: date->string duff ISO 8601 zone format
@ 2015-11-13 14:59 Zefram
  2017-04-19 21:21 ` Zefram
  0 siblings, 1 reply; 2+ messages in thread
From: Zefram @ 2015-11-13 14:59 UTC (permalink / raw)
  To: 21907

scheme@(guile-user)> (use-modules (srfi srfi-19))
scheme@(guile-user)> (date->string (julian-day->date 2450000 3600) "~4")
$1 = "1995-10-09T13:00:00+0100"
scheme@(guile-user)> (date->string (julian-day->date 2450000 3630) "~4")
$2 = "1995-10-09T13:00:30+0100"

There are two problems here with date->string's representation of zone
offsets for the ISO 8601 formats "~2" and "~4".  Firstly, because the
time-of-day is represented in the extended format with colon separators,
the zone offset must also be represented with colon separators.  So the
first "+0100" above should be "+01:00".

Secondly, the offset is being truncated to an integral minute, so the
output doesn't fully represent the zone offset.  More importantly,
because the local time-of-day isn't being adjusted to match, it's not
accurately representing the point in time.  ISO 8601 doesn't permit
a seconds component in the zone offset, so you have a choice of three
not-entirely-satisfactory options.  Firstly, you could round the zone
offset and adjust the represented local time accordingly, so the 3630
conversion above would yield either "1995-10-09T13:00:00+01:00" or
"1995-10-09T13:01:00+01:01".  Secondly, you could use the obvious
extension of the ISO 8601 format to a seconds component, outputting
"1995-10-09T13:00:30+01:00:30".  Or finally you could signal an error
when trying to represent a zone offset that's not an integral minute.

Incidentally, for offsets of -1 to -59 inclusive, the truncation isn't
clearing the negative sign, so is producing the invalid output "-0000".
The zero offset is required to be represented with a "+" sign.  If you
take the rounding option described above, anything that rounds to a
zero-minutes offset must yield "+00:00" in the output.

-zefram





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

* bug#21907: date->string duff ISO 8601 zone format
  2015-11-13 14:59 bug#21907: date->string duff ISO 8601 zone format Zefram
@ 2017-04-19 21:21 ` Zefram
  0 siblings, 0 replies; 2+ messages in thread
From: Zefram @ 2017-04-19 21:21 UTC (permalink / raw)
  To: 21907

[-- 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


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

end of thread, other threads:[~2017-04-19 21:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-13 14:59 bug#21907: date->string duff ISO 8601 zone format Zefram
2017-04-19 21:21 ` Zefram

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).