Subject: bug#71572: [PATCH] seconds-to-string-approximate

From: JD Smith <[email-masked]>
Date: Thu, 11 Jul 2024 17:01:05 -0400

> On Jul 6, 2024, at 5:09 PM, Paul Eggert <[email-masked]> wrote:
> 
> On 7/6/24 21:29, JD Smith wrote:
>> do we universally avoid unicode in core files?
> 
> Not in comments, but even today it's wise to be cautious about generating user-visible Unicode characters like "½" when there's a simple ASCII substitute like ".5". Plus, why stop with ½? Why not also do ¼ and ¾?
> 
> It might be better to have an optional precision argument, defaulting to 0, specifying the number of digits of precision after the decimal point. Or something like that.

Thanks for the feedback.  Attached find an updated patch:

- HALF is dropped.
- PRECISION can now be specified as a whole-number of digits or a float <1.0 (e.g. 0.5)
- NEWS and doc entries.

Users who want ½ can always use precision=0.5 and edit the string after the fact.

Another attached file includes commands to produce and display a simple benchmark, as well as the example output (below).  

I see about a 10x performance difference between the standard seconds-to-string and the "bells and whistles" readable version.  It's still <35µs per delay for me, so formatting thousands of strings at once should be no problem.  Happy to take performance improvement ideas.

Current example:

     Delay (s)        s2s       s2s-r  s2s-ra  s2s-ra1  s2s-rah                  s2s-e   s2s-ea    s2s-ea1      s2s-ea3    s2s-eah
         0.000         0s   0 seconds      0s       0s       0s              0 seconds       0s         0s           0s         0s
         0.450   450.00ms   0 seconds      0s     0.4s     0.5s              0 seconds       0s       0.4s       0.450s       0.5s
         1.035      1.03s    1 second      1s       1s       1s               1 second       1s         1s       1.035s         1s
         2.380      2.38s   2 seconds      2s     2.4s     2.5s              2 seconds       2s       2.4s       2.380s       2.5s
         5.475      5.48s   5 seconds      5s     5.5s     5.5s              5 seconds       5s       5.5s       5.475s       5.5s
        12.593     12.59s  13 seconds     13s    12.6s    12.5s             13 seconds      13s      12.6s      12.593s      12.5s
        28.964     28.96s  29 seconds     29s      29s      29s             29 seconds      29s        29s      28.964s        29s
        66.616     66.62s    1 minute      1m     1.1m       1m     1 minute 7 seconds    1m 7s    1m 6.6s    1m 6.616s    1m 6.5s
       153.217      2.55m   3 minutes      3m     2.6m     2.5m   2 minutes 33 seconds   2m 33s   2m 33.2s   2m 33.217s     2m 33s
       352.399      5.87m   6 minutes      6m     5.9m       6m   5 minutes 52 seconds   5m 52s   5m 52.4s   5m 52.399s   5m 52.5s
       810.519     13.51m  14 minutes     14m    13.5m    13.5m  13 minutes 31 seconds  13m 31s  13m 30.5s  13m 30.519s  13m 30.5s
      1864.193     31.07m  31 minutes     31m    31.1m      31m   31 minutes 4 seconds   31m 4s   31m 4.2s   31m 4.193s     31m 4s
      4287.644     71.46m      1 hour      1h     1.2h       1h      1 hour 11 minutes   1h 11m   1h 11.5m   1h 11.461m   1h 11.5m
      9861.581      2.74h     3 hours      3h     2.7h     2.5h     2 hours 44 minutes   2h 44m   2h 44.4m   2h 44.360m   2h 44.5m
     22681.636      6.30h     6 hours      6h     6.3h     6.5h     6 hours 18 minutes   6h 18m     6h 18m   6h 18.027m     6h 18m
     52167.763     14.49h    14 hours     14h    14.5h    14.5h    14 hours 29 minutes  14h 29m  14h 29.5m  14h 29.463m  14h 29.5m
    119985.856      1.39d       1 day      1d     1.4d     1.5d          1 day 9 hours    1d 9h    1d 9.3h    1d 9.329h    1d 9.5h
    275967.469      3.19d      3 days      3d     3.2d       3d         3 days 5 hours    3d 5h    3d 4.7h    3d 4.658h    3d 4.5h
    634725.178      7.35d      1 week      1w       1w       1w                 1 week       1w    1w 0.3d    1w 0.346d    1w 0.5d
   1459867.909     16.90d     2 weeks      2w     2.4w     2.5w         2 weeks 3 days    2w 3d    2w 2.9d    2w 2.897d      2w 3d
   3357696.192     38.86d     1 month      1M     1.3M     1.5M         1 month 1 week    1M 1w    1M 1.2w    1M 1.204w      1M 1w
   7722701.241     89.38d    3 months      3M     2.9M       3M       2 months 4 weeks    2M 4w    2M 4.1w    2M 4.073w      2M 4w
  17762212.854    205.58d    7 months      7M     6.8M       7M       6 months 3 weeks    6M 3w    6M 3.3w    6M 3.280w    6M 3.5w
  40853089.565      1.29y      1 year      1Y     1.3Y     1.5Y        1 year 4 months    1Y 4M    1Y 3.5M    1Y 3.535M    1Y 3.5M
  93962105.999      2.98y     3 years      3Y       3Y       3Y      2 years 12 months   2Y 12M   2Y 11.7M   2Y 11.730M   2Y 11.5M
 216112843.798      6.85y     7 years      7Y     6.8Y       7Y      6 years 10 months   6Y 10M   6Y 10.2M   6Y 10.180M     6Y 10M
 497059540.736     15.75y    16 years     16Y    15.8Y      16Y      15 years 9 months   15Y 9M     15Y 9M   15Y 9.014M     15Y 9M
1143236943.694     36.23y    36 years     36Y    36.2Y      36Y      36 years 3 months   36Y 3M   36Y 2.7M   36Y 2.733M   36Y 2.5M

￼￼