From: JD Smith <jdtsmith@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Adam Porter <adam@alphapapa.net>,
71572@debbugs.gnu.org, jonas@bernoul.li,
Paul Eggert <eggert@cs.ucla.edu>
Subject: bug#71572: [PATCH] seconds-to-string-approximate
Date: Thu, 4 Jul 2024 11:27:41 -0400 [thread overview]
Message-ID: <1FFF1E0D-A0E0-4EB9-AC97-AEB0BFADFBBC@gmail.com> (raw)
In-Reply-To: <86msmxafog.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 4718 bytes --]
> On Jul 4, 2024, at 1:29 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>>
>> Here's a comparison among:
>>
>> - the current seconds-to-string
>> - mastodon-tl--human-duration
>> - mastodon with a 3600s resolution
>> - mastodon with 1yr "resolution"
>> - the new seconds-to-string with option READABLE=t
>> - new seconds-to-string with abbreviated units and half unit resolution
<snip>
>
> Basically, this shows that:
>
> . mastodon truncates where seconds-to-string rounds
I definitely think rounding makes more sense for a readable unit; consider 2.98y.
> . seconds-to-string lacks the "1 hour 11 min" output format
> . seconds-to-string sometimes produces inaccurate results, as in
> 5.5 => 5.48s
> The last item worries me: can we fix this, please?
The latter issue results simply from my truncation of the initial delay column to 1 decimal digit. E.g. .45s * 2.3^3 = 5.47515s, so s2s has it right.
> The second item sounds like a useful feature, so maybe an optional
> behavior could provide it as well?
I've expanded the call format to (delay &optional readable abbrev half) , with READABLE='expanded an option to get the "1 hour 11 min" expanded format, i.e. one larger and one smaller unit (if appropriate). Patch below.
Updated output at the same delays for the various new options (r: readable, e: readable=expanded, a: abbrev, h: half):
Delay (s) s2s s2s-r s2s-ra s2s-rah s2s-e s2s-ea s2s-eah
0.45 450.00ms 0 seconds 0s ½s 0 seconds 0s ½s
1.03 1.03s 1 second 1s 1s 1 second 1s 1s
2.38 2.38s 2 seconds 2s 2½s 2 seconds 2s 2½s
5.48 5.48s 5 seconds 5s 5½s 5 seconds 5s 5½s
12.59 12.59s 13 seconds 13s 12½s 13 seconds 13s 12½s
28.96 28.96s 29 seconds 29s 29s 29 seconds 29s 29s
66.62 66.62s 1 minute 1m 1m 1 minute 7 seconds 1m 7s 1m 6½s
153.22 2.55m 3 minutes 3m 2½m 2 minutes 33 seconds 2m 33s 2m 33s
352.40 5.87m 6 minutes 6m 6m 5 minutes 52 seconds 5m 52s 5m 52½s
810.52 13.51m 14 minutes 14m 13½m 13 minutes 31 seconds 13m 31s 13m 30½s
1864.19 31.07m 31 minutes 31m 31m 31 minutes 4 seconds 31m 4s 31m 4s
4287.64 71.46m 1 hour 1h 1h 1 hour 11 minutes 1h 11m 1h 11½m
9861.58 2.74h 3 hours 3h 2½h 2 hours 44 minutes 2h 44m 2h 44½m
22681.64 6.30h 6 hours 6h 6½h 6 hours 18 minutes 6h 18m 6h 18m
52167.76 14.49h 14 hours 14h 14½h 14 hours 29 minutes 14h 29m 14h 29½m
119985.86 1.39d 1 day 1d 1½d 1 day 9 hours 1d 9h 1d 9½h
275967.47 3.19d 3 days 3d 3d 3 days 5 hours 3d 5h 3d 4½h
634725.18 7.35d 1 week 1w 1w 7 days 7d 1w ½d
1459867.91 16.90d 2 weeks 2w 2½w 2 weeks 3 days 2w 3d 2w 3d
3357696.19 38.86d 1 month 1M 1½M 1 month 1 week 1M 1w 1M 1w
7722701.24 89.38d 3 months 3M 3M 2 months 4 weeks 2M 4w 2M 4w
17762212.85 205.58d 7 months 7M 7M 6 months 3 weeks 6M 3w 6M 3½w
40853089.56 1.29y 1 year 1Y 1½Y 1 year 4 months 1Y 4M 1Y 3½M
93962106.00 2.98y 3 years 3Y 3Y 2 years 12 months 2Y 12M 2Y 11½M
216112843.80 6.85y 7 years 7Y 7Y 6 years 10 months 6Y 10M 6Y 10M
497059540.74 15.75y 16 years 16Y 16Y 15 years 9 months 15Y 9M 15Y 9M
This is produced with:
(concat
(format "%12s %10s %10s %6s %7s %21s %7s %s\n"
"Delay (s)" "s2s" "s2s-r" "s2s-ra" "s2s-rah" "s2s-e" "s2s-ea" "s2s-eah" )
(cl-loop for s = 0.45 then (* s 2.3) while (< s (* 365.25 24 3600 22))
concat (format "%12.2f %10s %10s %6s %7s %21s %7s %s\n" s
(seconds-to-string s)
(seconds-to-string s 'readable)
(seconds-to-string s 'readable 'abbrev)
(seconds-to-string s 'readable 'abbrev 'half)
(seconds-to-string s 'expanded)
(seconds-to-string s 'expanded 'abbrev)
(seconds-to-string s 'expanded 'abbrev 'half))))

[-- Attachment #2.1: Type: text/html, Size: 10366 bytes --]
[-- Attachment #2.2: time-data-readable-seconds-2.patch --]
[-- Type: application/octet-stream, Size: 2991 bytes --]
--- time-date.el 2024-06-22 21:51:21
+++ time-date_new.el 2024-07-04 11:16:22
@@ -406,10 +406,62 @@
(list (* 3600 24 400) "d" (* 3600.0 24.0))
(list nil "y" (* 365.25 24 3600)))
"Formatting used by the function `seconds-to-string'.")
+
+(defvar seconds-to-string-readable
+ `(("Y" "year" "years" ,(round (* 60 60 24 365.2425)))
+ ("M" "month" "months" ,(round (* 60 60 24 30.436875)))
+ ("w" "week" "weeks" ,(* 60 60 24 7))
+ ("d" "day" "days" ,(* 60 60 24))
+ ("h" "hour" "hours" ,(* 60 60))
+ ("m" "minute" "minutes" 60)
+ ("s" "second" "seconds" 1))
+ "Formatting used by the function `seconds-to-string' with READABLE set.")
+
;;;###autoload
-(defun seconds-to-string (delay)
- "Convert the time interval in seconds to a short string."
- (cond ((> 0 delay) (concat "-" (seconds-to-string (- delay))))
+(defun seconds-to-string (delay &optional readable abbrev half)
+ "Convert time interval DELAY (in seconds) to a short string.
+By default, the returned string has two decimal precision in the
+smallest unit that is larger than DELAY from the variable
+`seconds-to-string'. If READABLE is non-nil, convert DELAY into
+a readable string, using the information in the variable
+`seconds-to-string-readable'. If it is the symbol `expanded',
+use two units to describe DELAY, if appropriate. E.g. \"1 hour
+32 minutes\". If ABBREV is non-nil, abbreviate the readable
+units. If HALF is non-nil, round the smallest unit displayed to
+the nearest half unit."
+ (cond ((> 0 delay) (concat "-" (seconds-to-string (- delay) readable half)))
+ (readable
+ (let* ((stsa seconds-to-string-readable)
+ (expanded (eq readable 'expanded))
+ (round-to (if half 0.5 1))
+ (padding (if abbrev "" " "))
+ here cnt cnt-pre here-pre)
+ (if (= (round delay round-to) 0)
+ (format "0%s" (if abbrev "s" " seconds"))
+ (while (and (setq here (pop stsa)) stsa
+ (< (/ delay (nth 3 here)) 1)))
+ (or (and expanded stsa
+ (progn
+ (setq here-pre here here (car stsa)
+ cnt-pre (floor (/ (float delay) (nth 3 here-pre)))
+ cnt (round (/ (- (float delay) (* cnt-pre (nth 3 here-pre)))
+ (nth 3 here))
+ round-to))
+ (if (> cnt 0) t (setq here-pre nil))))
+ (setq cnt (round (/ (float delay) (nth 3 here)) round-to)))
+ (cl-labels
+ ((unit (cnt here &optional half)
+ (cond (abbrev (car here))
+ ((<= cnt (if half 2 1)) (nth 1 here))
+ (t (nth 2 here)))))
+ (concat
+ (when here-pre
+ (concat (number-to-string cnt-pre) padding
+ (unit cnt-pre here-pre) " "))
+ (let ((c (if half (/ cnt 2) cnt)))
+ (if (> c 0) (number-to-string c) ""))
+ (if (and half (= (mod cnt 2) 1)) "½" "")
+ padding (unit cnt here half))))))
((= 0 delay) "0s")
(t (let ((sts seconds-to-string) here)
(while (and (car (setq here (pop sts)))
[-- Attachment #2.3: Type: text/html, Size: 212 bytes --]
next prev parent reply other threads:[~2024-07-04 15:27 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-15 17:24 bug#71573: [PATCH] seconds-to-string-approximate JD Smith
2024-06-15 17:36 ` Eli Zaretskii
2024-06-17 6:20 ` bug#71573: Related functions from ts.el Adam Porter
2024-06-22 10:55 ` Stefan Kangas
2024-06-22 21:54 ` Adam Porter
2024-06-22 8:45 ` bug#71572: [PATCH] seconds-to-string-approximate Eli Zaretskii
2024-06-22 21:56 ` Adam Porter
2024-06-22 23:42 ` Paul Eggert
2024-06-23 2:16 ` JD Smith
2024-07-04 5:29 ` Eli Zaretskii
2024-07-04 6:04 ` Eli Zaretskii
2024-07-04 7:09 ` Paul Eggert
2024-07-06 19:29 ` JD Smith
2024-07-06 21:09 ` Paul Eggert
2024-07-11 21:01 ` JD Smith
2024-07-04 15:27 ` JD Smith [this message]
2024-07-04 15:59 ` Eli Zaretskii
2024-07-04 17:16 ` JD Smith
2024-07-04 18:06 ` Eli Zaretskii
2024-07-04 16:36 ` Ihor Radchenko
2024-07-04 17:23 ` JD Smith
2024-07-04 17:57 ` Ihor Radchenko
2024-06-23 5:13 ` Eli Zaretskii
2024-07-03 20:32 ` JD Smith
2024-07-04 5:29 ` Eli Zaretskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1FFF1E0D-A0E0-4EB9-AC97-AEB0BFADFBBC@gmail.com \
--to=jdtsmith@gmail.com \
--cc=71572@debbugs.gnu.org \
--cc=adam@alphapapa.net \
--cc=eggert@cs.ucla.edu \
--cc=eliz@gnu.org \
--cc=jonas@bernoul.li \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.