Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # DeltaT
- # - Contagem de tempo entre processos
- # - Precisão de segundos
- # - Pode ser usado para contagem "aninhada" de tempo
- # - Aceita uma quantidade "offset" opcional de segundos
- # - Devolve:
- # o timestamp e a data e hora de início e fim da contagem
- # a variação de tempo em segundos e em hh:mm:ss
- #
- # Arkanon <arkanon@lsd.org.br>
- # 2010/10/01 (Sex) 18:47:50 (BRS)
- # 2010/03/18 (Qui) 15:54:42 (BRS)
- #
- # EXEMPLO
- #
- # Um script com
- # -------------
- # t_time=$(deltat start)
- #
- # echo "sleeping 3s..."
- # time_1=$(deltat start)
- # sleep 3
- # time_1=$(deltat stop "$time_1")
- #
- # time_2=$(deltat start)
- # read -p "prompt: " p
- # echo -e "\nyou said: $p\n"
- # time_2=$(deltat stop "$time_2")
- #
- # t_time=$(deltat stop "$t_time")
- #
- # echo -e "Tempo do sleep \n$time_1\n"
- # echo -e "Tempo do prompt\n$time_2\n"
- # echo -e "Tempo total \n$t_time"
- #
- # Devolve
- # -------
- # sleeping 3s...
- # prompt: oi
- #
- # you said: oi
- #
- # Tempo do sleep
- # OFFSET 0
- # START 1285969587 2010/10/01 Sex 18:46:27 BRS
- # STOP 1285969590 2010/10/01 Sex 18:46:30 BRS
- # DELTA 3 00:00:03
- #
- # Tempo do prompt
- # OFFSET 0
- # START 1285969590 2010/10/01 Sex 18:46:30 BRS
- # STOP 1285969597 2010/10/01 Sex 18:46:37 BRS
- # DELTA 7 00:00:07
- #
- # Tempo total
- # OFFSET 0
- # START 1285969587 2010/10/01 Sex 18:46:27 BRS
- # STOP 1285969597 2010/10/01 Sex 18:46:37 BRS
- # DELTA 10 00:00:10
- fmt="%s\t%Y/%m/%d %a %H:%M:%S %Z"
- now=$(date +'%m/%d/%Y %H:%M:%S')
- ts=$(date -d "$now" +"%s")
- point=$1
- if [ "$point" = "start" ] && ( [ $# = 1 ] || [ $# = 2 ] )
- then
- [ $# = 2 ] && offset=$2 || offset=0
- echo -e "OFFSET\t$(printf '%10d' $offset)"
- echo -e " START\t"$(LANG=pt_BR date -d "$now" +"$fmt")
- else
- offset=$(echo "$2" | grep "OFFSET" | cut -f2)
- ts0=$(echo "$2" | grep "START" | cut -f2)
- if [ "$point" = "stop" ] && [ $# = 2 ] && [ "$ts" -ge "$ts0" ]
- then
- diffts=$(printf '%10d' $[$ts-$ts0+$offset])
- time_t=$diffts
- time_s=$[$time_t%60]
- time_t=$[$time_t/60]
- time_m=$[$time_t%60]
- time_h=$[$time_t/60]
- [ $time_s -le 9 ] && time_s="0$time_s"
- [ $time_m -le 9 ] && time_m="0$time_m"
- [ $time_h -le 9 ] && time_h="0$time_h"
- delta="$time_h:$time_m:$time_s"
- echo "$2"
- echo -e " STOP\t"$(LANG=pt_BR date -d "$now" +"$fmt")
- echo -e " DELTA\t$diffts\t$delta"
- else
- echo 'Usage: var=`deltat start [ts offset]`; cmd[; cmd; ...]; var=`deltat stop "$var"`; echo $var'
- fi
- fi
- # [EOF]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement