The integer number of minutes.
@item %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
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
optional leading \".\" for zero-padding. For example, \"%.3Y\" will
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
must be given in order of decreasing size. To the left of \"%z\", nothing
is output until the first non-zero unit is encountered."
("s" "second" 1)
("z")))
(case-fold-search t)
- spec match usedunits zeroflag larger prev name unit num zeropos)
- (while (string-match "%\\.?[0-9]*\\(.\\)" string start)
+ spec match usedunits zeroflag larger prev name unit num zeropos
+ fraction)
+ (while (string-match "%\\.?[0-9]*\\(,[0-9]\\)?\\(.\\)" string start)
(setq start (match-end 0)
- spec (match-string 1 string))
+ spec (match-string 2 string))
(unless (string-equal spec "%")
(or (setq match (assoc (downcase spec) units))
(error "Bad format specifier: `%s'" spec))
(push match usedunits)))
(and zeroflag larger
(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)
(setq spec (car u)
name (cadr 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
(setq string
(replace-regexp-in-string
(setq zeropos (unless (zerop num) (match-beginning 0))))
(setq string
(replace-match
- (format (concat "%" (match-string 1 string) "d%s") num
- (if (string-equal (match-string 2 string) spec)
- "" ; lower-case, no unit-name
+ (format (if (match-string 2 string)
+ (concat
+ "%"
+ (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
(if (= num 1) "" "s"))))
t t string))))))
(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 %%" 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 ()
(should (equal (date-ordinal-to-time 2008 271)