From: Daniel Llorens <daniel.llorens@bluewin.ch>
To: guile-devel <guile-devel@gnu.org>
Subject: [PATCH] Support ~N in SRFI-19 string->date
Date: Mon, 10 Dec 2018 19:09:17 +0100 [thread overview]
Message-ID: <CB1C1F67-F2A0-4260-86B3-DF3892301777@bluewin.ch> (raw)
I copied from the reference implementation, added a test and fixed the doc.
OK to apply?
Regards
Daniel
From 95b024dbf4ac0788560dcc9b251b9ad855296d67 Mon Sep 17 00:00:00 2001
From: Daniel Llorens <daniel.llorens@bluewin.ch>
Date: Mon, 10 Dec 2018 11:57:05 +0100
Subject: [PATCH] Support ~N in SRFI-19 string->date
* module/srfi/srfi-19.scm (fractional-integer-reader,
make-fractional-integer-reader): From reference implementation.
(reader-directives): Handle #\N, from reference implementation.
* test-suite/tests/srfi-19: Add tests for string->date ~N.
* doc/ref/srfi-modules.texi (string->date): Add line for ~N.
---
doc/ref/srfi-modules.texi | 5 +++++
module/srfi/srfi-19.scm | 22 ++++++++++++++++++++++
test-suite/tests/srfi-19.test | 8 ++++++++
3 files changed, 35 insertions(+)
diff --git a/doc/ref/srfi-modules.texi b/doc/ref/srfi-modules.texi
index 99967e574..11fa24d68 100644
--- a/doc/ref/srfi-modules.texi
+++ b/doc/ref/srfi-modules.texi
@@ -2926,6 +2926,11 @@ the date.
@tab minute
@tab @nicode{date-minute}
+@item @nicode{~N}
+@tab @nicode{char-numeric?}
+@tab nanosecond
+@tab @nicode{date-nanosecond}
+
@item @nicode{~S}
@tab @nicode{char-numeric?}
@tab second
diff --git a/module/srfi/srfi-19.scm b/module/srfi/srfi-19.scm
index 46de91a7e..66939f92c 100644
--- a/module/srfi/srfi-19.scm
+++ b/module/srfi/srfi-19.scm
@@ -1193,6 +1193,24 @@
(lambda (port)
(integer-reader upto port)))
+;; read an fractional integer upto n characters long on port; upto -> #f if any length
+;;
+;; The return value is normalized to upto decimal places. For example, if upto is 9 and
+;; the string read is "123", the return value is 123000000.
+(define (fractional-integer-reader upto port)
+ (define (accum-int port accum nchars)
+ (let ((ch (peek-char port)))
+ (if (or (eof-object? ch)
+ (not (char-numeric? ch))
+ (and upto (>= nchars upto)))
+ (* accum (expt 10 (- upto nchars)))
+ (accum-int port (+ (* accum 10) (char->int (read-char port))) (+ nchars 1)))))
+ (accum-int port 0 0))
+
+(define (make-fractional-integer-reader upto)
+ (lambda (port)
+ (fractional-integer-reader upto port)))
+
;; read *exactly* n characters and convert to integer; could be padded
(define (integer-reader-exact n port)
(let ((padding-ok #t))
@@ -1305,6 +1323,7 @@
(define read-directives
(let ((ireader4 (make-integer-reader 4))
(ireader2 (make-integer-reader 2))
+ (fireader9 (make-fractional-integer-reader 9))
(eireader2 (make-integer-exact-reader 2))
(locale-reader-abbr-weekday (make-locale-reader
locale-abbr-weekday->index))
@@ -1343,6 +1362,9 @@
(list #\M char-numeric? ireader2 (lambda (val object)
(set-date-minute!
object val)))
+ (list #\N char-numeric? fireader9 (lambda (val object)
+ (set-date-nanosecond!
+ object val)))
(list #\S char-numeric? ireader2 (lambda (val object)
(set-date-second! object val)))
(list #\y char-fail eireader2
diff --git a/test-suite/tests/srfi-19.test b/test-suite/tests/srfi-19.test
index 4d79f1043..3642ec690 100644
--- a/test-suite/tests/srfi-19.test
+++ b/test-suite/tests/srfi-19.test
@@ -176,6 +176,14 @@ incomplete numerical tower implementation.)"
(equal? "Sun Jun 05 18:33:00+0200 2005"
(date->string date))))
+ (pass-if "string->date understands nanoseconds (1)"
+ (time=? (date->string "2018-12-10 10:53:24.189" "~Y-~m-~d ~H:~M:~S.~N")
+ (make-date 189000000 24 53 10 10 12 2018 3600)))
+
+ (pass-if "string->date understands nanoseconds (2)"
+ (time=? (date->string "2018-12-10 10:53:24.189654321" "~Y-~m-~d ~H:~M:~S.~N")
+ (make-date 189654321 24 53 10 10 12 2018 3600)))
+
(pass-if "date->string pads small nanoseconds values correctly"
(let* ((date (make-date 99999999 5 34 12 26 3 2017 0)))
(equal? "099999999"
--
2.11.0
next reply other threads:[~2018-12-10 18:09 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-10 18:09 Daniel Llorens [this message]
2018-12-11 18:28 ` [PATCH] Support ~N in SRFI-19 string->date Daniel Llorens
2018-12-11 20:52 ` Mark H Weaver
2018-12-12 1:53 ` Mark H Weaver
2018-12-12 7:36 ` Daniel Llorens
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=CB1C1F67-F2A0-4260-86B3-DF3892301777@bluewin.ch \
--to=daniel.llorens@bluewin.ch \
--cc=guile-devel@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).