# elisp 怎么对像“17:15” 的时间字符串进行加减

elisp 怎么对像“17:15” 的时间字符串进行加减

(elisp) Time Parsing

`````` (defun my//calculate-time-duration (start end)
"Calculate seconds(format: SS) duration from START to END(format: \"HH:MM:SS\")."
(-
(let ((end-sum 0) (end-acc 0))
(mapc
(lambda (x)
(if (and (<= (- x ?0) 9) (>= (- x ?0) 0))
(setq end-acc (+ (* 10 end-acc) (- x ?0)))
(setq end-sum (+ (* end-sum 60) end-acc)
end-acc 0)))
end)
(setq end-sum (+ (* end-sum 60) end-acc))
end-sum)
(let ((start-sum 0) (start-acc 0))
(mapc
(lambda (x)
(if (and (<= (- x ?0) 9) (>= (- x ?0) 0))
(setq start-acc (+ (* 10 start-acc) (- x ?0)))
(setq start-sum (+ (* start-sum 60) start-acc)
start-acc 0)))
start)
(setq start-sum (+ (* start-sum 60) start-acc))
start-sum)))

(defun my//convert-second-to-time-format (second)
"Convert input SECOND(SS) to time format(\"HH:MM:SS\")."
(let (time)
(dotimes (i 2)
(setq time (concat (format ":%02d" (% second 60)) time)
second (/ second 60)))
(setq time (concat (format "%02d" second) time))
time))
``````
``````(my//calculate-time-duration "01:23" "45:56")
;; => 2673

(my//convert-second-to-time-format 2673)
;; => "00:44:33"

(my//calculate-time-duration "12:15" "01:12:34")
;; => 3619

(my//convert-second-to-time-format 3619)
;; => "01:00:19"
``````
``````(defun zerolee-time-subtract-timestamps (start-timestamp end-timestamp)
"Subtract END-TIMESTAMP with START-TIMESTAMP."
(let ((start 0)
(end 0))
(- (dolist (time (mapcar #'string-to-number (split-string end-timestamp ":")) end)
(setq end (+ time (* end 60))))
(dolist (time (mapcar #'string-to-number (split-string start-timestamp ":")) start)
(setq start (+ time (* start 60)))))))

(defun zerolee-time-format (seconds)
(let ((min (/ seconds 60)))
(format "%02d:%02d:%02d" (/ min 60) (% min 60) (% seconds 60))))
``````