1 ;;; cal-mayan.el --- calendar functions for the Mayan calendars
3 ;; Copyright (C) 1992, 1993, 1995, 1997, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Stewart M. Clamen <clamen@cs.cmu.edu>
7 ;; Edward M. Reingold <reingold@cs.uiuc.edu>
8 ;; Maintainer: Glenn Morris <rgm@gnu.org>
10 ;; Human-Keywords: Mayan calendar, Maya, calendar, diary
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
37 (defconst calendar-mayan-days-before-absolute-zero
1137142
38 "Number of days of the Mayan calendar epoch before absolute day 0.
39 This is the Goodman-Martinez-Thompson correlation used by almost all experts,
40 but some use 1137140. Using 1232041 gives you Spinden's correlation; using
41 1142840 gives you Hochleitner's correlation.")
43 (defconst calendar-mayan-haab-at-epoch
'(8 .
18)
44 "Mayan haab date at the epoch.")
46 (defconst calendar-mayan-haab-month-name-array
47 ["Pop" "Uo" "Zip" "Zotz" "Tzec" "Xul" "Yaxkin" "Mol" "Chen" "Yax"
48 "Zac" "Ceh" "Mac" "Kankin" "Muan" "Pax" "Kayab" "Cumku"]
49 "Names of the Mayan haab months.")
51 (defconst calendar-mayan-tzolkin-at-epoch
'(4 .
20)
52 "Mayan tzolkin date at the epoch.")
54 (defconst calendar-mayan-tzolkin-names-array
55 ["Imix" "Ik" "Akbal" "Kan" "Chicchan" "Cimi" "Manik" "Lamat" "Muluc" "Oc"
56 "Chuen" "Eb" "Ben" "Ix" "Men" "Cib" "Caban" "Etznab" "Cauac" "Ahau"]
57 "Names of the Mayan tzolkin months.")
59 (defun calendar-mayan-long-count-from-absolute (date)
60 "Compute the Mayan long count corresponding to the absolute DATE."
61 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero
))
62 (baktun (/ long-count
144000))
63 (remainder (% long-count
144000))
64 (katun (/ remainder
7200))
65 (remainder (% remainder
7200))
66 (tun (/ remainder
360))
67 (remainder (% remainder
360))
68 (uinal (/ remainder
20))
69 (kin (% remainder
20)))
70 (list baktun katun tun uinal kin
)))
72 (defun calendar-mayan-long-count-to-string (mayan-long-count)
73 "Convert MAYAN-LONG-COUNT into traditional written form."
74 (apply 'format
(cons "%s.%s.%s.%s.%s" mayan-long-count
)))
76 (defun calendar-mayan-string-from-long-count (str)
77 "Given STR, a string of format \"%d.%d.%d.%d.%d\", return list of numbers."
83 (while (string-match "[0-9]+" str end
)
84 (setq rlc
(cons (string-to-number (match-string 0 str
)) rlc
)
86 (unless (= (length rlc
) 5) (signal 'invalid-read-syntax nil
)))
87 (invalid-read-syntax nil
))
90 (defun calendar-mayan-haab-from-absolute (date)
91 "Convert absolute DATE into a Mayan haab date (a pair)."
92 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero
))
95 (car calendar-mayan-haab-at-epoch
)
96 (* 20 (1- (cdr calendar-mayan-haab-at-epoch
))))
98 (day (% day-of-haab
20))
99 (month (1+ (/ day-of-haab
20))))
102 (defun calendar-mayan-haab-difference (date1 date2
)
103 "Number of days from Mayan haab DATE1 to next occurrence of haab date DATE2."
104 (mod (+ (* 20 (- (cdr date2
) (cdr date1
)))
105 (- (car date2
) (car date1
)))
108 (defun calendar-mayan-haab-on-or-before (haab-date date
)
109 "Absolute date of latest HAAB-DATE on or before absolute DATE."
112 (calendar-mayan-haab-difference
113 (calendar-mayan-haab-from-absolute 0) haab-date
))
117 (defun calendar-mayan-date-string (&optional date
)
118 "String of Mayan date of Gregorian DATE; default today."
119 (let* ((d (calendar-absolute-from-gregorian
120 (or date
(calendar-current-date))))
121 (tzolkin (calendar-mayan-tzolkin-from-absolute d
))
122 (haab (calendar-mayan-haab-from-absolute d
))
123 (long-count (calendar-mayan-long-count-from-absolute d
)))
124 (format "Long count = %s; tzolkin = %s; haab = %s"
125 (calendar-mayan-long-count-to-string long-count
)
126 (calendar-mayan-tzolkin-to-string tzolkin
)
127 (calendar-mayan-haab-to-string haab
))))
130 (defun calendar-mayan-print-date ()
131 "Show the Mayan long count, tzolkin, and haab equivalents of date."
133 (message "Mayan date: %s"
134 (calendar-mayan-date-string (calendar-cursor-to-date t
))))
136 (define-obsolete-function-alias 'calendar-print-mayan-date
137 'calendar-mayan-print-date
"23.1")
139 (defun calendar-mayan-read-haab-date ()
140 "Prompt for a Mayan haab date."
141 (let* ((completion-ignore-case t
)
142 (haab-day (calendar-read
144 (lambda (x) (and (>= x
0) (< x
20)))))
145 (haab-month-list (append calendar-mayan-haab-month-name-array
146 (and (< haab-day
5) '("Uayeb"))))
149 (completing-read "Haab uinal: "
150 (mapcar 'list haab-month-list
)
152 (calendar-make-alist haab-month-list
1) t
))))
153 (cons haab-day haab-month
)))
155 (defun calendar-mayan-read-tzolkin-date ()
156 "Prompt for a Mayan tzolkin date."
157 (let* ((completion-ignore-case t
)
158 (tzolkin-count (calendar-read
159 "Tzolkin kin (1-13): "
160 (lambda (x) (and (> x
0) (< x
14)))))
161 (tzolkin-name-list (append calendar-mayan-tzolkin-names-array nil
))
164 (completing-read "Tzolkin uinal: "
165 (mapcar 'list tzolkin-name-list
)
167 (calendar-make-alist tzolkin-name-list
1) t
))))
168 (cons tzolkin-count tzolkin-name
)))
171 (defun calendar-mayan-next-haab-date (haab-date &optional noecho
)
172 "Move cursor to next instance of Mayan HAAB-DATE.
173 Echo Mayan date unless NOECHO is non-nil."
174 (interactive (list (calendar-mayan-read-haab-date)))
176 (calendar-gregorian-from-absolute
177 (calendar-mayan-haab-on-or-before
180 (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
181 (or noecho
(calendar-mayan-print-date)))
183 (define-obsolete-function-alias 'calendar-next-haab-date
184 'calendar-mayan-next-haab-date
"23.1")
187 (defun calendar-mayan-previous-haab-date (haab-date &optional noecho
)
188 "Move cursor to previous instance of Mayan HAAB-DATE.
189 Echo Mayan date unless NOECHO is non-nil."
190 (interactive (list (calendar-mayan-read-haab-date)))
192 (calendar-gregorian-from-absolute
193 (calendar-mayan-haab-on-or-before
195 (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
196 (or noecho
(calendar-mayan-print-date)))
198 (define-obsolete-function-alias 'calendar-previous-haab-date
199 'calendar-mayan-previous-haab-date
"23.1")
201 (defun calendar-mayan-haab-to-string (haab)
202 "Convert Mayan HAAB date (a pair) into its traditional written form."
203 (let ((month (cdr haab
)))
204 (format "%d %s" (car haab
) ; day
205 ;; 19th month consists of 5 special days
206 (if (= month
19) "Uayeb"
207 (aref calendar-mayan-haab-month-name-array
(1- month
))))))
209 (defun calendar-mayan-tzolkin-from-absolute (date)
210 "Convert absolute DATE into a Mayan tzolkin date (a pair)."
211 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero
))
212 ;; Remainder on division by 13,20 with 13,20 instead of zero.
214 (1- (+ long-count
(car calendar-mayan-tzolkin-at-epoch
)))
217 (1- (+ long-count
(cdr calendar-mayan-tzolkin-at-epoch
)))
221 (defun calendar-mayan-tzolkin-difference (date1 date2
)
222 "Number of days from Mayan tzolkin DATE1 to next occurrence of tzolkin DATE2."
223 (let ((number-difference (- (car date2
) (car date1
)))
224 (name-difference (- (cdr date2
) (cdr date1
))))
225 (mod (+ number-difference
226 (* 13 (mod (* 3 (- number-difference name-difference
))
230 (defun calendar-mayan-tzolkin-on-or-before (tzolkin-date date
)
231 "Absolute date of latest TZOLKIN-DATE on or before absolute DATE."
233 (%
(- date
(calendar-mayan-tzolkin-difference
234 (calendar-mayan-tzolkin-from-absolute 0)
239 (defun calendar-mayan-next-tzolkin-date (tzolkin-date &optional noecho
)
240 "Move cursor to next instance of Mayan TZOLKIN-DATE.
241 Echo Mayan date unless NOECHO is non-nil."
242 (interactive (list (calendar-mayan-read-tzolkin-date)))
244 (calendar-gregorian-from-absolute
245 (calendar-mayan-tzolkin-on-or-before
248 (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
249 (or noecho
(calendar-mayan-print-date)))
251 (define-obsolete-function-alias 'calendar-next-tzolkin-date
252 'calendar-mayan-next-tzolkin-date
"23.1")
255 (defun calendar-mayan-previous-tzolkin-date (tzolkin-date &optional noecho
)
256 "Move cursor to previous instance of Mayan TZOLKIN-DATE.
257 Echo Mayan date unless NOECHO is non-nil."
258 (interactive (list (calendar-mayan-read-tzolkin-date)))
260 (calendar-gregorian-from-absolute
261 (calendar-mayan-tzolkin-on-or-before
263 (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
264 (or noecho
(calendar-mayan-print-date)))
266 (define-obsolete-function-alias 'calendar-previous-tzolkin-date
267 'calendar-mayan-previous-tzolkin-date
"23.1")
269 (defun calendar-mayan-tzolkin-to-string (tzolkin)
270 "Convert Mayan TZOLKIN date (a pair) into its traditional written form."
273 (aref calendar-mayan-tzolkin-names-array
(1- (cdr tzolkin
)))))
275 (defun calendar-mayan-tzolkin-haab-on-or-before (tzolkin-date haab-date date
)
276 "Absolute date that is Mayan TZOLKIN-DATE and HAAB-DATE.
277 Latest such date on or before DATE.
278 Returns nil if such a tzolkin-haab combination is impossible."
279 (let* ((haab-difference
280 (calendar-mayan-haab-difference
281 (calendar-mayan-haab-from-absolute 0)
284 (calendar-mayan-tzolkin-difference
285 (calendar-mayan-tzolkin-from-absolute 0)
287 (difference (- tzolkin-difference haab-difference
)))
288 (if (zerop (% difference
5))
291 (+ haab-difference
(* 365 difference
)))
296 (defun calendar-mayan-next-round-date (tzolkin-date haab-date
298 "Move cursor to next instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
299 Echo Mayan date unless NOECHO is non-nil."
300 (interactive (list (calendar-mayan-read-tzolkin-date)
301 (calendar-mayan-read-haab-date)))
302 (let ((date (calendar-mayan-tzolkin-haab-on-or-before
303 tzolkin-date haab-date
304 (+ 18980 (calendar-absolute-from-gregorian
305 (calendar-cursor-to-date))))))
307 (error "%s, %s does not exist in the Mayan calendar round"
308 (calendar-mayan-tzolkin-to-string tzolkin-date
)
309 (calendar-mayan-haab-to-string haab-date
))
310 (calendar-goto-date (calendar-gregorian-from-absolute date
))
311 (or noecho
(calendar-mayan-print-date)))))
313 (define-obsolete-function-alias 'calendar-next-calendar-round-date
314 'calendar-mayan-next-round-date
"23.1")
317 (defun calendar-mayan-previous-round-date
318 (tzolkin-date haab-date
&optional noecho
)
319 "Move to previous instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
320 Echo Mayan date unless NOECHO is non-nil."
321 (interactive (list (calendar-mayan-read-tzolkin-date)
322 (calendar-mayan-read-haab-date)))
323 (let ((date (calendar-mayan-tzolkin-haab-on-or-before
324 tzolkin-date haab-date
325 (1- (calendar-absolute-from-gregorian
326 (calendar-cursor-to-date))))))
328 (error "%s, %s does not exist in the Mayan calendar round"
329 (calendar-mayan-tzolkin-to-string tzolkin-date
)
330 (calendar-mayan-haab-to-string haab-date
))
331 (calendar-goto-date (calendar-gregorian-from-absolute date
))
332 (or noecho
(calendar-mayan-print-date)))))
334 (define-obsolete-function-alias 'calendar-previous-calendar-round-date
335 'calendar-mayan-previous-round-date
"23.1")
337 (defun calendar-mayan-long-count-to-absolute (c)
338 "Compute the absolute date corresponding to the Mayan Long Count C.
339 Long count is a list (baktun katun tun uinal kin)"
340 (+ (* (nth 0 c
) 144000) ; baktun
341 (* (nth 1 c
) 7200) ; katun
342 (* (nth 2 c
) 360) ; tun
343 (* (nth 3 c
) 20) ; uinal
344 (nth 4 c
) ; kin (days)
345 ;; Days before absolute date 0.
346 (- calendar-mayan-days-before-absolute-zero
)))
348 (define-obsolete-function-alias 'calendar-absolute-from-mayan-long-count
349 'calendar-mayan-long-count-to-absolute
"23.1")
351 (defun calendar-mayan-long-count-common-era (lc)
352 "Return non-nil if long count LC represents a date in the Common Era."
353 (let ((base (calendar-mayan-long-count-from-absolute 1)))
354 (while (and base
(= (car lc
) (car base
)))
357 (or (null lc
) (> (car lc
) (car base
)))))
360 (defun calendar-mayan-goto-long-count-date (date &optional noecho
)
361 "Move cursor to Mayan long count DATE.
362 Echo Mayan date unless NOECHO is non-nil."
365 (while (not (setq datum
366 (calendar-mayan-string-from-long-count
368 "Mayan long count (baktun.katun.tun.uinal.kin): "
369 (calendar-mayan-long-count-to-string
370 (calendar-mayan-long-count-from-absolute
371 (calendar-absolute-from-gregorian
372 (calendar-current-date))))))
373 datum
(if (calendar-mayan-long-count-common-era datum
)
377 (calendar-gregorian-from-absolute
378 (calendar-mayan-long-count-to-absolute date
)))
379 (or noecho
(calendar-mayan-print-date)))
381 (define-obsolete-function-alias 'calendar-goto-mayan-long-count-date
382 'calendar-mayan-goto-long-count-date
"23.1")
386 ;; To be called from diary-list-sexp-entries, where DATE is bound.
388 (defun diary-mayan-date ()
389 "Show the Mayan long count, haab, and tzolkin dates as a diary entry."
390 (format "Mayan date: %s" (calendar-mayan-date-string date
)))
394 ;; arch-tag: 54f35144-cd0f-4873-935a-a60129de07df
395 ;;; cal-mayan.el ends here