From 00f1a4be719cab4f1a3591ab3321ff34c86af86b Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Mon, 14 Jun 2021 15:32:03 +0200 Subject: [PATCH] Get fractional seconds in iso8601 parsing right * lisp/calendar/iso8601.el (iso8601-parse-time): Get fractional times (with leading zeroes in the fraction part) right (bug#49017). Fix based on a patch by "J.P." . --- lisp/calendar/iso8601.el | 17 +++++++++++------ test/lisp/calendar/iso8601-tests.el | 10 +++++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lisp/calendar/iso8601.el b/lisp/calendar/iso8601.el index 44c48119845..f22f060e205 100644 --- a/lisp/calendar/iso8601.el +++ b/lisp/calendar/iso8601.el @@ -231,17 +231,22 @@ See `decode-time' for the meaning of FORM." (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))))) + (frac-string (match-string 4 time)) + fraction fraction-precision) + (when frac-string + ;; Remove trailing zeroes. + (setq frac-string (replace-regexp-in-string "0+\\'" "" frac-string)) + (when (length> frac-string 0) + (setq fraction (string-to-number frac-string) + fraction-precision (length frac-string)))) (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 fraction-precision)) + fraction) + (expt 10 fraction-precision)))) ;; 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 618e5b12386..c4d038ab68c 100644 --- a/test/lisp/calendar/iso8601-tests.el +++ b/test/lisp/calendar/iso8601-tests.el @@ -183,7 +183,15 @@ (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))) + (should (equal (iso8601-parse-time "15:27:35.00001" t) + '((3500001 . 100000) 27 15 nil nil nil nil -1 nil))) + (should (equal (iso8601-parse-time "15:27:35.0000100" t) + '((3500001 . 100000) 27 15 nil nil nil nil -1 nil))) + (should (equal (iso8601-parse-time "15:27:35.0" t) + '(35 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.39.5