Skip to content

Instantly share code, notes, and snippets.

@chuckwagoncomputing
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckwagoncomputing/2cd2a59d61f3d6157cb0 to your computer and use it in GitHub Desktop.
Save chuckwagoncomputing/2cd2a59d61f3d6157cb0 to your computer and use it in GitHub Desktop.
#!/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