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: Tapani Tarvainen
Subject: Re: Duration between two time stamps
Date: Thu, 19 Aug 2021 08:10:34 +0300

On Thu, Aug 19, 2021 at 11:23:22AM +0900, Koichi Murase 
(myoga.murase@gmail.com) wrote:

> In Bash 5.0+, you may use the special shell variables, EPOCHSECONDS
> (for granularity of seconds) or EPOCHREALTIME (for finer granularity).
> 
> A=$EPOCHREALTIME
> your_search_command
> B=$EPOCHREALTIME

Yes. And it's worth noting that $EPOCHREALTIME does not drop
trailing zeroes, there're always six digits after the decimal point.

So if you're happy with the difference in microseconds, you can
simply discard the decimal points and subtract the resulting
microsecond values:

echo $((${B/.}-${A/.}))

If you want it in seconds but don't want to spawn bc or the like, you
can add the decimal point back, e.g., like this:

udiff1=$((${B/.}-${A/.}+1000000))
sec1=${udiff1::-6}
echo $((sec1-1)).${udiff1#$sec1}

An extra second is added and later substracted to deal with leading
zeroes in case the time difference is less than a second.

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, and it'd actually be useful to
be able to get "at most N characters from the end".

-- 
Tapani Tarvainen



reply via email to

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