date_add 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/csh -ef
  2. set prog = `basename $0`
  3. if ( ${#argv} < 2 ) then
  4. cat << EOF
  5. usage : $prog ccyymmdd increment
  6. EOF
  7. exit( 1 )
  8. endif
  9. # --- const ------------------------------------
  10. set ndaymon = ( '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31' )
  11. # --- arguments -----------------------------
  12. set datein = $1
  13. set ddays = $2
  14. # --- begin -------------------------------
  15. # extract parts without leading zeros:
  16. set year = `echo $datein | /usr/bin/cut -c 1-4`
  17. set month = `echo $datein | /usr/bin/cut -c 5-6`
  18. set day = `echo $datein | /usr/bin/cut -c 7-8`
  19. # on IBM, '09' means octal '9' which is an error ...
  20. if ( $month =~ 0? ) set month = `echo $month | /usr/bin/cut -c 2`
  21. if ( $day =~ 0? ) set day = `echo $day | /usr/bin/cut -c 2`
  22. # Initialize results without leading zeros:
  23. @ day2 = ${day}
  24. @ month2 = $month
  25. @ year2 = $year
  26. if ( $ddays > 0 ) then
  27. @ day2 = $day2 + $ddays
  28. while ( 1 )
  29. @ ndays = $ndaymon[$month2]
  30. if ( $month2 == 2 ) then
  31. if ( $year2 % 4 == 0 ) set ndays = 29
  32. if ( $year2 % 100 == 0 ) set ndays = 28
  33. if ( $year2 % 400 == 0 ) set ndays = 29
  34. endif
  35. if ( $day2 <= $ndays ) break
  36. @ day2 = $day2 - $ndays
  37. @ month2 = $month2 + 1
  38. if ( $month2 > 12 ) then
  39. @ year2 = $year2 + 1
  40. @ month2 = 1
  41. endif
  42. end
  43. else
  44. @ day2 = $day2 + $ddays
  45. while ( 1 )
  46. if ( $day2 >= 1 ) break
  47. @ month2 = $month2 - 1
  48. if ( $month2 == 0 ) then
  49. @ year2 = $year2 - 1
  50. @ month2 = 12
  51. endif
  52. @ ndays = $ndaymon[$month2]
  53. if ( $month2 == 2 ) then
  54. if ( $year2 % 4 == 0 ) set ndays = 29
  55. if ( $year2 % 100 == 0 ) set ndays = 28
  56. if ( $year2 % 400 == 0 ) set ndays = 29
  57. endif
  58. @ day2 = $day2 + $ndays
  59. end
  60. endif
  61. # Make resulting day, month, year 2 digits:
  62. if ( $day2 < 10 ) set day2 = "0${day2}"
  63. if ( $month2 < 10 ) set month2 = "0${month2}"
  64. # display the result:
  65. echo "${year2}${month2}${day2}"