0
|
1 ;;; cal-mayan.el --- calendar functions for the Mayan calendars.
|
|
2
|
|
3 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Stewart M. Clamen <clamen@cs.cmu.edu>
|
|
6 ;; Edward M. Reingold <reingold@cs.uiuc.edu>
|
|
7 ;; Keywords: calendar
|
|
8 ;; Human-Keywords: Mayan calendar, Maya, calendar, diary
|
|
9
|
|
10 ;; This file is part of XEmacs.
|
|
11
|
|
12 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
13 ;; under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
|
17 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 ;; General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
16
|
23 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
25 ;; Boston, MA 02111-1307, USA.
|
0
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This collection of functions implements the features of calendar.el and
|
|
30 ;; diary.el that deal with the Mayan calendar. It was written jointly by
|
|
31
|
|
32 ;; Stewart M. Clamen School of Computer Science
|
|
33 ;; clamen@cs.cmu.edu Carnegie Mellon University
|
|
34 ;; 5000 Forbes Avenue
|
|
35 ;; Pittsburgh, PA 15213
|
|
36
|
|
37 ;; and
|
|
38
|
|
39 ;; Edward M. Reingold Department of Computer Science
|
|
40 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
|
|
41 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
|
|
42 ;; Urbana, Illinois 61801
|
|
43
|
|
44 ;; Comments, improvements, and bug reports should be sent to Reingold.
|
|
45
|
|
46 ;; Technical details of the Mayan calendrical calculations can be found in
|
|
47 ;; ``Calendrical Calculations, Part II: Three Historical Calendars''
|
|
48 ;; by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
|
|
49 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
|
|
50 ;; pages 383-404.
|
|
51
|
|
52 ;;; Code:
|
|
53
|
|
54 (require 'calendar)
|
|
55
|
|
56 (defun mayan-adjusted-mod (m n)
|
|
57 "Non-negative remainder of M/N with N instead of 0."
|
|
58 (1+ (mod (1- m) n)))
|
|
59
|
|
60 (defconst calendar-mayan-days-before-absolute-zero 1137140
|
|
61 "Number of days of the Mayan calendar epoch before absolute day 0.
|
|
62 According to the Goodman-Martinez-Thompson correlation. This correlation is
|
|
63 not universally accepted, as it still a subject of astro-archeological
|
|
64 research. Using 1232041 will give you Spinden's correlation; using
|
|
65 1142840 will give you Hochleitner's correlation.")
|
|
66
|
|
67 (defconst calendar-mayan-haab-at-epoch '(8 . 18)
|
|
68 "Mayan haab date at the epoch.")
|
|
69
|
|
70 (defconst calendar-mayan-haab-month-name-array
|
|
71 ["Pop" "Uo" "Zip" "Zotz" "Tzec" "Xul" "Yaxkin" "Mol" "Chen" "Yax"
|
|
72 "Zac" "Ceh" "Mac" "Kankin" "Muan" "Pax" "Kayab" "Cumku"])
|
|
73
|
|
74 (defconst calendar-mayan-tzolkin-at-epoch '(4 . 20)
|
|
75 "Mayan tzolkin date at the epoch.")
|
|
76
|
|
77 (defconst calendar-mayan-tzolkin-names-array
|
|
78 ["Imix" "Ik" "Akbal" "Kan" "Chicchan" "Cimi" "Manik" "Lamat" "Muluc" "Oc"
|
|
79 "Chuen" "Eb" "Ben" "Ix" "Men" "Cib" "Caban" "Etznab" "Cauac" "Ahau"])
|
|
80
|
|
81 (defun calendar-mayan-long-count-from-absolute (date)
|
|
82 "Compute the Mayan long count corresponding to the absolute DATE."
|
|
83 (let ((long-count (+ date calendar-mayan-days-before-absolute-zero)))
|
|
84 (let* ((baktun (/ long-count 144000))
|
|
85 (remainder (% long-count 144000))
|
|
86 (katun (/ remainder 7200))
|
|
87 (remainder (% remainder 7200))
|
|
88 (tun (/ remainder 360))
|
|
89 (remainder (% remainder 360))
|
|
90 (uinal (/ remainder 20))
|
|
91 (kin (% remainder 20)))
|
|
92 (list baktun katun tun uinal kin))))
|
|
93
|
|
94 (defun calendar-mayan-long-count-to-string (mayan-long-count)
|
|
95 "Convert MAYAN-LONG-COUNT into traditional written form."
|
|
96 (apply 'format (cons "%s.%s.%s.%s.%s" mayan-long-count)))
|
|
97
|
|
98 (defun calendar-string-to-mayan-long-count (str)
|
|
99 "Given STR, a string of format \"%d.%d.%d.%d.%d\", return list of nums."
|
|
100 (let ((rlc nil)
|
|
101 (c (length str))
|
|
102 (cc 0))
|
|
103 (condition-case condition
|
|
104 (progn
|
|
105 (while (< cc c)
|
|
106 (let* ((start (string-match "[0-9]+" str cc))
|
|
107 (end (match-end 0))
|
|
108 datum)
|
|
109 (setq datum (read (substring str start end)))
|
|
110 (setq rlc (cons datum rlc))
|
|
111 (setq cc end)))
|
|
112 (if (not (= (length rlc) 5)) (signal 'invalid-read-syntax nil)))
|
|
113 (invalid-read-syntax nil))
|
|
114 (reverse rlc)))
|
|
115
|
|
116 (defun calendar-mayan-haab-from-absolute (date)
|
|
117 "Convert absolute DATE into a Mayan haab date (a pair)."
|
|
118 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
|
|
119 (day-of-haab
|
|
120 (% (+ long-count
|
|
121 (car calendar-mayan-haab-at-epoch)
|
|
122 (* 20 (1- (cdr calendar-mayan-haab-at-epoch))))
|
|
123 365))
|
|
124 (day (% day-of-haab 20))
|
|
125 (month (1+ (/ day-of-haab 20))))
|
|
126 (cons day month)))
|
|
127
|
|
128 (defun calendar-mayan-haab-difference (date1 date2)
|
|
129 "Number of days from Mayan haab DATE1 to next occurrence of haab date DATE2."
|
|
130 (mod (+ (* 20 (- (cdr date2) (cdr date1)))
|
|
131 (- (car date2) (car date1)))
|
|
132 365))
|
|
133
|
|
134 (defun calendar-mayan-haab-on-or-before (haab-date date)
|
|
135 "Absolute date of latest HAAB-DATE on or before absolute DATE."
|
|
136 (- date
|
|
137 (% (- date
|
|
138 (calendar-mayan-haab-difference
|
|
139 (calendar-mayan-haab-from-absolute 0) haab-date))
|
|
140 365)))
|
|
141
|
|
142 (defun calendar-next-haab-date (haab-date &optional noecho)
|
|
143 "Move cursor to next instance of Mayan HAAB-DATE.
|
|
144 Echo Mayan date if NOECHO is t."
|
|
145 (interactive (list (calendar-read-mayan-haab-date)))
|
|
146 (calendar-goto-date
|
|
147 (calendar-gregorian-from-absolute
|
|
148 (calendar-mayan-haab-on-or-before
|
|
149 haab-date
|
|
150 (+ 365
|
|
151 (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
|
|
152 (or noecho (calendar-print-mayan-date)))
|
|
153
|
|
154 (defun calendar-previous-haab-date (haab-date &optional noecho)
|
|
155 "Move cursor to previous instance of Mayan HAAB-DATE.
|
|
156 Echo Mayan date if NOECHO is t."
|
|
157 (interactive (list (calendar-read-mayan-haab-date)))
|
|
158 (calendar-goto-date
|
|
159 (calendar-gregorian-from-absolute
|
|
160 (calendar-mayan-haab-on-or-before
|
|
161 haab-date
|
|
162 (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
|
|
163 (or noecho (calendar-print-mayan-date)))
|
|
164
|
|
165 (defun calendar-mayan-haab-to-string (haab)
|
|
166 "Convert Mayan haab date (a pair) into its traditional written form."
|
|
167 (let ((month (cdr haab))
|
|
168 (day (car haab)))
|
|
169 ;; 19th month consists of 5 special days
|
|
170 (if (= month 19)
|
|
171 (format "%d Uayeb" day)
|
|
172 (format "%d %s"
|
|
173 day
|
|
174 (aref calendar-mayan-haab-month-name-array (1- month))))))
|
|
175
|
|
176 (defun calendar-mayan-tzolkin-from-absolute (date)
|
|
177 "Convert absolute DATE into a Mayan tzolkin date (a pair)."
|
|
178 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
|
|
179 (day (mayan-adjusted-mod
|
|
180 (+ long-count (car calendar-mayan-tzolkin-at-epoch))
|
|
181 13))
|
|
182 (name (mayan-adjusted-mod
|
|
183 (+ long-count (cdr calendar-mayan-tzolkin-at-epoch))
|
|
184 20)))
|
|
185 (cons day name)))
|
|
186
|
|
187 (defun calendar-mayan-tzolkin-difference (date1 date2)
|
|
188 "Number of days from Mayan tzolkin DATE1 to next occurrence of tzolkin DATE2."
|
|
189 (let ((number-difference (- (car date2) (car date1)))
|
|
190 (name-difference (- (cdr date2) (cdr date1))))
|
|
191 (mod (+ number-difference
|
|
192 (* 13 (mod (* 3 (- number-difference name-difference))
|
|
193 20)))
|
|
194 260)))
|
|
195
|
|
196 (defun calendar-mayan-tzolkin-on-or-before (tzolkin-date date)
|
|
197 "Absolute date of latest TZOLKIN-DATE on or before absolute DATE."
|
|
198 (- date
|
|
199 (% (- date (calendar-mayan-tzolkin-difference
|
|
200 (calendar-mayan-tzolkin-from-absolute 0)
|
|
201 tzolkin-date))
|
|
202 260)))
|
|
203
|
|
204 (defun calendar-next-tzolkin-date (tzolkin-date &optional noecho)
|
|
205 "Move cursor to next instance of Mayan TZOLKIN-DATE.
|
|
206 Echo Mayan date if NOECHO is t."
|
|
207 (interactive (list (calendar-read-mayan-tzolkin-date)))
|
|
208 (calendar-goto-date
|
|
209 (calendar-gregorian-from-absolute
|
|
210 (calendar-mayan-tzolkin-on-or-before
|
|
211 tzolkin-date
|
|
212 (+ 260
|
|
213 (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
|
|
214 (or noecho (calendar-print-mayan-date)))
|
|
215
|
|
216 (defun calendar-previous-tzolkin-date (tzolkin-date &optional noecho)
|
|
217 "Move cursor to previous instance of Mayan TZOLKIN-DATE.
|
|
218 Echo Mayan date if NOECHO is t."
|
|
219 (interactive (list (calendar-read-mayan-tzolkin-date)))
|
|
220 (calendar-goto-date
|
|
221 (calendar-gregorian-from-absolute
|
|
222 (calendar-mayan-tzolkin-on-or-before
|
|
223 tzolkin-date
|
|
224 (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
|
|
225 (or noecho (calendar-print-mayan-date)))
|
|
226
|
|
227 (defun calendar-mayan-tzolkin-to-string (tzolkin)
|
|
228 "Convert Mayan tzolkin date (a pair) into its traditional written form."
|
|
229 (format "%d %s"
|
|
230 (car tzolkin)
|
|
231 (aref calendar-mayan-tzolkin-names-array (1- (cdr tzolkin)))))
|
|
232
|
|
233 (defun calendar-mayan-tzolkin-haab-on-or-before (tzolkin-date haab-date date)
|
|
234 "Absolute date that is Mayan TZOLKIN-DATE and HAAB-DATE.
|
|
235 Latest such date on or before DATE.
|
|
236 Returns nil if such a tzolkin-haab combination is impossible."
|
|
237 (let* ((haab-difference
|
|
238 (calendar-mayan-haab-difference
|
|
239 (calendar-mayan-haab-from-absolute 0)
|
|
240 haab-date))
|
|
241 (tzolkin-difference
|
|
242 (calendar-mayan-tzolkin-difference
|
|
243 (calendar-mayan-tzolkin-from-absolute 0)
|
|
244 tzolkin-date))
|
|
245 (difference (- tzolkin-difference haab-difference)))
|
|
246 (if (= (% difference 5) 0)
|
|
247 (- date
|
|
248 (mod (- date
|
|
249 (+ haab-difference (* 365 difference)))
|
|
250 18980))
|
|
251 nil)))
|
|
252
|
|
253 (defun calendar-read-mayan-haab-date ()
|
|
254 "Prompt for a Mayan haab date"
|
|
255 (let* ((completion-ignore-case t)
|
|
256 (haab-day (calendar-read
|
|
257 "Haab kin (0-19): "
|
|
258 '(lambda (x) (and (>= x 0) (< x 20)))))
|
|
259 (haab-month-list (append calendar-mayan-haab-month-name-array
|
|
260 (and (< haab-day 5) '("Uayeb"))))
|
|
261 (haab-month (cdr
|
|
262 (assoc
|
|
263 (capitalize
|
|
264 (completing-read "Haab uinal: "
|
|
265 (mapcar 'list haab-month-list)
|
|
266 nil t))
|
|
267 (calendar-make-alist
|
|
268 haab-month-list 1 'capitalize)))))
|
|
269 (cons haab-day haab-month)))
|
|
270
|
|
271 (defun calendar-read-mayan-tzolkin-date ()
|
|
272 "Prompt for a Mayan tzolkin date"
|
|
273 (let* ((completion-ignore-case t)
|
|
274 (tzolkin-count (calendar-read
|
|
275 "Tzolkin kin (1-13): "
|
|
276 '(lambda (x) (and (> x 0) (< x 14)))))
|
|
277 (tzolkin-name-list (append calendar-mayan-tzolkin-names-array nil))
|
|
278 (tzolkin-name (cdr
|
|
279 (assoc
|
|
280 (capitalize
|
|
281 (completing-read "Tzolkin uinal: "
|
|
282 (mapcar 'list tzolkin-name-list)
|
|
283 nil t))
|
|
284 (calendar-make-alist
|
|
285 tzolkin-name-list 1 'capitalize)))))
|
|
286 (cons tzolkin-count tzolkin-name)))
|
|
287
|
|
288 (defun calendar-next-calendar-round-date
|
|
289 (tzolkin-date haab-date &optional noecho)
|
|
290 "Move cursor to next instance of Mayan HAAB-DATE TZOKLIN-DATE combination.
|
|
291 Echo Mayan date if NOECHO is t."
|
|
292 (interactive (list (calendar-read-mayan-tzolkin-date)
|
|
293 (calendar-read-mayan-haab-date)))
|
|
294 (let ((date (calendar-mayan-tzolkin-haab-on-or-before
|
|
295 tzolkin-date haab-date
|
|
296 (+ 18980 (calendar-absolute-from-gregorian
|
|
297 (calendar-cursor-to-date))))))
|
|
298 (if (not date)
|
|
299 (error "%s, %s does not exist in the Mayan calendar round"
|
|
300 (calendar-mayan-tzolkin-to-string tzolkin-date)
|
|
301 (calendar-mayan-haab-to-string haab-date))
|
|
302 (calendar-goto-date (calendar-gregorian-from-absolute date))
|
|
303 (or noecho (calendar-print-mayan-date)))))
|
|
304
|
|
305 (defun calendar-previous-calendar-round-date
|
|
306 (tzolkin-date haab-date &optional noecho)
|
|
307 "Move to previous instance of Mayan TZOKLIN-DATE HAAB-DATE combination.
|
|
308 Echo Mayan date if NOECHO is t."
|
|
309 (interactive (list (calendar-read-mayan-tzolkin-date)
|
|
310 (calendar-read-mayan-haab-date)))
|
|
311 (let ((date (calendar-mayan-tzolkin-haab-on-or-before
|
|
312 tzolkin-date haab-date
|
|
313 (1- (calendar-absolute-from-gregorian
|
|
314 (calendar-cursor-to-date))))))
|
|
315 (if (not date)
|
|
316 (error "%s, %s does not exist in the Mayan calendar round"
|
|
317 (calendar-mayan-tzolkin-to-string tzolkin-date)
|
|
318 (calendar-mayan-haab-to-string haab-date))
|
|
319 (calendar-goto-date (calendar-gregorian-from-absolute date))
|
|
320 (or noecho (calendar-print-mayan-date)))))
|
|
321
|
|
322 (defun calendar-absolute-from-mayan-long-count (c)
|
|
323 "Compute the absolute date corresponding to the Mayan Long Count C.
|
|
324 Long count is a list (baktun katun tun uinal kin)"
|
|
325 (+ (* (nth 0 c) 144000) ; baktun
|
|
326 (* (nth 1 c) 7200) ; katun
|
|
327 (* (nth 2 c) 360) ; tun
|
|
328 (* (nth 3 c) 20) ; uinal
|
|
329 (nth 4 c) ; kin (days)
|
|
330 (- ; days before absolute date 0
|
|
331 calendar-mayan-days-before-absolute-zero)))
|
|
332
|
|
333 (defun calendar-mayan-date-string (&optional date)
|
|
334 "String of Mayan date of Gregorian DATE.
|
|
335 Defaults to today's date if DATE is not given."
|
|
336 (let* ((d (calendar-absolute-from-gregorian
|
|
337 (or date (calendar-current-date))))
|
|
338 (tzolkin (calendar-mayan-tzolkin-from-absolute d))
|
|
339 (haab (calendar-mayan-haab-from-absolute d))
|
|
340 (long-count (calendar-mayan-long-count-from-absolute d)))
|
|
341 (format "Long count = %s; tzolkin = %s; haab = %s"
|
|
342 (calendar-mayan-long-count-to-string long-count)
|
|
343 (calendar-mayan-tzolkin-to-string tzolkin)
|
|
344 (calendar-mayan-haab-to-string haab))))
|
|
345
|
|
346 (defun calendar-print-mayan-date ()
|
|
347 "Show the Mayan long count, tzolkin, and haab equivalents of date."
|
|
348 (interactive)
|
|
349 (message "Mayan date: %s"
|
|
350 (calendar-mayan-date-string (calendar-cursor-to-date t))))
|
|
351
|
|
352 (defun calendar-goto-mayan-long-count-date (date &optional noecho)
|
|
353 "Move cursor to Mayan long count DATE. Echo Mayan date unless NOECHO is t."
|
|
354 (interactive
|
|
355 (let (lc)
|
|
356 (while (not lc)
|
|
357 (let ((datum
|
|
358 (calendar-string-to-mayan-long-count
|
|
359 (read-string "Mayan long count (baktun.katun.tun.uinal.kin): "
|
|
360 (calendar-mayan-long-count-to-string
|
|
361 (calendar-mayan-long-count-from-absolute
|
|
362 (calendar-absolute-from-gregorian
|
|
363 (calendar-current-date))))))))
|
|
364 (if (calendar-mayan-long-count-common-era datum)
|
|
365 (setq lc datum))))
|
|
366 (list lc)))
|
|
367 (calendar-goto-date
|
|
368 (calendar-gregorian-from-absolute
|
|
369 (calendar-absolute-from-mayan-long-count date)))
|
|
370 (or noecho (calendar-print-mayan-date)))
|
|
371
|
|
372 (defun calendar-mayan-long-count-common-era (lc)
|
|
373 "T if long count represents date in the Common Era."
|
|
374 (let ((base (calendar-mayan-long-count-from-absolute 1)))
|
|
375 (while (and (not (null base)) (= (car lc) (car base)))
|
|
376 (setq lc (cdr lc)
|
|
377 base (cdr base)))
|
|
378 (or (null lc) (> (car lc) (car base)))))
|
|
379
|
|
380 (defun diary-mayan-date ()
|
|
381 "Show the Mayan long count, haab, and tzolkin dates as a diary entry."
|
|
382 (format "Mayan date: %s" (calendar-mayan-date-string date)))
|
|
383
|
|
384 (provide 'cal-mayan)
|
|
385
|
|
386 ;;; cal-mayan.el ends here
|