help-bash
[Top][All Lists]
Advanced

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

Re: Duration between two time stamps


From: Emanuele Torre
Subject: Re: Duration between two time stamps
Date: Thu, 19 Aug 2021 18:19:06 +0200

hancooper <hancooper@protonmail.com> wrote:
> Why does the following EPOCHREALTIME computation not work ?
>
> ta=$( $EPOCHREALTIME | tr ',' '.' )
> grep ...
> tb=$( $EPOCHREALTIME | tr ',' '.' )
> dt=$( ${tb}-${ta} | bc )
> printf '%s\n' "ta:$ta | tb:$tb | dt: $dt"

Because that is not the correct syntax, `$EPOCHREALTIME|tr' will try to
run the content of EPOCHREALTIME as a command and its output to tr(1).

You want to use:

 ta=$(<<< "$EPOCHREALTIME" tr ',' '.' )
 grep ...
 tb=$(<<< "$EPOCHREALTIME" tr ',' '.' )
 dt=$(<<< "${tb}-${ta}" bc )
 printf '%s\n' "ta:$ta | tb:$tb | dt: $dt"

Note that there is no need to waste time doing an expensive tr(1)
invocation (that will mess up your timing).  bash parameter expansions
can do the same same substitution as tr(1) much faster.

 ta=${EPOCHREALTIME//,/.}
 grep ...
 tb=${EPOCHREALTIME//,/.}
 dt=$(<<< "${tb}-${ta}" bc )
 printf '%s\n' "ta:$ta | tb:$tb | dt: $dt"

(Also i would use `[!0-9]' instead of `,' since the decimal separator
could be neither `.' nor `,' for all we know.)

As I told you in my first email that you read and replied to, but I
forgot to CC to the mailing list (oops).

On Thursday, August 19, 2021 3:02 PM CEST, Emanuele Torre
<torreemanuele6@gmail.com> wrote:
> hancooper <hancooper@protonmail.com> wrote:
>
> > The utilisation of tho comma `,` is very inconvenient for those who want to 
> > do time
> > computations. A change towards a period `.` would be the proper way to 
> > display the
> > variable.
>
> EPOCHREALTIME uses the locale's decimal separator and that is not always ",".
>
>  bash$ LC_NUMERIC=it_IT.UTF-8 declare -p EPOCHREALTIME
>  declare -- EPOCHREALTIME="1629377691,503828"
>  bash$ LC_NUMERIC=C declare -p EPOCHREALTIME
>  declare -- EPOCHREALTIME="1629377698.422459"
>
> If you want to get EPOCHREALTIME as an integer (natural number), you
> can simply use the following parameter substitution:
>
>  epochrealtime_int=${EPOCHREALTIME//[!0-9]}
>
> bash has no problem computing the difference of numbers that are this
> big, so there is no need to worry about that. :)
>
> Cheers,
>  emanuele6

You can simply convert EPOCHREALTIME to a natural number and do math
directly with bash without the need to use bc(1).

 ta=${EPOCHREALTIME//[!0-9]}
 grep ...
 tb=${EPOCHREALTIME//[!0-9]}
 (( dt = tb - ta ))
 printf '%s\n' "ta:$ta | tb:$tb | dt: $dt"
 # or, if you want the same format you were using before.
 echo "ta:${ta/%??????}.${ta: -6} | tb:${tb/%??????}.${tb: -6} | dt:
${dt/%??????}.${dt: -6}"


On 19/08/2021, hancooper <hancooper@protonmail.com> wrote:
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> On Thursday, August 19, 2021 2:47 PM, Chet Ramey <chet.ramey@case.edu>
> wrote:
>
>> On 8/19/21 1:10 AM, Tapani Tarvainen wrote:
>>
>> > Incidentally, why does ${var:offset} with a negative offset result in
>> > an empty string when the variable is shorter than the offset? E.g.,
>> > x=abc
>> > echo ${x: -6}
>> > I find that counterintuitive,
>>
>> Why? It's an out-of-bounds error.
>>
>> > and it'd actually be useful to
>> > be able to get "at most N characters from the end".
>>
>> If you want that, you can use ${#x} to get the length and work from
>> there.
>
> Why does the following EPOCHREALTIME computation not work ?
>
> ta=$( $EPOCHREALTIME | tr ',' '.' )
> grep ...
> tb=$( $EPOCHREALTIME | tr ',' '.' )
> dt=$( ${tb}-${ta} | bc )
> printf '%s\n' "ta:$ta | tb:$tb | dt: $dt"
>
> Thank you
>
>
>
>



reply via email to

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