>From 22d1d026a5edc6cc6f85e4fe522ed29bf0a23f16 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Sun, 13 Jun 2021 23:50:47 -0700 Subject: [PATCH] Use string length for fraction in iso8601-parse-time * lisp/calendar/iso8601.el (iso8601-parse-time): Replace arithmetic operation to count number of digits in fractional seconds of time value. Use number of text chars in regexp match (range 0-9) instead. * test/lisp/calendar/iso8601-tests.el (nonstandard-test-time-of-day-decimals): Add test case for above. --- lisp/calendar/iso8601.el | 21 ++++++++++----------- test/lisp/calendar/iso8601-tests.el | 4 +++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lisp/calendar/iso8601.el b/lisp/calendar/iso8601.el index 44c4811984..a2f843e643 100644 --- a/lisp/calendar/iso8601.el +++ b/lisp/calendar/iso8601.el @@ -226,22 +226,21 @@ iso8601-parse-time (zone (match-string 2 string))) (if (not (iso8601--match iso8601--time-match time)) (signal 'wrong-type-argument string) - (let ((hour (string-to-number (match-string 1 time))) - (minute (and (match-string 2 time) - (string-to-number (match-string 2 time)))) - (second (and (match-string 3 time) - (string-to-number (match-string 3 time)))) - (fraction (and (not (zerop (length (match-string 4 time)))) - (string-to-number (match-string 4 time))))) + (let* ((hour (string-to-number (match-string 1 time))) + (minute (and (match-string 2 time) + (string-to-number (match-string 2 time)))) + (second (and (match-string 3 time) + (string-to-number (match-string 3 time)))) + (frac-prec (length (match-string 4 time))) + (fraction (and (not (zerop frac-prec)) + (string-to-number (match-string 4 time))))) (when (and fraction (eq form t)) (cond ;; Sub-second time. (second - (let ((digits (1+ (truncate (log fraction 10))))) - (setq second (cons (+ (* second (expt 10 digits)) - fraction) - (expt 10 digits))))) + (setq second (cons (+ (* second (expt 10 frac-prec)) fraction) + (expt 10 frac-prec)))) ;; Fractional minute. (minute (setq second (iso8601--decimalize fraction 60))) diff --git a/test/lisp/calendar/iso8601-tests.el b/test/lisp/calendar/iso8601-tests.el index 618e5b1238..59292c063c 100644 --- a/test/lisp/calendar/iso8601-tests.el +++ b/test/lisp/calendar/iso8601-tests.el @@ -183,7 +183,9 @@ nonstandard-test-time-of-day-decimals (should (equal (iso8601-parse-time "15:27:35.123" t) '((35123 . 1000) 27 15 nil nil nil nil -1 nil))) (should (equal (iso8601-parse-time "15:27:35.123456789" t) - '((35123456789 . 1000000000) 27 15 nil nil nil nil -1 nil)))) + '((35123456789 . 1000000000) 27 15 nil nil nil nil -1 nil))) + (should (equal (iso8601-parse-time "15:27:35.012345678" t) + '((35012345678 . 1000000000) 27 15 nil nil nil nil -1 nil)))) (ert-deftest standard-test-time-of-day-beginning-of-day () (should (equal (iso8601-parse-time "000000") -- 2.31.1