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, 2009 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 of the License, or
17 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
35 (defconst calendar-mayan-days-before-absolute-zero
1137142
36 "Number of days of the Mayan calendar epoch before absolute day 0.
37 This is the Goodman-Martinez-Thompson correlation used by almost all experts,
38 but some use 1137140. Using 1232041 gives you Spinden's correlation; using
39 1142840 gives you Hochleitner's correlation.")
41 (defconst calendar-mayan-haab-at-epoch
'(8 .
18)
42 "Mayan haab date at the epoch.")
44 (defconst calendar-mayan-haab-month-name-array
45 ["Pop" "Uo" "Zip" "Zotz" "Tzec" "Xul" "Yaxkin" "Mol" "Chen" "Yax"
46 "Zac" "Ceh" "Mac" "Kankin" "Muan" "Pax" "Kayab" "Cumku"]
47 "Names of the Mayan haab months.")
49 (defconst calendar-mayan-tzolkin-at-epoch
'(4 .
20)
50 "Mayan tzolkin date at the epoch.")
52 (defconst calendar-mayan-tzolkin-names-array
53 ["Imix" "Ik" "Akbal" "Kan" "Chicchan" "Cimi" "Manik" "Lamat" "Muluc" "Oc"
54 "Chuen" "Eb" "Ben" "Ix" "Men" "Cib" "Caban" "Etznab" "Cauac" "Ahau"]
55 "Names of the Mayan tzolkin months.")
57 (defun calendar-mayan-long-count-from-absolute (date)
58 "Compute the Mayan long count corresponding to the absolute DATE."
59 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero
))
60 (baktun (/ long-count
144000))
61 (remainder (% long-count
144000))
62 (katun (/ remainder
7200))
63 (remainder (% remainder
7200))
64 (tun (/ remainder
360))
65 (remainder (% remainder
360))
66 (uinal (/ remainder
20))
67 (kin (% remainder
20)))
68 (list baktun katun tun uinal kin
)))
70 (defun calendar-mayan-long-count-to-string (mayan-long-count)
71 "Convert MAYAN-LONG-COUNT into traditional written form."
72 (apply 'format
(cons "%s.%s.%s.%s.%s" mayan-long-count
)))
74 (defun calendar-mayan-string-from-long-count (str)
75 "Given STR, a string of format \"%d.%d.%d.%d.%d\", return list of numbers."
81 (while (string-match "[0-9]+" str end
)
82 (setq rlc
(cons (string-to-number (match-string 0 str
)) rlc
)
84 (unless (= (length rlc
) 5) (signal 'invalid-read-syntax nil
)))
85 (invalid-read-syntax nil
))
88 (defun calendar-mayan-haab-from-absolute (date)
89 "Convert absolute DATE into a Mayan haab date (a pair)."
90 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero
))
93 (car calendar-mayan-haab-at-epoch
)
94 (* 20 (1- (cdr calendar-mayan-haab-at-epoch
))))
96 (day (% day-of-haab
20))
97 (month (1+ (/ day-of-haab
20))))
100 (defun calendar-mayan-haab-difference (date1 date2
)
101 "Number of days from Mayan haab DATE1 to next occurrence of haab date DATE2."
102 (mod (+ (* 20 (- (cdr date2
) (cdr date1
)))
103 (- (car date2
) (car date1
)))
106 (defun calendar-mayan-haab-on-or-before (haab-date date
)
107 "Absolute date of latest HAAB-DATE on or before absolute DATE."
110 (calendar-mayan-haab-difference
111 (calendar-mayan-haab-from-absolute 0) haab-date
))
115 (defun calendar-mayan-date-string (&optional date
)
116 "String of Mayan date of Gregorian DATE; default today."
117 (let* ((d (calendar-absolute-from-gregorian
118 (or date
(calendar-current-date))))
119 (tzolkin (calendar-mayan-tzolkin-from-absolute d
))
120 (haab (calendar-mayan-haab-from-absolute d
))
121 (long-count (calendar-mayan-long-count-from-absolute d
)))
122 (format "Long count = %s; tzolkin = %s; haab = %s"
123 (calendar-mayan-long-count-to-string long-count
)
124 (calendar-mayan-tzolkin-to-string tzolkin
)
125 (calendar-mayan-haab-to-string haab
))))
128 (defun calendar-mayan-print-date ()
129 "Show the Mayan long count, tzolkin, and haab equivalents of date."
131 (message "Mayan date: %s"
132 (calendar-mayan-date-string (calendar-cursor-to-date t
))))
134 (define-obsolete-function-alias 'calendar-print-mayan-date
135 'calendar-mayan-print-date
"23.1")
137 (defun calendar-mayan-read-haab-date ()
138 "Prompt for a Mayan haab date."
139 (let* ((completion-ignore-case t
)
140 (haab-day (calendar-read
142 (lambda (x) (and (>= x
0) (< x
20)))))
143 (haab-month-list (append calendar-mayan-haab-month-name-array
144 (and (< haab-day
5) '("Uayeb"))))
147 (completing-read "Haab uinal: "
148 (mapcar 'list haab-month-list
)
150 (calendar-make-alist haab-month-list
1) t
))))
151 (cons haab-day haab-month
)))
153 (defun calendar-mayan-read-tzolkin-date ()
154 "Prompt for a Mayan tzolkin date."
155 (let* ((completion-ignore-case t
)
156 (tzolkin-count (calendar-read
157 "Tzolkin kin (1-13): "
158 (lambda (x) (and (> x
0) (< x
14)))))
159 (tzolkin-name-list (append calendar-mayan-tzolkin-names-array nil
))
162 (completing-read "Tzolkin uinal: "
163 (mapcar 'list tzolkin-name-list
)
165 (calendar-make-alist tzolkin-name-list
1) t
))))
166 (cons tzolkin-count tzolkin-name
)))
169 (defun calendar-mayan-next-haab-date (haab-date &optional noecho
)
170 "Move cursor to next instance of Mayan HAAB-DATE.
171 Echo Mayan date unless NOECHO is non-nil."
172 (interactive (list (calendar-mayan-read-haab-date)))
174 (calendar-gregorian-from-absolute
175 (calendar-mayan-haab-on-or-before
178 (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
179 (or noecho
(calendar-mayan-print-date)))
181 (define-obsolete-function-alias 'calendar-next-haab-date
182 'calendar-mayan-next-haab-date
"23.1")
185 (defun calendar-mayan-previous-haab-date (haab-date &optional noecho
)
186 "Move cursor to previous instance of Mayan HAAB-DATE.
187 Echo Mayan date unless NOECHO is non-nil."
188 (interactive (list (calendar-mayan-read-haab-date)))
190 (calendar-gregorian-from-absolute
191 (calendar-mayan-haab-on-or-before
193 (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
194 (or noecho
(calendar-mayan-print-date)))
196 (define-obsolete-function-alias 'calendar-previous-haab-date
197 'calendar-mayan-previous-haab-date
"23.1")
199 (defun calendar-mayan-haab-to-string (haab)
200 "Convert Mayan HAAB date (a pair) into its traditional written form."
201 (let ((month (cdr haab
)))
202 (format "%d %s" (car haab
) ; day
203 ;; 19th month consists of 5 special days
204 (if (= month
19) "Uayeb"
205 (aref calendar-mayan-haab-month-name-array
(1- month
))))))
207 (defun calendar-mayan-tzolkin-from-absolute (date)
208 "Convert absolute DATE into a Mayan tzolkin date (a pair)."
209 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero
))
210 ;; Remainder on division by 13,20 with 13,20 instead of zero.
212 (1- (+ long-count
(car calendar-mayan-tzolkin-at-epoch
)))
215 (1- (+ long-count
(cdr calendar-mayan-tzolkin-at-epoch
)))
219 (defun calendar-mayan-tzolkin-difference (date1 date2
)
220 "Number of days from Mayan tzolkin DATE1 to next occurrence of tzolkin DATE2."
221 (let ((number-difference (- (car date2
) (car date1
)))
222 (name-difference (- (cdr date2
) (cdr date1
))))
223 (mod (+ number-difference
224 (* 13 (mod (* 3 (- number-difference name-difference
))
228 (defun calendar-mayan-tzolkin-on-or-before (tzolkin-date date
)
229 "Absolute date of latest TZOLKIN-DATE on or before absolute DATE."
231 (%
(- date
(calendar-mayan-tzolkin-difference
232 (calendar-mayan-tzolkin-from-absolute 0)
237 (defun calendar-mayan-next-tzolkin-date (tzolkin-date &optional noecho
)
238 "Move cursor to next instance of Mayan TZOLKIN-DATE.
239 Echo Mayan date unless NOECHO is non-nil."
240 (interactive (list (calendar-mayan-read-tzolkin-date)))
242 (calendar-gregorian-from-absolute
243 (calendar-mayan-tzolkin-on-or-before
246 (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
247 (or noecho
(calendar-mayan-print-date)))
249 (define-obsolete-function-alias 'calendar-next-tzolkin-date
250 'calendar-mayan-next-tzolkin-date
"23.1")
253 (defun calendar-mayan-previous-tzolkin-date (tzolkin-date &optional noecho
)
254 "Move cursor to previous instance of Mayan TZOLKIN-DATE.
255 Echo Mayan date unless NOECHO is non-nil."
256 (interactive (list (calendar-mayan-read-tzolkin-date)))
258 (calendar-gregorian-from-absolute
259 (calendar-mayan-tzolkin-on-or-before
261 (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
262 (or noecho
(calendar-mayan-print-date)))
264 (define-obsolete-function-alias 'calendar-previous-tzolkin-date
265 'calendar-mayan-previous-tzolkin-date
"23.1")
267 (defun calendar-mayan-tzolkin-to-string (tzolkin)
268 "Convert Mayan TZOLKIN date (a pair) into its traditional written form."
271 (aref calendar-mayan-tzolkin-names-array
(1- (cdr tzolkin
)))))
273 (defun calendar-mayan-tzolkin-haab-on-or-before (tzolkin-date haab-date date
)
274 "Absolute date that is Mayan TZOLKIN-DATE and HAAB-DATE.
275 Latest such date on or before DATE.
276 Returns nil if such a tzolkin-haab combination is impossible."
277 (let* ((haab-difference
278 (calendar-mayan-haab-difference
279 (calendar-mayan-haab-from-absolute 0)
282 (calendar-mayan-tzolkin-difference
283 (calendar-mayan-tzolkin-from-absolute 0)
285 (difference (- tzolkin-difference haab-difference
)))
286 (if (zerop (% difference
5))
289 (+ haab-difference
(* 365 difference
)))
294 (defun calendar-mayan-next-round-date (tzolkin-date haab-date
296 "Move cursor to next instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
297 Echo Mayan date unless NOECHO is non-nil."
298 (interactive (list (calendar-mayan-read-tzolkin-date)
299 (calendar-mayan-read-haab-date)))
300 (let ((date (calendar-mayan-tzolkin-haab-on-or-before
301 tzolkin-date haab-date
302 (+ 18980 (calendar-absolute-from-gregorian
303 (calendar-cursor-to-date))))))
305 (error "%s, %s does not exist in the Mayan calendar round"
306 (calendar-mayan-tzolkin-to-string tzolkin-date
)
307 (calendar-mayan-haab-to-string haab-date
))
308 (calendar-goto-date (calendar-gregorian-from-absolute date
))
309 (or noecho
(calendar-mayan-print-date)))))
311 (define-obsolete-function-alias 'calendar-next-calendar-round-date
312 'calendar-mayan-next-round-date
"23.1")
315 (defun calendar-mayan-previous-round-date
316 (tzolkin-date haab-date
&optional noecho
)
317 "Move to previous instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
318 Echo Mayan date unless NOECHO is non-nil."
319 (interactive (list (calendar-mayan-read-tzolkin-date)
320 (calendar-mayan-read-haab-date)))
321 (let ((date (calendar-mayan-tzolkin-haab-on-or-before
322 tzolkin-date haab-date
323 (1- (calendar-absolute-from-gregorian
324 (calendar-cursor-to-date))))))
326 (error "%s, %s does not exist in the Mayan calendar round"
327 (calendar-mayan-tzolkin-to-string tzolkin-date
)
328 (calendar-mayan-haab-to-string haab-date
))
329 (calendar-goto-date (calendar-gregorian-from-absolute date
))
330 (or noecho
(calendar-mayan-print-date)))))
332 (define-obsolete-function-alias 'calendar-previous-calendar-round-date
333 'calendar-mayan-previous-round-date
"23.1")
335 (defun calendar-mayan-long-count-to-absolute (c)
336 "Compute the absolute date corresponding to the Mayan Long Count C.
337 Long count is a list (baktun katun tun uinal kin)"
338 (+ (* (nth 0 c
) 144000) ; baktun
339 (* (nth 1 c
) 7200) ; katun
340 (* (nth 2 c
) 360) ; tun
341 (* (nth 3 c
) 20) ; uinal
342 (nth 4 c
) ; kin (days)
343 ;; Days before absolute date 0.
344 (- calendar-mayan-days-before-absolute-zero
)))
346 (define-obsolete-function-alias 'calendar-absolute-from-mayan-long-count
347 'calendar-mayan-long-count-to-absolute
"23.1")
349 (defun calendar-mayan-long-count-common-era (lc)
350 "Return non-nil if long count LC represents a date in the Common Era."
351 (let ((base (calendar-mayan-long-count-from-absolute 1)))
352 (while (and base
(= (car lc
) (car base
)))
355 (or (null lc
) (> (car lc
) (car base
)))))
358 (defun calendar-mayan-goto-long-count-date (date &optional noecho
)
359 "Move cursor to Mayan long count DATE.
360 Echo Mayan date unless NOECHO is non-nil."
363 (while (not (setq datum
364 (calendar-mayan-string-from-long-count
366 "Mayan long count (baktun.katun.tun.uinal.kin): "
367 (calendar-mayan-long-count-to-string
368 (calendar-mayan-long-count-from-absolute
369 (calendar-absolute-from-gregorian
370 (calendar-current-date))))))
371 datum
(if (calendar-mayan-long-count-common-era datum
)
375 (calendar-gregorian-from-absolute
376 (calendar-mayan-long-count-to-absolute date
)))
377 (or noecho
(calendar-mayan-print-date)))
379 (define-obsolete-function-alias 'calendar-goto-mayan-long-count-date
380 'calendar-mayan-goto-long-count-date
"23.1")
384 ;; To be called from diary-list-sexp-entries, where DATE is bound.
386 (defun diary-mayan-date ()
387 "Show the Mayan long count, haab, and tzolkin dates as a diary entry."
388 (format "Mayan date: %s" (calendar-mayan-date-string date
)))
392 ;; arch-tag: 54f35144-cd0f-4873-935a-a60129de07df
393 ;;; cal-mayan.el ends here