aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-tl.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2023-08-27 22:37:15 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2023-08-27 22:54:08 -0400
commit6360c8b9d06cdbebaa8254abdbc8f70550c78507 (patch)
tree9782d9e3bd9a5bc38a012b30d342d71ce795a434 /lisp/mastodon-tl.el
parent126e2d8642669d3bc3f16d8a163dc9f0e0325a07 (diff)
* 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.
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."