Long Count Mayan Date in Python
<p>I need to compile a program that can convert a Gregorian date to a Mayan one. I also need to use 01/01/1970 as a reference date.</p>

<p>The following auxiliary functions work accordingly and have no mistakes.</p>

<pre><code># turns dates into tuples


def dmj(date):

"""
&gt;&gt;&gt; dmj('01/01/1970')
(1, 1, 1970)
&gt;&gt;&gt; dmj('00012+00012+02012')
(12, 12, 2012)
"""

tup = ()
for i in date:
    if i.isdigit() == False and i != ' ':
        date = date.replace(i, ' ')

number_str = ''
for i in date:
    number_str += i
    if i == ' ':
        number = int(number_str)
        tup += (number,)
        number_str = ''
tup += (int(number_str),)
return tup

# counts days that have passed since 01/01/1970

def daysPassed(date):
"""
&gt;&gt;&gt; daysPassed('01/01/1970')
0
&gt;&gt;&gt; daysPassed('20-7-1988')
6775
&gt;&gt;&gt; daysPassed('00012+00012+02012')
15686
"""

from datetime import date
tup = dmj(date)
begin = date(1970, 1, 1)
end = date(tup[2], tup[1], tup[0])
passed = abs(end - begin)        
return passed.days
</code></pre>

<p>My idea was to first calculate how many days have passed since the beginning of this pictun (20 baktuns long) in 01/01/1970 and then added the days passed since then according to the given date. </p>

<p>In the Mayan calendar a day is refered to as a kin. Their periods (within one pictun) are as follows:</p>

<p>20 kin = 1 uinal; 18 uinal = 1 tun; 20 tun = 1 katun; 20 katun = 1 baktun</p>

<p>In the long count notation the Mayan date for 01/01/1970 is '12.17.16.7.5'. Baktun are written first, then katuns, etc... Mayan dates start from 0. Basically the first kin of a uinal is number zero and the last one number 19, twenty in total.</p>

<p>I've first compiled the following:</p>

<pre><code>def mayanDate(date, separation='')

"""
&gt;&gt;&gt; mayanDate('01/01/1970')
'12/17/16/7/5'
&gt;&gt;&gt; mayaDate('20-7-1988', separator='/')
'12/18/15/4/0'
&gt;&gt;&gt; mayaDate('00012+00012+02012', separator='-')
'12-19-19-17-11'
&gt;&gt;&gt; mayaDate('21 12 2012', separator='+')
'13+0+0+0+0'
&gt;&gt;&gt; mayaDate('26.03.2407')
'14.0.0.0.0'
"""
days = daysPassed(date) + 13 * 144000 + 18 * 7200\
 + 17 * 400 + 8 * 20 + 6     
baktun = str((days //((20 **3) * 18)) - 1)
days = days % ((20 **3) * 18)    
katun = str((days //((20 **2) * 18)) - 1)
days = days % ((20 **2) * 18)    
tun = str((days // (20 **2)) - 1)
days = days % (20 **2)    
uinal = str((days // 20) - 1)
days = days % 20 - 1

kin = str(days)
mayanlist = [baktun, katun, tun, uinal, kin]

for i in date:
    if i.isdigit() == False and separator == '':
        separator = i
        break

mayandate = separator.join(mayanlist)
return mayandate
</code></pre>

<p>For some strange reason only 01/01/1970 returns the correct Mayan long notation, despite counting from the beginning of an entire pictun (7,900 years in length!). For all other dates it seems to advance too fast through the calendar, despite my second auxiliary function returning the correct values (even for millennia into the future).</p>

<p>I wonder where's the mistake. For instance <code>mayanDate('20-7-1988')</code> returns <code>'12-18-15-6-0'</code> instead of <code>'12-18-15-4-0'</code> and <code>mayanDate('21 12 2012')</code> returns <code>'13 0 1 12 0'</code> instead of <code>'13 0 0 0 0'</code>.</p>
