aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-tl.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/mastodon-tl.el')
-rw-r--r--lisp/mastodon-tl.el46
1 files changed, 28 insertions, 18 deletions
diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el
index 1d672a5..8db889c 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."