branch: elpa/mastodon
commit 6360c8b9d06cdbebaa8254abdbc8f70550c78507
Author: Stefan Monnier <monn...@iro.umontreal.ca>
Commit: Stefan Monnier <monn...@iro.umontreal.ca>

    * lisp/mastodon.el: Remove `ts` from `Package-Requires:`
    
    This is/was the main hurdle to be able to include `mastodon.el`
    into (Non)GNU ELPA since `ts` is/was not in (Non)GNU ELPA.
    
    Also I think the replacement function is more flexible and gives better
    results :-)
    
    * lisp/mastodon-tl.el: Don't require `ts`.
     (mastodon-tl--time-units): New const.
    (mastodon-tl--format-poll-expiry): Rewrite.
---
 lisp/mastodon-tl.el | 46 ++++++++++++++++++++++++++++------------------
 lisp/mastodon.el    |  2 +-
 2 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el
index 1d672a5bc1..8db889cec1 100644
--- a/lisp/mastodon-tl.el
+++ b/lisp/mastodon-tl.el
@@ -32,7 +32,6 @@
 ;;; Code:
 
 (require 'shr)
-(require 'ts)
 (require 'thingatpt) ; for word-at-point
 (require 'time-date)
 (require 'cl-lib)
@@ -1145,25 +1144,36 @@ LONGEST-OPTION is the option whose length determines 
the formatting."
                 (propertize str 'face 'font-lock-comment-face))
               "\n"))))
 
+(defconst mastodon-tl--time-units
+  '("sec"   60.0                          ;Use a float to convert `n' to float.
+    "min"   60
+    "hour"  24
+    "day"   7
+    "week"  4.345
+    "month" 12
+    "year"))
+
 (defun mastodon-tl--format-poll-expiry (timestamp)
   "Convert poll expiry TIMESTAMP into a descriptive string."
-  (let ((parsed (ts-human-duration
-                 (ts-diff (ts-parse timestamp) (ts-now)))))
-    (cond ((> (plist-get parsed :days) 0)
-           (format "%s days, %s hours left"
-                   (plist-get parsed :days)
-                   (plist-get parsed :hours)))
-          ((> (plist-get parsed :hours) 0)
-           (format "%s hours, %s minutes left"
-                   (plist-get parsed :hours)
-                   (plist-get parsed :minutes)))
-          ((> (plist-get parsed :minutes) 0)
-           (format "%s minutes left" (plist-get parsed :minutes)))
-          (t ; we failed to guess:
-           (format "%s days, %s hours, %s minutes left"
-                   (plist-get parsed :days)
-                   (plist-get parsed :hours)
-                   (plist-get parsed :minutes))))))
+  ;; FIXME: Could we document the format of TIMESTAMP here?
+  (let* ((ts (encode-time (parse-time-string timestamp)))
+         (seconds (time-to-seconds (time-subtract nil ts)))
+         (units mastodon-tl--time-units)
+         (n1 seconds) (unit1 (pop units)) n2 unit2
+         next)
+    (while (and units (> (truncate (setq next (/ n1 (car units)))) 0))
+      (setq unit2 unit1)
+      (setq n2 (- n1 (* (car units) (truncate n1 (car units)))))
+      (setq n1 next)
+      (pop units)
+      (setq unit1 (pop units)))
+    (setq n1 (truncate n1))
+    (if n2 (setq n2 (truncate n2)))
+    (if (memq n2 '(nil 0))
+        (format "%d %s%s left" n1 unit1 (if (> n1 1) "s" ""))
+      (format "%d %s%s, %d %s%s left"
+              n1 unit1 (if (> n1 1) "s" "")
+              n2 unit2 (if (> n2 1) "s" "")))))
 
 (defun mastodon-tl--read-poll-option ()
   "Read a poll option to vote on a poll."
diff --git a/lisp/mastodon.el b/lisp/mastodon.el
index a3b372de9a..1c5e0d5443 100644
--- a/lisp/mastodon.el
+++ b/lisp/mastodon.el
@@ -7,7 +7,7 @@
 ;;         Marty Hiatt <martianhia...@riseup.net>
 ;; Maintainer: Marty Hiatt <martianhia...@riseup.net>
 ;; Version: 1.0.0
-;; Package-Requires: ((emacs "27.1") (request "0.3.0") (persist "0.4") (ts 
"0.3"))
+;; Package-Requires: ((emacs "27.1") (request "0.3.0") (persist "0.4"))
 ;; Homepage: https://codeberg.org/martianh/mastodon.el
 
 ;; This file is not part of GNU Emacs.

Reply via email to