Last active
August 29, 2015 14:01
-
-
Save chuckwagoncomputing/2cd2a59d61f3d6157cb0 to your computer and use it in GitHub Desktop.
Age as a fraction. Thanks to http://blog.plover.com/math/age-fraction-2.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BIRTHDAY=$1 | |
DAYSTODAY=$(date +%j | sed "s/^0*//g") | |
DAYSATBIRTH=$(date -d "$BIRTHDAY" +%j | sed "s/^0*//g") | |
YEARSOLD=$(($(date +%Y)-$(date -d "$BIRTHDAY" +%Y))) | |
if [ $DAYSATBIRTH -gt $DAYSTODAY ]; then | |
YEARSOLD=$(($YEARSOLD-1)); | |
DAYSSINCE=$(($((365-$DAYSATBIRTH))+$DAYSTODAY)) | |
else | |
DAYSSINCE=$(($DAYSTODAY-$DAYSATBIRTH)) | |
fi | |
if [ $DAYSSINCE -eq 0 ]; then | |
echo "$YEARSOLD years old." | |
exit | |
fi | |
isin() { | |
isle $1 $2 $3 $4 && islt $3 $4 $5 $6 | |
} | |
islt() { | |
test $(($1*$4)) -lt $(($2*$3)) | |
} | |
isle() { | |
test $(($1*$4)) -le $(($2*$3)) | |
} | |
LOWN=0; | |
LOWD=1; | |
HIGHN=1; | |
HIGHD=0; | |
while true; do | |
NUM=$((LOWN+HIGHN)) | |
DEN=$((LOWD+HIGHD)) | |
if isin $DAYSSINCE 365 $NUM $DEN $(($DAYSSINCE+1)) 365; then | |
echo "$YEARSOLD $NUM/$DEN years old." | |
break | |
elif islt $NUM $DEN $DAYSSINCE 365; then | |
LOWN=$NUM; | |
LOWD=$DEN; | |
elif isle $(($DAYSSINCE+1)) 365 $NUM $DEN; then | |
HIGHN=$NUM; | |
HIGHD=$DEN; | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment