~mikini/hometools

30788f6c0c43145289eb9955bfc9fc155afd15b9 — Mikkel Kirkgaard Nielsen 4 months ago 637040b
stop_watch: Fix parsing of H, M & S to only include remainder

* move elapsed parsing into function to also be able print elapsed on exit
* also add output of day to elapsed
* add posibility to set start from argument
1 files changed, 44 insertions(+), 10 deletions(-)

M stop_watch
M stop_watch => stop_watch +44 -10
@@ 1,22 1,56 @@
#! /bin/bash

#(START=$(date +%s);echo -en "Start: $(date -d @$START)\n"; while true; do NOW=$(date +%s);ELAPSED=$(($NOW-$START));ELAPSED_H=$(($ELAPSED/3600));ELAPSED_M=$(($ELAPSED/60));ELAPSED_S=$(($ELAPSED-$ELAPSED_H*3600-$ELAPSED_M*60));echo -en "\e[GNow: $(date -d @$NOW), Elapsed: $ELAPSED_H:$ELAPSED_M:$ELAPSED_S (PT${ELAPSED_H}H${ELAPSED_M}M${ELAPSED_S}S)"; sleep 1; done;)
# Show a counting summary of elapsed time since invocation of the script, or some
# arbitrary start date in the past given as argument. Argument can be a free
# form date parsable by GNU coreutils (see 'info coreutils "Date input formats"').

FAIL=0

# print a textual representation of the elapsed time between the Unix Epoch
# timestamps given as parameters as days, hours, minutes, seconds
function print_elapsed () {
    SECONDS_PR_MINUTE=60
    SECONDS_PR_HOUR=$((60*$SECONDS_PR_MINUTE))
    SECONDS_PR_DAY=$((24*$SECONDS_PR_HOUR))

    ELAPSED=$(($2-$1))
    ELAPSED_D=$(($ELAPSED/$SECONDS_PR_DAY))
    ELAPSED_REMAINDER=$(($ELAPSED-$ELAPSED_D*$SECONDS_PR_DAY))
    ELAPSED_H=$(($ELAPSED_REMAINDER/$SECONDS_PR_HOUR))
    ELAPSED_REMAINDER=$(($ELAPSED_REMAINDER-$ELAPSED_H*$SECONDS_PR_HOUR))
    ELAPSED_M=$(($ELAPSED_REMAINDER/$SECONDS_PR_MINUTE))
    ELAPSED_S=$(($ELAPSED_REMAINDER-$ELAPSED_M*$SECONDS_PR_MINUTE))
    # TODO: ISO period should be printed in a fixed width box of 2 characters
    echo -n "$ELAPSED_D days, $ELAPSED_H:$ELAPSED_M:$ELAPSED_S (P${ELAPSED_D}DT${ELAPSED_H}H${ELAPSED_M}M${ELAPSED_S}S)"
}

function exith () {
    STOP=$(date +%s)
    echo -en "\nStop : $(date -d @$STOP)"
    if [ ! $FAIL -eq 1 ]; then
	echo -e "\nStop : $(date -d @$STOP) ($STOP)"
	echo -n "Elapsed from start to stop: "
	print_elapsed $START $STOP
    fi
}

trap exith EXIT

START=$(date +%s)
echo -en "Start: $(date -d @$START)\n"
while true; do 
if [ -z "$1" ]; then
    START=$(date +%s)
else
    START=$(date +%s --date "$1")
    if [ $? -eq 1 ]; then
	echo "Invalid start date argument: $START. Supply no argument for current time. Aborting."
	FAIL=1
	exit 27
    fi
fi

echo -en "Start: $(date -d @$START) ($START)\n"
while true; do
    NOW=$(date +%s)
    ELAPSED=$(($NOW-$START))
    ELAPSED_H=$(($ELAPSED/3600))
    ELAPSED_M=$(($ELAPSED/60))
    ELAPSED_S=$(($ELAPSED-$ELAPSED_H*3600-$ELAPSED_M*60))
    echo -en "\e[GNow  : $(date -d @$NOW), Elapsed: $ELAPSED_H:$ELAPSED_M:$ELAPSED_S (PT${ELAPSED_H}H${ELAPSED_M}M${ELAPSED_S}S)"
    echo -en "\e[GNow  : $(date -d @$NOW), elapsed: "
    print_elapsed $START $NOW
    echo -n "      " # remove potential leftovers from 23:59:59 -> 0:0:0 times two
    sleep 1
done