help-bash
[Top][All Lists]
Advanced

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

Re: was there a more fine number than $SECONDS for the same purpose


From: Tapani Tarvainen
Subject: Re: was there a more fine number than $SECONDS for the same purpose
Date: Sun, 7 Nov 2021 15:57:30 +0200

On Sun, Nov 07, 2021 at 03:34:12PM +0200, Tapani Tarvainen 
(bash@tapanitarvainen.fi) wrote:

> On Sun, Nov 07, 2021 at 08:17:56AM -0500, Greg Wooledge (greg@wooledge.org) 
> wrote:
> 
> > You can store the value of EPOCHREALTIME in a different variable at the
> > start of your script, and then retrieve it again later on, and subtract
> > the two.  However, subtracting the two values will require forking an
> > external program, because bash can't do it.
> 
> Well bash can do it, if you save the values as microseconds.
> $EPOCHREALTIME always has six decimal points, it doesn't omit
> trainling zeroes, so it's pretty easy. E.g.,
> 
> START=${EPOCHREALTIME/[[:punct:]]/}
> ...
> NOW=$(( ${EPOCHREALTIME/[[:punct:]]/} - START ))

Or keep them as seconds and just do the math in microseconds,
here simplified by assuming period as decimal point:

START=$EPOCHREALTIME
...
STOP=$EPOCHREALTIME
MUSDIFF=$(( ${STOP/./} - ${START/./} ))
SDIFF=${MUSDIFF::-6}.${MUSDIFF: -6:6}

To cover locales with different decimal points, this should work:

...
RADIX=${EPOCHREALTIME//[[:digit:]]/}
MUSDIFF=$(( ${STOP/$RADIX/} - ${START/$RADIX/} ))
SDIFF=${MUSDIFF::-6}$RADIX${MUSDIFF: -6:6}

Incidentally, is there any easier way to determine the decimal
point in current locale?

-- 
Tapani Tarvainen



reply via email to

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