diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2021-06-14 15:32:03 +0200 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2021-06-14 15:32:03 +0200 |
commit | 00f1a4be719cab4f1a3591ab3321ff34c86af86b (patch) | |
tree | 5d6a8a785b8d34fbac4ea919d7be89606289a4d6 | |
parent | 663fb3b774887d3d15a6791c3f35af56daa3c676 (diff) | |
download | emacs-00f1a4be719cab4f1a3591ab3321ff34c86af86b.tar.gz |
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." <jp@neverwas.me>.
-rw-r--r-- | lisp/calendar/iso8601.el | 17 | ||||
-rw-r--r-- | 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") |