/misc/folkrec.cgi

#!/usr/bin/env python
"""
folkrec.py - Convert Folktown Records editions into weeks
License: GPL 2; share and enjoy!
Author: Sean B. Palmer, inamidst.com

Example: 
   >>> fr2ec(1)
   '-13/3/22 EC'
   >>> fr2ec(614)
   '0/1/1 EC'
   >>> fr2ec(312)
   '-7/9/15 EC'
   >>> 
"""

# FR1 -> -13/3/22 EC
# FR2 -> -13/4/1 EC
# FR6 -> -13/5/1 EC
# FR10 -> -13/6/1 EC
# FR14 -> -13/7/1 EC
# FR18 -> -13/8/1 EC
# FR22 -> -13/9/1 EC
# FR26 -> -13/10/1 EC
# FR30 -> -13/11/1 EC
# FR34 -> -13/12/1 EC
# FR38 -> -12/1/1 EC
# FR86 -> -11/1/1 EC
# FR134 -> -10/1/1 EC
# FR182 -> -9/1/1 EC
# FR230 -> -8/1/1 EC
# FR278 -> -7/1/1 EC
# FR326 -> -6/1/1 EC
# FR374 -> -5/1/1 EC
# FR422 -> -4/1/1 EC
# FR470 -> -3/1/1 EC
# FR518 -> -2/1/1 EC
# FR566 -> -1/1/1 EC
# FR614 -> 0/1/1 EC
# FR615 -> 0/1/8 EC

class EC(object): 
   def __init__(self, year=0, month=1, day=1): 
      self.days = 0
      self.set(year, month, day)

   def __str__(self): 
      return "%s/%s/%s EC" % self.get()

   def set(self, year, month=1, day=1): 
      """Set ECD according to year, month, and day.
         0/1/1 -> 0 ECD
         0/1/2 -> 1 ECD
         -1/12/28 -> -1 ECD
      """
      if (month < 1) or (month > 12): 
         raise "InvalidMonth", "Got: %s" % month
      if (day < 1) or (day > 28): 
         raise "InvalidDay", "Got: %s" % day

      if year < 0: 
         t = -((abs(year) * (12 * 28)) - (((month - 1) * 28) + (day - 1)))
      else: t = (year * (12 * 28)) + (((month - 1) * 28) + day)
      if not t: raise "DateOddness", "Aka. E_TECHNICALLY_IMPOSSIBLE"

      self.days = t
      return t

   def get(self): 
      """Return a year, month, day tuple."""
      date = self.days
      if not date: raise "DateOddness", "Aka. E_TECHNICALLY_IMPOSSIBLE"

      if date > -1: 
         year = date / 336
         date = date % 336

         month = date / 28
         day = date % 28
      else: 
         year = -1
         while date < -336: 
            year -= 1
            date += 336
         month = 12
         while date < -28: 
            month -= 1
            date += 28
         day = (28 + (date + 1))
      return year, month, day

def fr2ec(i): 
   if i < 1: raise "InvalidEdition", "Can't have FR numbers < 1."
   e = EC(0, 1, 1)
   if i < 614: 
      e.days = e.days - (((614 - i) * 7) + 1) # -2, -1, 1, 2, 3...
      return str(e)
   else: 
      e.days = e.days + ((i - 614) * 7)
      return str(e)

def main(): 
   import os
   print "Content-Type: text/html"
   print 

   i = os.environ.get('PATH_INFO', '/578')
   i = int(i.lstrip('/'))
   if i > 0: 
      print '<p>Folktown Records edition %s was issued on ' % i
      print '%s.</p>' % fr2ec(i)
   else: print '<p>Invalid edition: Folktown Records %s.</p>' % i
   print '<address><a href="http://gamegrene.com/wiki/User:Sbp"'
   print '>Sean B. Palmer</a>, <a href="http://gamegrene.com/wiki/"'
   print '>Ghyll</a>.</address>'

if __name__=="__main__": 
   main()