1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2025-01-22 18:35:09 +00:00

`format-time' can now do sub-second times

* doc/lispref/os.texi (Time Parsing): Document it.

* lisp/calendar/time-date.el (format-seconds): Allow formatting
sub-second times.
This commit is contained in:
Lars Ingebrigtsen 2020-10-30 12:10:17 +01:00
parent ff3838ecc3
commit 617f70bc7d
4 changed files with 49 additions and 10 deletions

View File

@ -1901,7 +1901,10 @@ The integer number of hours.
The integer number of minutes. The integer number of minutes.
@item %s @item %s
@itemx %S @itemx %S
The integer number of seconds. The number of seconds. If the optional @samp{,} parameter is used,
it's a floating point number, and the number after the @samp{,}
specifies how many decimals to be used. @samp{%,2s} means ``use two
decimals''.
@item %z @item %z
Non-printing control flag. When it is used, other specifiers must be Non-printing control flag. When it is used, other specifiers must be
given in the order of decreasing size, i.e., years before days, hours given in the order of decreasing size, i.e., years before days, hours

View File

@ -1204,6 +1204,11 @@ list.
** Miscellaneous ** Miscellaneous
+++
*** 'format-seconds' can now be used for sub-second times.
The new optional "," parameter has been added, and
(format-seconds "%mm %,1ss" 66.4) will now result in "1m 6.4s".
--- ---
*** 'global-display-fill-column-indicator-mode' skips some buffers. *** 'global-display-fill-column-indicator-mode' skips some buffers.
By default, turning on 'global-display-fill-column-indicator-mode' By default, turning on 'global-display-fill-column-indicator-mode'

View File

@ -278,6 +278,10 @@ Lower-case specifiers return only the unit.
optional leading \".\" for zero-padding. For example, \"%.3Y\" will optional leading \".\" for zero-padding. For example, \"%.3Y\" will
return something of the form \"001 year\". return something of the form \"001 year\".
The \"%s\" spec takes an additional optional parameter,
introduced by the \",\" character, to say how many decimals to
use. \"%,1s\" means \"use one decimal\".
The \"%z\" specifier does not print anything. When it is used, specifiers The \"%z\" specifier does not print anything. When it is used, specifiers
must be given in order of decreasing size. To the left of \"%z\", nothing must be given in order of decreasing size. To the left of \"%z\", nothing
is output until the first non-zero unit is encountered." is output until the first non-zero unit is encountered."
@ -289,10 +293,11 @@ is output until the first non-zero unit is encountered."
("s" "second" 1) ("s" "second" 1)
("z"))) ("z")))
(case-fold-search t) (case-fold-search t)
spec match usedunits zeroflag larger prev name unit num zeropos) spec match usedunits zeroflag larger prev name unit num zeropos
(while (string-match "%\\.?[0-9]*\\(.\\)" string start) fraction)
(while (string-match "%\\.?[0-9]*\\(,[0-9]\\)?\\(.\\)" string start)
(setq start (match-end 0) (setq start (match-end 0)
spec (match-string 1 string)) spec (match-string 2 string))
(unless (string-equal spec "%") (unless (string-equal spec "%")
(or (setq match (assoc (downcase spec) units)) (or (setq match (assoc (downcase spec) units))
(error "Bad format specifier: `%s'" spec)) (error "Bad format specifier: `%s'" spec))
@ -307,12 +312,17 @@ is output until the first non-zero unit is encountered."
(push match usedunits))) (push match usedunits)))
(and zeroflag larger (and zeroflag larger
(error "Units are not in decreasing order of size")) (error "Units are not in decreasing order of size"))
(setq seconds (time-convert seconds 'integer)) (unless (numberp seconds)
(setq seconds (float-time seconds)))
(setq fraction (mod seconds 1)
seconds (round seconds))
(dolist (u units) (dolist (u units)
(setq spec (car u) (setq spec (car u)
name (cadr u) name (cadr u)
unit (nth 2 u)) unit (nth 2 u))
(when (string-match (format "%%\\(\\.?[0-9]+\\)?\\(%s\\)" spec) string) (when (string-match
(format "%%\\(\\.?[0-9]+\\)?\\(,[0-9]+\\)?\\(%s\\)" spec)
string)
(if (string-equal spec "z") ; must be last in units (if (string-equal spec "z") ; must be last in units
(setq string (setq string
(replace-regexp-in-string (replace-regexp-in-string
@ -327,9 +337,23 @@ is output until the first non-zero unit is encountered."
(setq zeropos (unless (zerop num) (match-beginning 0)))) (setq zeropos (unless (zerop num) (match-beginning 0))))
(setq string (setq string
(replace-match (replace-match
(format (concat "%" (match-string 1 string) "d%s") num (format (if (match-string 2 string)
(if (string-equal (match-string 2 string) spec) (concat
"" ; lower-case, no unit-name "%"
(and (match-string 1 string)
(if (= (elt (match-string 1 string) 0) ?.)
(concat "0" (substring
(match-string 1 string) 1))
(match-string 1 string)))
(concat "." (substring
(match-string 2 string) 1))
"f%s")
(concat "%" (match-string 1 string) "d%s"))
(if (= unit 1)
(+ num fraction)
num)
(if (string-equal (match-string 3 string) spec)
"" ; lower-case, no unit-name
(format " %s%s" name (format " %s%s" name
(if (= num 1) "" "s")))) (if (= num 1) "" "s"))))
t t string)))))) t t string))))))

View File

@ -81,7 +81,14 @@
(ert-deftest test-format-seconds () (ert-deftest test-format-seconds ()
(should (equal (format-seconds "%y %d %h %m %s %%" 0) "0 0 0 0 0 %")) (should (equal (format-seconds "%y %d %h %m %s %%" 0) "0 0 0 0 0 %"))
(should (equal (format-seconds "%y %d %h %m %s %%" 9999999) "0 115 17 46 39 %")) (should (equal (format-seconds "%y %d %h %m %s %%" 9999999) "0 115 17 46 39 %"))
(should (equal (format-seconds "%y %d %h %m %z %s %%" 1) " 1 %"))) (should (equal (format-seconds "%y %d %h %m %z %s %%" 1) " 1 %"))
(should (equal (format-seconds "%mm %ss" 66) "1m 6s"))
(should (equal (format-seconds "%mm %5ss" 66) "1m 6s"))
(should (equal (format-seconds "%mm %.5ss" 66.4) "1m 00006s"))
(should (equal (format-seconds "%mm %,1ss" 66.4) "1m 6.4s"))
(should (equal (format-seconds "%mm %5,1ss" 66.4) "1m 6.4s"))
(should (equal (format-seconds "%mm %.5,1ss" 66.4) "1m 006.4s")))
(ert-deftest test-ordinal () (ert-deftest test-ordinal ()
(should (equal (date-ordinal-to-time 2008 271) (should (equal (date-ordinal-to-time 2008 271)