command line – stupid date tricks

Just because you can do something doesn’t mean that you should do it.  I try and keep to that simple rule but from time to time it is fun to see what crazy things you can do in a simple shell script.

Easter is one of those holidays that does not fall on a fixed date each year.  There is a fairly complicated formula to calculate the Easter weekend.

All that is needed to calculate Easter is integer math.  I described how to perform simple math in a bash shell in a previous blog entry.

# https://en.wikipedia.org/wiki/Computus
# anonymous Gregorian algorithm

twodig()
{
  retval=$1
  if [ $1 -lt 10 ]
  then
    retval=0$1
  fi
  echo $retval
}

easter()
{
  Y=$1
  a=$(( $Y % 19 ))
  b=$(( $Y / 100 ))
  c=$(( $Y % 100 ))
  d=$(( $b / 4 ))
  e=$(( $b % 4 ))
  f=$(( ($b + 8) / 25 ))
  g=$(( ($b - $f + 1) / 3 ))
  h=$(( (19 * $a + $b - $d - $g + 15) % 30 ))
  i=$(( $c / 4 ))
  k=$(( $c % 4 ))
  l=$(( (32 + 2 * $e + 2 * $i - $h - $k) % 7 ))
  m=$(( ($a + 11 * $h + 22 * $l) / 451 ))

  month=$(( (($h + $l - 7 * $m + 114) / 31) ))
  month=`twodig $month`
  day=$(( (($h + $l - 7 * $m + 114) % 31) + 1 ))
  day=`twodig $day`

  echo $Y$month$day
}

for i in 2008 2009 2010 2011 2012 2013 2014 2015
do
  easter $i
done
This entry was posted in Command line and tagged , . Bookmark the permalink.