| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # days-between.sh: Number of days between two dates. |
| 3 | # Usage: ./days-between.sh [M]M/[D]D/YYYY [M]M/[D]D/YYYY |
| 4 | # |
| 5 | # Note: Script modified to account for changes in Bash, v. 2.05b +, |
| 6 | #+ that closed the loophole permitting large negative |
| 7 | #+ integer return values. |
| 8 | |
| 9 | ARGS=2 # Two command-line parameters expected. |
| 10 | E_PARAM_ERR=85 # Param error. |
| 11 | |
| 12 | REFYR=1600 # Reference year. |
| 13 | CENTURY=100 |
| 14 | DIY=365 |
| 15 | ADJ_DIY=367 # Adjusted for leap year + fraction. |
| 16 | MIY=12 |
| 17 | DIM=31 |
| 18 | LEAPCYCLE=4 |
| 19 | |
| 20 | MAXRETVAL=255 # Largest permissible |
| 21 | #+ positive return value from a function. |
| 22 | |
| 23 | diff= # Declare global variable for date difference. |
| 24 | value= # Declare global variable for absolute value. |
| 25 | day= # Declare globals for day, month, year. |
| 26 | month= |
| 27 | year= |
| 28 | |
| 29 | |
| 30 | Param_Errorscip-shell shell abs-guide 10-4 Param_Error(). () # Command-line parameters wrong. |
| 31 | { |
| 32 | echoscip-shell system . . echo. "Usage: `basenamescip-shell system . . basename. $0` [M]M/[D]D/YYYY [M]M/[D]D/YYYY" |
| 33 | echoscip-shell system . . echo. " (date must be after 1/3/1600)" |
| 34 | exit $E_PARAM_ERR |
| 35 | } |
| 36 | |
| 37 | |
| 38 | Parse_Datescip-shell shell abs-guide 10-4 Parse_Date(). () # Parse date from command-line params. |
| 39 | { |
| 40 | month=${1%%/**} |
| 41 | dm=${1%/**} # Day and month. |
| 42 | day=${dm#*/} |
| 43 | let "year = `basenamescip-shell system . . basename. $1`" # Not a filename, but works just the same. |
| 44 | } |
| 45 | |
| 46 | |
| 47 | check_datescip-shell shell abs-guide 10-4 check_date(). () # Checks for invalid date(s) passed. |
| 48 | { |
| 49 | [scip-shell system . . `[`. "$day" -gt "$DIM" ] || [scip-shell system . . `[`. "$month" -gt "$MIY" ] || |
| 50 | [scip-shell system . . `[`. "$year" -lt "$REFYR" ] && Param_Errorscip-shell shell abs-guide 10-4 Param_Error(). |
| 51 | # Exit script on bad value(s). |
| 52 | # Uses or-list / and-list. |
| 53 | # |
| 54 | # Exercise: Implement more rigorous date checking. |
| 55 | } |
| 56 | |
| 57 | |
| 58 | strip_leading_zeroscip-shell shell abs-guide 10-4 strip_leading_zero(). () # Better to strip possible leading zero(s) |
| 59 | { #+ from day and/or month |
| 60 | return ${1#0} #+ since otherwise Bash will interpret them |
| 61 | } #+ as octal values (POSIX.2, sect 2.9.2.1). |
| 62 | |
| 63 | |
| 64 | day_indexscip-shell shell abs-guide 10-4 day_index(). () # Gauss' Formula: |
| 65 | { # Days from March 1, 1600 to date passed as param. |
| 66 | # ^^^^^^^^^^^^^ |
| 67 | day=$1 |
| 68 | month=$2 |
| 69 | year=$3 |
| 70 | |
| 71 | let "month = $month - 2" |
| 72 | if [scip-shell system . . `[`. "$month" -le 0 ] |
| 73 | then |
| 74 | let "month += 12" |
| 75 | let "year -= 1" |
| 76 | fi |
| 77 | |
| 78 | let "year -= $REFYR" |
| 79 | let "indexyr = $year / $CENTURY" |
| 80 | |
| 81 | |
| 82 | let "Days = $DIY*$year + $year/$LEAPCYCLE - $indexyr \ |
| 83 | + $indexyr/$LEAPCYCLE + $ADJ_DIY*$month/$MIY + $day - $DIM" |
| 84 | # For an in-depth explanation of this algorithm, see |
| 85 | #+ http://weblogs.asp.net/pgreborio/archive/2005/01/06/347968.aspx |
| 86 | |
| 87 | |
| 88 | echoscip-shell system . . echo. $Days |
| 89 | |
| 90 | } |
| 91 | |
| 92 | |
| 93 | calculate_differencescip-shell shell abs-guide 10-4 calculate_difference(). () # Difference between two day indices. |
| 94 | { |
| 95 | let "diff = $1 - $2" # Global variable. |
| 96 | } |
| 97 | |
| 98 | |
| 99 | absscip-shell shell abs-guide 10-4 abs(). () # Absolute value |
| 100 | { # Uses global "value" variable. |
| 101 | if [scip-shell system . . `[`. "$1" -lt 0 ] # If negative |
| 102 | then #+ then |
| 103 | let "value = 0 - $1" #+ change sign, |
| 104 | else #+ else |
| 105 | let "value = $1" #+ leave it alone. |
| 106 | fi |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | if [scip-shell system . . `[`. $# -ne "$ARGS" ] # Require two command-line params. |
| 112 | then |
| 113 | Param_Errorscip-shell shell abs-guide 10-4 Param_Error(). |
| 114 | fi |
| 115 | |
| 116 | Parse_Datescip-shell shell abs-guide 10-4 Parse_Date(). $1 |
| 117 | check_datescip-shell shell abs-guide 10-4 check_date(). $day $month $year # See if valid date. |
| 118 | |
| 119 | strip_leading_zeroscip-shell shell abs-guide 10-4 strip_leading_zero(). $day # Remove any leading zeroes |
| 120 | day=$? #+ on day and/or month. |
| 121 | strip_leading_zeroscip-shell shell abs-guide 10-4 strip_leading_zero(). $month |
| 122 | month=$? |
| 123 | |
| 124 | let "date1 = `day_indexscip-shell shell abs-guide 10-4 day_index(). $day $month $year`" |
| 125 | |
| 126 | |
| 127 | Parse_Datescip-shell shell abs-guide 10-4 Parse_Date(). $2 |
| 128 | check_datescip-shell shell abs-guide 10-4 check_date(). $day $month $year |
| 129 | |
| 130 | strip_leading_zeroscip-shell shell abs-guide 10-4 strip_leading_zero(). $day |
| 131 | day=$? |
| 132 | strip_leading_zeroscip-shell shell abs-guide 10-4 strip_leading_zero(). $month |
| 133 | month=$? |
| 134 | |
| 135 | date2=$(day_indexscip-shell shell abs-guide 10-4 day_index(). $day $month $year) # Command substitution. |
| 136 | |
| 137 | |
| 138 | calculate_differencescip-shell shell abs-guide 10-4 calculate_difference(). $date1 $date2 |
| 139 | |
| 140 | absscip-shell shell abs-guide 10-4 abs(). $diff # Make sure it's positive. |
| 141 | diff=$value |
| 142 | |
| 143 | echoscip-shell system . . echo. $diff |
| 144 | |
| 145 | exit 0 |
| 146 | |
| 147 | # Exercise: |
| 148 | # -------- |
| 149 | # If given only one command-line parameter, have the script |
| 150 | #+ use today's date as the second. |
| 151 | |
| 152 | |
| 153 | # Compare this script with |
| 154 | #+ the implementation of Gauss' Formula in a C program at |
| 155 | #+ http://buschencrew.hypermart.net/software/datedif |