I need time elapsed since $var
$var= [19:11:27 19:34:09]
$var
was created from
date "+[%y:%m:%d %T]"
I need time elapsed since $var
$var= [19:11:27 19:34:09]
$var
was created from
date "+[%y:%m:%d %T]"
If mktime
function of awk
is available, how about:
awk -v var='[19:11:28 14:34:09]' '
BEGIN {
split(var, a, "[^0-9]")
elapsed = systime() - mktime(a[2]+2000" "a[3]" "a[4]" "a[5]" "a[6]" "a[7])
d = int(elapsed / 86400)
h = int((elapsed - d * 86400) / 3600)
m = int((elapsed - d * 86400 - h * 3600) / 60)
printf("%d days %d hours %d mins passed.\n", d, h, m)
}'
Few alternative,
If you can change the setting of var
:
You are better using the SECONDS
bash variable. You can then use bash math to calculate difference, and format the output.
# Instead of var=...
START=$SECONDS
... do something that take long time
... to executed
FINISH=$SECONDS
ELAPSED=$((FINISH-START))
If you can NOT change the setting of var
:
You will have to convert var to a format that date will accept, and use the methods suggested in the comments. Minimal effort to convert to ISO YYYY-MM-DD HH:MM:SS
VX=20${var/:/-} # Replace first : with -, add 2000 to the yy part
VX=${var/:/-} # Replace Second : with -
START=$(date -d "$VX" +'%s')
FINISH=(date +'%s')
ELAPSED=$((FINISH-START))