Samuel Santiago
Samuel Santiago

Reputation: 47

Calculate the difference in milliseconds between 2 given time

I have a log and in this log I have this times: 14:03:26,599 14:03:26,788 14:03:26,924 14:03:27,125

How calculate the difference between two of theses times in milliseconds? I tryed:

LOGTIME1=14:03:26,599
LOGTIME2=14:03:26,788
CONVERT1=$(date +d $LOGTIME1 +%s.%N)
CONVERT2=$(date +d $LOGTIME1 +%s.%N)
TOTAL=$(CONVERT2 - CONVERT1)

But I do not get this to work. I recive the error: date: extra operand ‘14:03:26,599’ I belive is because "," and 599... But I do not know how handle it.

Upvotes: 1

Views: 494

Answers (1)

anubhava
anubhava

Reputation: 784908

You may use:

bc -l <<< "$(date -d "$LOGTIME2" '+%s.%N') - $(date -d "$LOGTIME1" '+%s.%N')"

.189000000

Note that you have to use -d in date and use bc -l for floating point arithmetic and bash only does integer arithmetic.

Upvotes: 3

Related Questions