- For a shorter display when hours [or hh:mm] are not required, set the ‘short’ parameter to 1.
- To display hh:mm:ss without milliseconds, set the second parameter [places] to 0.
- The ’round’ parameter can be set to round up, round down or round to the closest whole number of seconds.
For full details of all options, please look at the code, but feel free to comment and ask if anything is not clear. So here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
;; Handy function to convert seconds into hh:mm:ss ;; by Steve Daulton (https://audionyq.com) March 2013 ;; ;; Released under terms of the GNU General Public License version 2 or later: ;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html (defun ss-to-hhmmss (ss &optional (short 1)(places 3)(round 1)(sep ":")) ;; short=0 shows full hhmmss ;; short=1 shows hh, mm only if required ;; places is decimal places ;; round=0 to round down. ;; round=1 to round to closest ;; round=2 to round up ;; sep is the value separator ;; returns a string value (setq ss (float ss)) (let* ((hh (truncate (/ ss 3600.0))) (mm (- (truncate (/ ss 60.0))(* hh 60))) (dec (- ss (truncate ss))) (ss (rem (truncate ss) 60)) ; whole seconds (exp10 (power 10 places))) ;; handle rounding (setq dec (let ((dec10 (* dec exp10))) (case round (0 (/ (truncate dec10) exp10)) (2 (if (> (truncate (* dec10 10))(truncate(* 10 (truncate dec10)))) (/ (truncate (1+ dec10)) exp10) (/ (truncate dec10) exp10))) (T dec)))) ; Add back decimal places if required (when (> places 0)(setq ss (+ ss dec))) (setf *float-format* (format nil "%#.~af" places)) ;;; pad numbers with leading zeros (defun pad (num) (cond ((> num 9) num) ((> num 0) (format nil "0~a" num)) (T "00"))) (case short (0 (format nil "~a~a~a~a~a" (pad hh) sep (pad mm) sep (pad ss))) (T (format nil "~a~a~a~a~a" (if (> hh 0) hh "") (if (> hh 0) sep "") (if (> mm 0) (if (> hh 0)(pad mm) mm) "") (if (> mm 0) sep "") (if (> (+ hh mm) 0) (pad ss) ss)))))) ; test it (ss-to-hhmmss 123456.789) |