diff --git a/ement-lib.el b/ement-lib.el index 350829a..1003e47 100644 --- a/ement-lib.el +++ b/ement-lib.el @@ -1088,6 +1088,49 @@ slots, and puts them on ROOM's `members' table." (funcall then data)))) (message "Ement: Getting joined members in %s..." (ement--format-room room)))) +(cl-defun ement--human-format-duration (seconds &optional abbreviate) + "Return human-formatted string describing duration SECONDS. +If SECONDS is less than 1, returns \"0 seconds\". If ABBREVIATE +is non-nil, return a shorter version, without spaces. This is a +simple calculation that does not account for leap years, leap +seconds, etc." + ;; Copied from `ts-human-format-duration' (same author). + (if (< seconds 1) + (if abbreviate "0s" "0 seconds") + (cl-macrolet ((format> (place) + ;; When PLACE is greater than 0, return formatted string using its symbol name. + `(when (> ,place 0) + (format "%d%s%s" ,place + (if abbreviate "" " ") + (if abbreviate + ,(substring (symbol-name place) 0 1) + ,(symbol-name place))))) + (join-places (&rest places) + ;; Return string joining the names and values of PLACES. + `(string-join (delq nil + (list ,@(cl-loop for place in places + collect `(format> ,place)))) + (if abbreviate "" ", ")))) + (pcase-let ((`(,years ,days ,hours ,minutes ,seconds) (ement--human-duration seconds))) + (join-places years days hours minutes seconds))))) + +(defun ement--human-duration (seconds) + "Return list describing duration SECONDS. +List includes years, days, hours, minutes, and seconds. This is +a simple calculation that does not account for leap years, leap +seconds, etc." + ;; Copied from `ts-human-format-duration' (same author). + (cl-macrolet ((dividef (place divisor) + ;; Divide PLACE by DIVISOR, set PLACE to the remainder, and return the quotient. + `(prog1 (/ ,place ,divisor) + (setf ,place (% ,place ,divisor))))) + (let* ((seconds (floor seconds)) + (years (dividef seconds 31536000)) + (days (dividef seconds 86400)) + (hours (dividef seconds 3600)) + (minutes (dividef seconds 60))) + (list years days hours minutes seconds)))) + ;;; Footer (provide 'ement-lib) diff --git a/ement-room-list.el b/ement-room-list.el index b89b3f6..d0d9f0d 100644 --- a/ement-room-list.el +++ b/ement-room-list.el @@ -360,8 +360,8 @@ To be called in `ement-sync-callback-hook'." (replace-regexp-in-string "\n" "" topic t t) "")) (formatted-timestamp (if latest-ts - (ts-human-format-duration (- (ts-unix (ts-now)) (/ latest-ts 1000)) - t) + (ement--human-format-duration (- (time-convert nil 'integer) (/ latest-ts 1000)) + t) "")) (latest-face (when latest-ts (let* ((difference-seconds (- (float-time) (/ latest-ts 1000)) ) diff --git a/ement-room.el b/ement-room.el index dcdab8e..4f57ca4 100644 --- a/ement-room.el +++ b/ement-room.el @@ -2005,11 +2005,11 @@ For use as `imenu-create-index-function'." ement-ewoc (lambda (node) (pcase (ewoc-data node) (`(ts . ,_) t))))) - (ts-format (string-trim ement-room-timestamp-header-with-date-format))) + (timestamp-format (string-trim ement-room-timestamp-header-with-date-format))) (cl-loop for node in timestamp-nodes collect (pcase-let* ((`(ts ,timestamp) (ewoc-data node)) - (formatted (format-time-string ts-format timestamp))) + (formatted (format-time-string timestamp-format timestamp))) (cons formatted (ewoc-location node)))))) ;;;;; Occur diff --git a/ement-taxy.el b/ement-taxy.el index efaf706..2a761c7 100644 --- a/ement-taxy.el +++ b/ement-taxy.el @@ -152,7 +152,7 @@ ((cl-struct ement-room latest-ts) room) (age)) (when latest-ts - (setf age (ts-diff (ts-now) (make-ts :unix (/ latest-ts 1000)))) + (setf age (- (time-convert nil 'integer) (/ latest-ts 1000))) (cond (newer-than (when (<= age newer-than) (or name (format "Newer than %s seconds" newer-than)))) @@ -174,7 +174,7 @@ ((cl-struct ement-room latest-ts) room) (age)) (when latest-ts - (setf age (- (ts-unix (ts-now)) (/ latest-ts 1000))) + (setf age (- (time-convert nil 'integer) (/ latest-ts 1000))) (or (alist-get age intervals nil nil #'>) "Older than a year")))) @@ -319,7 +319,7 @@ (min (/ (length ement-room-list-timestamp-colors) 2) (+ 24 (truncate (/ difference-seconds 86400 7))))))) (face (list :foreground (elt ement-room-list-timestamp-colors n))) - (formatted-ts (ts-human-format-duration difference-seconds 'abbreviate))) + (formatted-ts (ement--human-format-duration difference-seconds 'abbreviate))) (string-match (rx (1+ digit) (repeat 1 alpha)) formatted-ts) (propertize (match-string 0 formatted-ts) 'face face 'help-echo formatted-ts)) diff --git a/ement.el b/ement.el index c98296f..f1dd8a0 100644 --- a/ement.el +++ b/ement.el @@ -6,7 +6,7 @@ ;; Keywords: comm ;; URL: https://github.com/alphapapa/ement.el ;; Package-Version: 0.1-pre -;; Package-Requires: ((emacs "27.1") (map "2.1") (plz "0.1-pre") (taxy "0.9") (taxy-magit-section "0.9") (svg-lib "0.2.5") (transient "0.3.7") (ts "0.2.1")) +;; Package-Requires: ((emacs "27.1") (map "2.1") (plz "0.1-pre") (taxy "0.9") (taxy-magit-section "0.9") (svg-lib "0.2.5") (transient "0.3.7")) ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by