date_diff 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #! /bin/sh
  2. # -----------------------------------------------------
  3. # init
  4. # -----------------------------------------------------
  5. # exit on error
  6. set -e
  7. # program name and location:
  8. call="$0 $*"
  9. case $0 in
  10. /* ) script=$0 ;;
  11. * ) script="`/bin/pwd`/$0" ;;
  12. esac
  13. bindir=`/usr/bin/dirname ${script}`
  14. prog=`/bin/basename ${script}`
  15. # -----------------------------------------------------
  16. # help
  17. # -----------------------------------------------------
  18. DisplayHelp ()
  19. {
  20. ${PAGER:-less} << EOF
  21. NAME
  22. $prog - return number of days between two dates
  23. USAGE
  24. $prog <ccyymmdd1> <ccyymmdd2>
  25. EOF
  26. exit 0
  27. }
  28. # err 'help text'
  29. err ()
  30. {
  31. echo "$1" 1>&2
  32. }
  33. # errit <exit-status>
  34. errit ()
  35. {
  36. err "$prog - ERROR - in call : ${call}"
  37. err "$prog - ERROR - use '$prog --help' for more info."
  38. exit $1
  39. }
  40. # -----------------------------------------------------
  41. # arguments
  42. # -----------------------------------------------------
  43. day1=''
  44. day2=''
  45. # extract arguments
  46. for arg in "$@" ; do
  47. case ${arg} in
  48. -h | --help ) DisplayHelp ;;
  49. * ) # task or (one of the) location(s)
  50. if [ -z "${day1}" ]; then
  51. day1=${arg}
  52. elif [ -z "${day2}" ]; then
  53. day2=${arg}
  54. else
  55. err "$prog - ERROR - unknown argument : ${arg}"
  56. errit 1
  57. fi
  58. ;;
  59. esac
  60. done
  61. # not complete ?
  62. if [ -z "${day2}" ]; then
  63. err "$prog - ERROR - missing arguments"
  64. errit 1
  65. fi
  66. # -----------------------------------------------------
  67. # begin
  68. # -----------------------------------------------------
  69. # day2 should be equal to or later than day1
  70. if [ ${day2} -lt ${day1} ]; then
  71. err "$prog - day2 should not be earlier than day1 :"
  72. err "$prog - ${day1} ${day2}"
  73. errit 1
  74. fi
  75. # count from day1 to day2
  76. day=${day1}
  77. nday=0
  78. while [ ${day} -ne ${day2} ]; do
  79. nday=`/usr/bin/expr ${nday} + 1`
  80. day=`${bindir}/date_add ${day} 1`
  81. done
  82. # return number of days
  83. echo ${nday}
  84. # ok
  85. exit 0
  86. # -----------------------------------------------------
  87. # end
  88. # -----------------------------------------------------