emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/marginalia 6d3d10f: marginalia--time-relative: Simplify


From: ELPA Syncer
Subject: [elpa] externals/marginalia 6d3d10f: marginalia--time-relative: Simplify and optimize
Date: Wed, 28 Jul 2021 17:57:11 -0400 (EDT)

branch: externals/marginalia
commit 6d3d10f7bca86d0135c4420ea3cd05ee5db45e23
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    marginalia--time-relative: Simplify and optimize
---
 marginalia.el | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/marginalia.el b/marginalia.el
index 981790f..6173be8 100644
--- a/marginalia.el
+++ b/marginalia.el
@@ -833,28 +833,24 @@ These annotations are skipped for remote paths."
         (push attrs marginalia--fontified-file-attributes)
         attrs)))
 
-(defconst marginalia--time-relative-units
-  '((?s . "sec")
-    (?m . "min")
-    (?h . "hour")
-    (?d . "day")
-    (?y . "year"))
-  "Expansions of the short units used by function `seconds-to-string'.
-
-This is used in `marginalia--time-relative'.")
-
+(defconst marginalia--time-relative
+  `((100 "sec" 1)
+    (,(* 60 100) "min" 60.0)
+    (,(* 3600 30) "hour" 3600.0)
+    (,(* 3600 24 400) "day" ,(* 3600.0 24.0))
+    (nil "year" ,(* 365.25 24 3600)))
+  "Formatting used by the function `marginalia--time-relative'.")
+
+;; Taken from `seconds-to-string'.
 (defun marginalia--time-relative (time)
   "Format TIME as a relative age."
-  (replace-regexp-in-string
-   "\\`\\([0-9]+\\)\\.[0-9]+\\([a-z]\\)\\'"
-   (lambda (age)
-     (concat (match-string 1 age) " "
-             (or (cdr (assq (aref age (match-beginning 2)) 
marginalia--time-relative-units))
-                 (match-string 2 age))
-             (unless (string= "1" (match-string 1 age))
-               "s")
-             " ago"))
-   (seconds-to-string (float-time (time-since time)))))
+  (setq time (float-time (time-since time)))
+  (if (<= time 0)
+      "0 secs ago"
+    (let ((sts marginalia--time-relative) here)
+      (while (and (car (setq here (pop sts))) (<= (car here) time)))
+      (setq time (round time (caddr here)))
+      (format "%s %s%s ago" time (cadr here) (if (= time 1) "" "s")))))
 
 (defun marginalia--time-absolute (time)
   "Format TIME as an absolute age."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]