# bc script for showing the lengths of the months relative to skip years
# for the world of Bobbie, Karel, Dan, and Kristie, 
#
# by Joel Matthew Rees, winter/spring 2017.
# Copyright 2017, Joel Matthew Rees
#
# Permission granted to use for personal entertainment only.
# (If you need it for other purposes, rewriting it yourself is not that hard, 
# and the result will satisfy your needs much more effectively.)
#
# http://joel-rees-economics.blogspot.com/2017/03/soc500-03-08-calendar-math.html
# http://joel-rees-economics.blogspot.com/2017/03/soc500-03-09-computer-calendar.html
#
# Novel here:
# http://joel-rees-economics.blogspot.com/2017/01/soc500-00-00-toc.html
#
# Save as "econmonth.bc"
# invoke as "bc -l econmonth.bc
#
# In the running bc session, run it with
# showmonths(7) for seven years, etc.
 

scale = 10;


months[ 0 ] = 30;
months[ 1 ] = 29;
months[ 2 ] = 30;
months[ 3 ] = 29;
months[ 4 ] = 29;
months[ 5 ] = 30;
months[ 6 ] = 29;
months[ 7 ] = 30;
months[ 8 ] = 29;
months[ 9 ] = 29;
months[ 10 ] = 30;
months[ 11 ] = 29;

define abs( n ) {
  if ( n >= 0 ) {
    return n;
  }
  return -n;
}


# If you want to do something similar, 
# for looking at how leap years in your world  
# match the actual orbits and revolutions
# of your world and its moon,
# replace isskip() with an appropriate isleap().
# Left as an exercise for the reader.

define isskip( y, m ) {
  if ( m != 0 ) {
    return 0;
  }
  mem = scale;
  scale = 0;
  diff7 = y % 7;
  diff98 = y % 98;
  diff343 = y % 343;
  scale = mem;
  if ( diff98 == 48 ) {
    return 1;
  }
  if ( ( diff7 != 1 ) && ( diff7 != 4 ) ) {
    return 0;
  }
  if ( diff343 == 186 ) {
    return 0;
  }
  return 1;
}


# Note that we are treating the first year as year 0, 
# and the first month as month 0.
# For your earth, you will need to adjust the year and month input and output.


define showmonths( years ) {
  sum = 0;
  for ( year = 0; year < years; ++year ) {
    for ( month = 0; month <= 11; ++month ) {
      days = months[ month ];
      if ( isskip( year, month ) ) {
        days -= 1;
      }
      sum += days;
      product = year * ( 241957 / 686 ) + ( month + 1 ) * ( 241957 / 686 ) / 12;
      diff = product - sum;
      print year, " ", month, ": ", days, " (", sum , ", ", product, ": ", diff, ")";
      if ( abs( diff ) >= 1 ) {
        print "*** > 1 day! ";
      }
      print "\n";
    }
  }
}