Advertisement
arkanon

DeltaT - 2010/10/01 (Sex) 18:47:50 (BRS)

Oct 1st, 2010
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.71 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # DeltaT
  4. #   - Contagem de tempo entre processos
  5. #   - Precisão de segundos
  6. #   - Pode ser usado para contagem "aninhada" de tempo
  7. #   - Aceita uma quantidade "offset" opcional de segundos
  8. #   - Devolve:
  9. #       o timestamp e a data e hora de início e fim da contagem
  10. #       a variação de tempo em segundos e em hh:mm:ss
  11. #
  12. #   Arkanon <arkanon@lsd.org.br>
  13. #   2010/10/01 (Sex) 18:47:50 (BRS)
  14. #   2010/03/18 (Qui) 15:54:42 (BRS)
  15. #
  16. # EXEMPLO
  17. #
  18. #   Um script com
  19. #   -------------
  20. #     t_time=$(deltat start)
  21. #
  22. #     echo "sleeping 3s..."
  23. #     time_1=$(deltat start)
  24. #     sleep 3
  25. #     time_1=$(deltat stop "$time_1")
  26. #
  27. #     time_2=$(deltat start)
  28. #     read -p "prompt: " p
  29. #     echo -e "\nyou said: $p\n"
  30. #     time_2=$(deltat stop "$time_2")
  31. #
  32. #     t_time=$(deltat stop "$t_time")
  33. #
  34. #     echo -e "Tempo do sleep \n$time_1\n"
  35. #     echo -e "Tempo do prompt\n$time_2\n"
  36. #     echo -e "Tempo total    \n$t_time"
  37. #
  38. #   Devolve
  39. #   -------
  40. #     sleeping 3s...
  41. #     prompt: oi
  42. #
  43. #     you said: oi
  44. #
  45. #     Tempo do sleep
  46. #     OFFSET             0
  47. #      START    1285969587  2010/10/01 Sex 18:46:27 BRS
  48. #       STOP    1285969590  2010/10/01 Sex 18:46:30 BRS
  49. #      DELTA             3  00:00:03
  50. #
  51. #     Tempo do prompt
  52. #     OFFSET             0
  53. #      START    1285969590  2010/10/01 Sex 18:46:30 BRS
  54. #       STOP    1285969597  2010/10/01 Sex 18:46:37 BRS
  55. #      DELTA             7  00:00:07
  56. #
  57. #     Tempo total
  58. #     OFFSET             0
  59. #      START    1285969587  2010/10/01 Sex 18:46:27 BRS
  60. #       STOP    1285969597  2010/10/01 Sex 18:46:37 BRS
  61. #      DELTA            10  00:00:10
  62.  
  63.   fmt="%s\t%Y/%m/%d %a %H:%M:%S %Z"
  64.   now=$(date +'%m/%d/%Y %H:%M:%S')
  65.    ts=$(date -d "$now" +"%s")
  66.  
  67.   point=$1
  68.   if [ "$point" = "start" ] && ( [ $# = 1 ] || [ $# = 2 ] )
  69.   then
  70.     [ $# = 2 ] && offset=$2 || offset=0
  71.     echo -e "OFFSET\t$(printf '%10d' $offset)"
  72.     echo -e " START\t"$(LANG=pt_BR date -d "$now" +"$fmt")
  73.   else
  74.     offset=$(echo "$2" | grep "OFFSET" | cut -f2)
  75.        ts0=$(echo "$2" | grep "START"  | cut -f2)
  76.     if [ "$point" = "stop" ] && [ $# = 2 ] && [ "$ts" -ge "$ts0" ]
  77.     then
  78.       diffts=$(printf '%10d' $[$ts-$ts0+$offset])
  79.       time_t=$diffts
  80.       time_s=$[$time_t%60]
  81.       time_t=$[$time_t/60]
  82.       time_m=$[$time_t%60]
  83.       time_h=$[$time_t/60]
  84.       [ $time_s -le 9 ] && time_s="0$time_s"
  85.       [ $time_m -le 9 ] && time_m="0$time_m"
  86.       [ $time_h -le 9 ] && time_h="0$time_h"
  87.       delta="$time_h:$time_m:$time_s"
  88.       echo "$2"
  89.       echo -e "  STOP\t"$(LANG=pt_BR date -d "$now" +"$fmt")
  90.       echo -e " DELTA\t$diffts\t$delta"
  91.     else
  92.       echo 'Usage: var=`deltat start [ts offset]`; cmd[; cmd; ...]; var=`deltat stop "$var"`; echo $var'
  93.     fi
  94.   fi
  95.  
  96. # [EOF]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement