1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author : Aleksandr Tishin /Mystic-Mirage/
# Email : <aleksandr.tishin@gmail.com>
# License : Public Domain
from datetime import datetime
from re import sub
import sys
from getopt import getopt
from _strptime import TimeRE
from os import path
import gettext
i18n_path = path.join(path.realpath(path.dirname(sys.argv[0])), 'locale')
if not path.isdir(i18n_path):
i18n_path = None
gettext.install('mayanc', i18n_path)
gmt = 584283
astronomical = 584285
default_cor = gmt
default_fmt = _('%C, %Z %H') # == %b.%k.%t.%w.%i, %z2 %z3 %h2 %h3
default_sce = 1
def todaydatetuple():
return datetime.today().timetuple()[:3]
def getmayandays(g_tuple = todaydatetuple(), cor = default_cor, \
bc = False):
i_year = 0
if bc and g_tuple == (1, 2, 29):
g_date = datetime(1, 3, 1)
i_year = 1
else:
g_date = datetime(*g_tuple)
if bc:
if g_date < datetime(1, 3, 1) or g_date >= datetime(2, 1, 1):
i_year = 1
return ((g_date - datetime(g_date.year, 12, 31)).days \
- datetime(g_date.year, 1, 1).toordinal() \
+ 1721426 - cor - i_year) % 1872000
else:
return (g_date.toordinal() + 1721425 - cor) % 1872000
def getlongcount(m_days, scenario = default_sce):
d = m_days
result = []
for i in (144000, 7200, 360, 20, 1):
t, d = divmod(d, i)
result.append(t)
if scenario == 1 and not True in map(bool, result) or \
scenario == 2 and not result[0]:
result[0] = 13
return tuple(result)
def gettzolkin(m_days):
return ((m_days + 19) % 20, (m_days + 3) % 13 + 1)
def gethaab(m_days):
return divmod((m_days + 348) % 365, 20)
def getlord(m_days):
return (m_days + 8) % 9 + 1
tzolkinlist = (_("Imix'"), _("Ik'"), _("Ak'b'al"), _("K'an"), \
_("Chikchan"), _("Kimi"), _("Manik'"), _("Lamat"), _("Muluk"), \
_("Ok"), _("Chuwen"), _("Eb'"), _("B'en"), _("Ix"), _("Men"), \
_("K'ib'"), _("Kab'an"), _("Etz'nab'"), _("Kawak"), _("Ajaw"))
haablist = (_("Pop"), _("Wo"), _("Sip"), _("Sotz'"), _("Tzek"), \
_("Xul"), _("Yaxk'"), _("Mol"), _("Ch'en"), _("Yax"), \
_("Sac"), _("Keh"), _("Mak"), _("K'ank'in"), _("Muwan"), \
_("Pax"), _("K'ayab'"), _("Kumk'u"), _("Wayeb'"))
lordlist = (_("G1"), _("G2"), _("G3"), _("G4"), _("G5"), _("G6"), \
_("G7"), _("G8"), _("G9"))
def getmayandatetuple(g_tuple = todaydatetuple(), cor = default_cor, \
scenario = default_sce, bc = False):
days = getmayandays(g_tuple, cor, bc)
return getlongcount(days, scenario) + gettzolkin(days) + \
gethaab(days) + (getlord(days),)
def getmayandate(g_tuple = todaydatetuple(), cor = default_cor, \
fmt = default_fmt, scenario = default_sce, bc = False):
m_tuple = getmayandatetuple(g_tuple, cor, scenario, bc)
for s, r in map(lambda x, y: ('%' + x, str(y)), \
('C', 'Z', 'H', 'b', 'k', 't', 'w', 'i', 'z1', 'z2', \
'h1', 'h2', 'l', 'z3', 'h3', 'L'), \
('%b.%k.%t.%w.%i', '%z2 %z3', '%h2 %h3') + m_tuple + \
(tzolkinlist[m_tuple[5]], haablist[m_tuple[7]], \
lordlist[m_tuple[9] - 1])):
fmt = sub(s, r, fmt)
return fmt
def strpdate(data_string, format = '%Y-%m-%d', bc = False):
_TimeRE_cache = TimeRE()
_regex_cache = {}
try:
format_regex = _TimeRE_cache.compile(format)
# KeyError raised when a bad format is found; can be specified as
# \\, in which case it was a stray % but with a space after it
except KeyError, err:
bad_directive = err.args[0]
if bad_directive == "\\":
bad_directive = "%"
del err
raise ValueError("'%s' is a bad directive in format '%s'" %
(bad_directive, format))
# IndexError only occurs when the format string is "%"
except IndexError:
raise ValueError("stray %% in format '%s'" % format)
_regex_cache[format] = format_regex
found = format_regex.match(data_string)
if not found:
raise ValueError("time data %r does not match format %r" %
(data_string, format))
if len(data_string) != found.end():
raise ValueError("unconverted data remains: %s" %
data_string[found.end():])
date = list(todaydatetuple())
found_dict = found.groupdict()
for group_key in found_dict.iterkeys():
if group_key == 'y':
date[0] = int(found_dict['y'])
if date[0] <= 68:
date[0] += 2000
else:
date[0] += 1900
elif group_key == 'Y':
date[0] = int(found_dict['Y'])
elif group_key == 'm':
date[1] = int(found_dict['m'])
elif group_key == 'd':
date[2] = int(found_dict['d'])
if not (bc and date == [1, 2, 29]):
datetime(*date)
return tuple(date)
if __name__ == '__main__':
correlation = default_cor
gformat = _('%Y-%m-%d')
gdate = datetime.today().strftime(gformat)
mformat = default_fmt
scen = default_sce
befc = False
optlist, args = getopt(sys.argv[1:], 'c:d:g:f:s:', \
['gmt', 'astronomical', 'baktun', 'katun', 'tun', 'winal', \
'kin', 'tzol1', 'tzol2', 'tzol3', 'haab1', 'haab2', \
'haab3', 'lord1', 'lord2', 'long', 'tzol', 'haab', 'bc'])
for p, v in optlist:
if p == '-c':
if v == 'gmt':
correlation = gmt
elif v == 'astronomical':
correlation = astronomical
else:
correlation = int(v)
elif p == '--gmt':
correlation = gmt
elif p == '--astronomical':
correlation = astronomical
elif p == '-d':
gdate = v
elif p == '-g':
gformat = v
elif p == '-f':
mformat = v
elif p == '-s':
scen = int(v)
elif p == '--baktun':
mformat = '%b'
elif p == '--katun':
mformat = '%k'
elif p == '--tun':
mformat = '%t'
elif p == '--winal':
mformat = '%w'
elif p == '--kin':
mformat = '%i'
elif p == '--tzol1':
mformat = '%z1'
elif p == '--tzol2':
mformat = '%z2'
elif p == '--tzol3':
mformat = '%z3'
elif p == '--haab1':
mformat = '%h1'
elif p == '--haab2':
mformat = '%h2'
elif p == '--haab3':
mformat = '%h3'
elif p == '--lord1':
mformat = '%l'
elif p == '--lord2':
mformat = '%L'
elif p == '--long':
mformat = '%C'
elif p == '--tzol':
mformat = '%Z'
elif p == '--haab':
mformat = '%H'
elif p == '--bc':
befc = True
print getmayandate(strpdate(gdate, gformat, befc), \
correlation, mformat, scen, befc)
|