camscape - for excellent IT solutions itkb.ro - IT knowledge base

bash :: functie bash pentru formatarea timpului petrecut intr-un proces

Cristian
Cristian M.
TitleFunctie bash pentru formatarea timpului petrecut intr-un proces
Tagsbash, formatare timp, timp folosit
Desc.Functie bash pentru formatarea timpului petrecut intr-un proces
CodeKBSH0002 v1.0
Date14 septembrie 2012
Functia primeste 2 argumente start si sfarsit in formatul "seconds since" (date +%s).  Intoarce un text de forma:

ELAPSED_DAYS days and ELAPSED_TIME (from START_DATE until END_DATE).

Util pentru rapoarte de activitate pe mail.


function DisplayTimeElapsed {
    # Function that returns a string of the form
    # "ELAPSED_DAYS days and ELAPSED_TIME (from START_DATE until END_DATE)"
    #
    # Param:
    #   - start date in seconds (from date +%s)
    #   - end date in seconds
    #
    # Return:
    #   - string of the form "ELAPSED_DAYS days and ELAPSED_TIME (from START_DATE until END_DATE)"
    #
    # CAMSCAPE SERVICES GPLv2
    # http://www.camscape.ro
    #
    SECONDS_START=$1
    SECONDS_END=$2
    SECONDS_ELAPSED=$((SECONDS_END-SECONDS_START))
    ELAPSED_DAYS=`date -u --date=@$SECONDS_ELAPSED +%d`
    ELAPSED_DAYS=$((ELAPSED_DAYS-1))
    ELAPSED_TIME=`date -u --date=@$SECONDS_ELAPSED +%H:%M:%S`
    DATE_START=`date -u --date=@$SECONDS_START +"%d.%m.%Y %H:%M"`
    DATE_END=`date -u --date=@$SECONDS_END +"%d.%m.%Y %H:%M"`
    if [ $ELAPSED_DAYS -gt 0 ]; then
        echo "$ELAPSED_DAYS days and $ELAPSED_TIME (from $DATE_START until $DATE_END)"
    else
        echo "$ELAPSED_TIME (from $DATE_START until $DATE_END)"
    fi
}