Comment #460

Anonymous wrote the following reply to https://scripter.co/convert-seconds-to-human-time/:

That's not how you split seconds into mod-60 units.
Do something like this instead:
(defun secs-to-units (timef)
"Split the seconds in the integer or float `timef' into a list of dhms"
(let* ((time (floor timef))
(frac (- timef time))
(hrs (% (/ time (* 60 60)) 24))
(mins (% (/ time 60) 60))
(secs (+ (% time 60) frac))
(days (/ time (* 60 60 24))))
`(,days ,hrs ,mins ,secs)))

And to format it:
(defun fmt-secs (time)
"Format seconds."
(pcase-let ((`(,d ,h ,m ,s) (secs-to-units time)))
(format "%dd %dh %dm %.2fs" d h m s)))

Reply to this comment