diff options
author | Holger Dürer <me@hdurer.net> | 2017-05-19 22:10:38 +0100 |
---|---|---|
committer | Holger Dürer <me@hdurer.net> | 2017-06-19 17:16:29 +0100 |
commit | 20e3b77a9a37373754d0adedcee2ede6cf1f5922 (patch) | |
tree | dc182bd2a2084fedb67940aa1dec2e1d4631db75 | |
parent | d746a724db23fc3d897a6c6f11fca75b709b727c (diff) |
Show the posting date in a more readable, relative to current time format.
This is still static, i.e. doesn't update as time progresses.
-rw-r--r-- | lisp/mastodon-tl.el | 38 | ||||
-rw-r--r-- | test/mastodon-tl-tests.el | 54 |
2 files changed, 91 insertions, 1 deletions
diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 66452dd..4556613 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -146,6 +146,38 @@ Return value from boosted content if available." (or (cdr (assoc field (cdr (assoc 'reblog toot)))) (cdr (assoc field toot)))) +(defun mastodon-tl--relative-time-description (time-stamp) + "Returns a string with a human readable description of TIME-STMAP relative to the current time. + +E.g. this could return something like \"1 min ago\", \"yesterday\", etc. +TIME-STAMP is assumed to be in the past." + (let* ((now (current-time)) + (time-difference (time-subtract now time-stamp)) + (seconds-difference (float-time time-difference))) + (cond + ((< seconds-difference 60) + "less than a minute ago") + ((<= seconds-difference (* 1.5 60)) + "one minute ago") + ((< seconds-difference (* 60 59.5)) + (format "%d minutes ago" (round (/ seconds-difference 60)))) + ((<= seconds-difference (* 1.5 60 60)) + "one hour ago") + ((< seconds-difference (* 60 60 23.5)) + (format "%d hours ago" (round (/ seconds-difference 60 60)))) + ((<= seconds-difference (* 1.5 60 60 24)) + "one day ago") + ((<= seconds-difference (* 60 60 24 6.5)) + (format "%d days ago" (round (/ seconds-difference 60 60 24)))) + ((<= seconds-difference (* 1.5 60 60 24 7)) + "one week ago") + ((<= seconds-difference (* 60 60 24 365)) + (format "%d weeks ago" (round (/ seconds-difference 60 60 24 7)))) + ((<= seconds-difference (* 1.5 60 60 24 365)) + "one year ago") + (t + (format "%d years ago" (round (/ seconds-difference 60 60 24 365.25))))))) + (defun mastodon-tl--byline (toot) "Generate byline for TOOT." (let ((id (cdr (assoc 'id toot))) @@ -163,7 +195,11 @@ Return value from boosted content if available." (mastodon-tl--byline-author toot) (mastodon-tl--byline-boosted toot) " " - (format-time-string mastodon-toot-timestamp-format (date-to-time timestamp)) + (let ((parsed-time (date-to-time timestamp))) + (propertize + (format-time-string mastodon-toot-timestamp-format parsed-time) + 'timestamp parsed-time + 'display (mastodon-tl--relative-time-description parsed-time))) (propertize "\n ------------" 'face 'default)) 'favourited-p faved 'boosted-p boosted diff --git a/test/mastodon-tl-tests.el b/test/mastodon-tl-tests.el index 8c706f5..ed16b1b 100644 --- a/test/mastodon-tl-tests.el +++ b/test/mastodon-tl-tests.el @@ -1,4 +1,5 @@ (require 'el-mock) +(require 'cl-macs) (defconst mastodon-tl-test-base-toot '((id . 61208) @@ -103,6 +104,44 @@ (mock (mastodon-http--get-json "https://instance.url/api/v1/timelines/foo?max_id=12345")) (mastodon-tl--more-json "timelines/foo" 12345)))) +(ert-deftest mastodon-tl--relative-time-description () + "Should format relative time as expected" + (cl-labels ((minutes (n) (* n 60)) + (hours (n) (* n (minutes 60))) + (days (n) (* n (hours 24))) + (weeks (n) (* n (days 7))) + (years (n) (* n (days 365))) + (format-seconds-since (seconds) + (let ((time-stamp (time-subtract (current-time) (seconds-to-time seconds)))) + (mastodon-tl--relative-time-description time-stamp))) + (check (seconds expected) + (should (string= (format-seconds-since seconds) expected)))) + (check 1 "less than a minute ago") + (check 59 "less than a minute ago") + (check 60 "one minute ago") + (check 89 "one minute ago") ;; rounding down + (check 91 "2 minutes ago") ;; rounding up + (check (minutes 3.49) "3 minutes ago") ;; rounding down + (check (minutes 3.52) "4 minutes ago") + (check (minutes 59) "59 minutes ago") + (check (minutes 60) "one hour ago") + (check (minutes 89) "one hour ago") + (check (minutes 91) "2 hours ago") + (check (hours 3.49) "3 hours ago") ;; rounding down + (check (hours 3.51) "4 hours ago") ;; rounding down + (check (hours 23.4) "23 hours ago") + (check (hours 23.6) "one day ago") ;; rounding up + (check (days 1.48) "one day ago") ;; rounding down + (check (days 1.52) "2 days ago") ;; rounding up + (check (days 6.6) "one week ago") ;; rounding up + (check (weeks 2.49) "2 weeks ago") ;; rounding down + (check (weeks 2.51) "3 weeks ago") ;; rounding down + (check (weeks 52) "52 weeks ago") + (check (weeks 53) "one year ago") + (check (years 2.49) "2 years ago") ;; rounding down + (check (years 2.51) "3 years ago") ;; rounding down + )) + (ert-deftest mastodon-tl--byline-regular () "Should format the regular toot correctly." (let ((mastodon-tl--show-avatars-p nil) @@ -236,3 +275,18 @@ | (B) (F) Account 42 (@acct42@example.space) Boosted Account 43 (@acct43@example.space) original time ------------"))))) +(ert-deftest mastodon-tl--byline-timestamp-has-relative-display () + "Should display the timestamp with a relative time." + (let ((mastodon-tl--show-avatars-p nil) + (timestamp (cdr (assoc 'created_at mastodon-tl-test-base-toot)))) + (with-mock + (mock (date-to-time timestamp) => '(22782 21551)) + (mock (current-time) => '(22782 22000)) + (mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22") + + (let* ((formatted-string (mastodon-tl--byline mastodon-tl-test-base-toot)) + (timestamp-start (string-match "2999-99-99" formatted-string)) + (properties (text-properties-at timestamp-start formatted-string))) + (should (equal '(22782 21551) (plist-get properties 'timestamp))) + (should (string-equal "7 minutes ago" (plist-get properties 'display))))))) + |