;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/time-cmp.el (require 'cl-lib) (require 'pcase) (defun wall-clock-time (h1 m1 s1 h2 m2 s2) (let*((y 1978) (m 05) (d 08) ; arbitrary date, 1978-05-08 (total-seconds-1 (float-time (encode-time s1 m1 h1 d m y))) (total-seconds-2 (float-time (encode-time s2 m2 h2 d m y))) (s-diff (- total-seconds-2 total-seconds-1)) ) (format-seconds "%.2h:%.2m:%.2s" s-diff) )) (defalias 'wct #'wall-clock-time) ;; (wct 09 35 10 23 00 00) ; 13:24:50 (defun days (y1 m1 d1 y2 m2 d2) (let*((then (float-time (encode-time 0 0 0 d1 m1 y1))) (now (float-time (encode-time 0 0 0 d2 m2 y2))) (diff (- now then)) ) (string-to-number (format-seconds "%d" diff)) )) ;; (days 1958 04 13 1958 08 30) ; 139 days between Tahiti Nui 2 & 3 (defun days-date (date1 date2) (pcase-let*( (sep "-") (`(,y1 ,m1 ,d1) (cl-map 'list #'string-to-number (split-string date1 sep))) (`(,y2 ,m2 ,d2) (cl-map 'list #'string-to-number (split-string date2 sep))) ) (days y1 m1 d1 y2 m2 d2) )) ;; (days-date "2021-03-19" "2021-04-20") ; 31 ;; (days-date "1964-07-26" "2021-03-22") ; 20 693 (defun get-time-since (y m d) (interactive "nyear: \nnmonth: \nnday: ") (message "%s" (format-seconds "%yy %dd" (float-time (time-since (encode-time 0 0 0 d m y)) )))) ;; (get-time-since 2011 09 27) ; 10y 172d [2022-03-15] (defun days-since (y m d) (string-to-number (format-seconds "%d" (float-time (time-since (encode-time 0 0 0 d m y))) ))) ;; (days-since 1 1 1) ; 738 228 days from 0001-01-01 [2022-03-15] (provide 'time-cmp)