From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Daniel Llorens Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] Support ~N in SRFI-19 string->date Date: Mon, 10 Dec 2018 19:09:17 +0100 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 (Mac OS X Mail 12.1 \(3445.101.1\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1544465245 31209 195.159.176.226 (10 Dec 2018 18:07:25 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 10 Dec 2018 18:07:25 +0000 (UTC) To: guile-devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Dec 10 19:07:21 2018 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gWPxg-0007zz-V4 for guile-devel@m.gmane.org; Mon, 10 Dec 2018 19:07:21 +0100 Original-Received: from localhost ([::1]:34139 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWPzn-00053K-HB for guile-devel@m.gmane.org; Mon, 10 Dec 2018 13:09:31 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56622) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWPzj-00053E-2I for guile-devel@gnu.org; Mon, 10 Dec 2018 13:09:28 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gWPze-0000jJ-LT for guile-devel@gnu.org; Mon, 10 Dec 2018 13:09:26 -0500 Original-Received: from vimdzmsp-sfwd05.bluewin.ch ([195.186.120.133]:60610) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gWPze-0000ha-BN for guile-devel@gnu.org; Mon, 10 Dec 2018 13:09:22 -0500 Original-Received: from 4box.home ([100.91.106.162]) by vimdzmsp-sfwd05.bluewin.ch Swisscom AG with SMTP id WPzZg3Q1buZRuWPzZgLaAh; Mon, 10 Dec 2018 19:09:18 +0100 X-Bluewin-Spam-Analysis: v=2.1 cv=ZuUgU4PG c=1 sm=1 tr=0 a=E/cB+Ag6Owp6h9AtTK67vw==:117 a=E/cB+Ag6Owp6h9AtTK67vw==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=kj9zAlcOel0A:10 a=Yyz4WlLKvX0KinVbpBQA:9 a=CjuIK1q_8ugA:10 X-Bluewin-Spam-Score: 0.00 X-FXIT-IP: IPv4[100.91.106.162] Epoch[1544465358] X-Bluewin-AuthAs: dll@bluewin.ch X-Mailer: Apple Mail (2.3445.101.1) X-CMAE-Envelope: MS4wfBnVxPhgFAAf9qwrRAylxT9w0Rvl5YZexwoF+FKB0HcCpe18pln96rmm5l1jsmciqpYwcm/2X+meRjue/Lex2EsDjRP4SgbRBNWTt1kQ4JRiVk6eXQjX ocZJbHzwEvCT5SuTAWk7NKhX7p+8EcaVlmqQ7gMn4IUIZjJe40K7/Icw X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 195.186.120.133 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.org gmane.lisp.guile.devel:19773 Archived-At: I copied from the reference implementation, added a test and fixed the = doc. OK to apply? Regards Daniel =46rom 95b024dbf4ac0788560dcc9b251b9ad855296d67 Mon Sep 17 00:00:00 2001 From: Daniel Llorens 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): =46rom 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} =20 +@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))) =20 +;; 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 (>=3D 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)))) =20 + (pass-if "string->date understands nanoseconds (1)" + (time=3D? (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=3D? (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" --=20 2.11.0