(calendar-mayan-haab-to-string): Simplify.
[emacs.git] / lisp / calendar / cal-mayan.el
blobc36a82bc3e996b67473e38172b755ffdf02c5501
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>
9 ;; Keywords: calendar
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)
17 ;; 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; 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.
29 ;;; Commentary:
31 ;; This collection of functions implements the features of calendar.el and
32 ;; diary.el that deal with the Mayan calendar. It was written jointly by
34 ;; Stewart M. Clamen School of Computer Science
35 ;; clamen@cs.cmu.edu Carnegie Mellon University
36 ;; 5000 Forbes Avenue
37 ;; Pittsburgh, PA 15213
39 ;; and
41 ;; Edward M. Reingold Department of Computer Science
42 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
43 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
44 ;; Urbana, Illinois 61801
46 ;; Technical details of the Mayan calendrical calculations can be found in
47 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
48 ;; and Nachum Dershowitz, Cambridge University Press (2001), and in
49 ;; ``Calendrical Calculations, Part II: Three Historical Calendars''
50 ;; by E. M. Reingold, N. Dershowitz, and S. M. Clamen,
51 ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
52 ;; pages 383-404.
54 ;;; Code:
56 (require 'calendar)
58 (defconst calendar-mayan-days-before-absolute-zero 1137142
59 "Number of days of the Mayan calendar epoch before absolute day 0.
60 This is the Goodman-Martinez-Thompson correlation used by almost all experts,
61 but some use 1137140. Using 1232041 gives you Spinden's correlation; using
62 1142840 gives you Hochleitner's correlation.")
64 (defconst calendar-mayan-haab-at-epoch '(8 . 18)
65 "Mayan haab date at the epoch.")
67 (defconst calendar-mayan-haab-month-name-array
68 ["Pop" "Uo" "Zip" "Zotz" "Tzec" "Xul" "Yaxkin" "Mol" "Chen" "Yax"
69 "Zac" "Ceh" "Mac" "Kankin" "Muan" "Pax" "Kayab" "Cumku"]
70 "Names of the Mayan haab months.")
72 (defconst calendar-mayan-tzolkin-at-epoch '(4 . 20)
73 "Mayan tzolkin date at the epoch.")
75 (defconst calendar-mayan-tzolkin-names-array
76 ["Imix" "Ik" "Akbal" "Kan" "Chicchan" "Cimi" "Manik" "Lamat" "Muluc" "Oc"
77 "Chuen" "Eb" "Ben" "Ix" "Men" "Cib" "Caban" "Etznab" "Cauac" "Ahau"]
78 "Names of the Mayan tzolkin months.")
80 (defun calendar-mayan-long-count-from-absolute (date)
81 "Compute the Mayan long count corresponding to the absolute DATE."
82 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
83 (baktun (/ long-count 144000))
84 (remainder (% long-count 144000))
85 (katun (/ remainder 7200))
86 (remainder (% remainder 7200))
87 (tun (/ remainder 360))
88 (remainder (% remainder 360))
89 (uinal (/ remainder 20))
90 (kin (% remainder 20)))
91 (list baktun katun tun uinal kin)))
93 (defun calendar-mayan-long-count-to-string (mayan-long-count)
94 "Convert MAYAN-LONG-COUNT into traditional written form."
95 (apply 'format (cons "%s.%s.%s.%s.%s" mayan-long-count)))
97 (defun calendar-string-to-mayan-long-count (str)
98 "Given STR, a string of format \"%d.%d.%d.%d.%d\", return list of numbers."
99 (let ((c (length str))
100 (cc 0)
101 rlc)
102 (condition-case condition
103 (progn
104 (while (< cc c)
105 (let* ((start (string-match "[0-9]+" str cc))
106 (end (match-end 0))
107 (datum (read (substring str start end))))
108 (setq rlc (cons datum rlc)
109 cc end)))
110 (unless (= (length rlc) 5) (signal 'invalid-read-syntax nil)))
111 (invalid-read-syntax nil))
112 (reverse rlc)))
114 (defun calendar-mayan-haab-from-absolute (date)
115 "Convert absolute DATE into a Mayan haab date (a pair)."
116 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
117 (day-of-haab
118 (% (+ long-count
119 (car calendar-mayan-haab-at-epoch)
120 (* 20 (1- (cdr calendar-mayan-haab-at-epoch))))
121 365))
122 (day (% day-of-haab 20))
123 (month (1+ (/ day-of-haab 20))))
124 (cons day month)))
126 (defun calendar-mayan-haab-difference (date1 date2)
127 "Number of days from Mayan haab DATE1 to next occurrence of haab date DATE2."
128 (mod (+ (* 20 (- (cdr date2) (cdr date1)))
129 (- (car date2) (car date1)))
130 365))
132 (defun calendar-mayan-haab-on-or-before (haab-date date)
133 "Absolute date of latest HAAB-DATE on or before absolute DATE."
134 (- date
135 (% (- date
136 (calendar-mayan-haab-difference
137 (calendar-mayan-haab-from-absolute 0) haab-date))
138 365)))
140 ;;;###cal-autoload
141 (defun calendar-mayan-date-string (&optional date)
142 "String of Mayan date of Gregorian DATE; default today."
143 (let* ((d (calendar-absolute-from-gregorian
144 (or date (calendar-current-date))))
145 (tzolkin (calendar-mayan-tzolkin-from-absolute d))
146 (haab (calendar-mayan-haab-from-absolute d))
147 (long-count (calendar-mayan-long-count-from-absolute d)))
148 (format "Long count = %s; tzolkin = %s; haab = %s"
149 (calendar-mayan-long-count-to-string long-count)
150 (calendar-mayan-tzolkin-to-string tzolkin)
151 (calendar-mayan-haab-to-string haab))))
153 ;;;###cal-autoload
154 (defun calendar-print-mayan-date ()
155 "Show the Mayan long count, tzolkin, and haab equivalents of date."
156 (interactive)
157 (message "Mayan date: %s"
158 (calendar-mayan-date-string (calendar-cursor-to-date t))))
160 (defun calendar-read-mayan-haab-date ()
161 "Prompt for a Mayan haab date."
162 (let* ((completion-ignore-case t)
163 (haab-day (calendar-read
164 "Haab kin (0-19): "
165 (lambda (x) (and (>= x 0) (< x 20)))))
166 (haab-month-list (append calendar-mayan-haab-month-name-array
167 (and (< haab-day 5) '("Uayeb"))))
168 (haab-month (cdr
169 (assoc-string
170 (completing-read "Haab uinal: "
171 (mapcar 'list haab-month-list)
172 nil t)
173 (calendar-make-alist haab-month-list 1) t))))
174 (cons haab-day haab-month)))
176 (defun calendar-read-mayan-tzolkin-date ()
177 "Prompt for a Mayan tzolkin date."
178 (let* ((completion-ignore-case t)
179 (tzolkin-count (calendar-read
180 "Tzolkin kin (1-13): "
181 (lambda (x) (and (> x 0) (< x 14)))))
182 (tzolkin-name-list (append calendar-mayan-tzolkin-names-array nil))
183 (tzolkin-name (cdr
184 (assoc-string
185 (completing-read "Tzolkin uinal: "
186 (mapcar 'list tzolkin-name-list)
187 nil t)
188 (calendar-make-alist tzolkin-name-list 1) t))))
189 (cons tzolkin-count tzolkin-name)))
191 ;;;###cal-autoload
192 (defun calendar-next-haab-date (haab-date &optional noecho)
193 "Move cursor to next instance of Mayan HAAB-DATE.
194 Echo Mayan date unless NOECHO is non-nil."
195 (interactive (list (calendar-read-mayan-haab-date)))
196 (calendar-goto-date
197 (calendar-gregorian-from-absolute
198 (calendar-mayan-haab-on-or-before
199 haab-date
200 (+ 365
201 (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
202 (or noecho (calendar-print-mayan-date)))
204 ;;;###cal-autoload
205 (defun calendar-previous-haab-date (haab-date &optional noecho)
206 "Move cursor to previous instance of Mayan HAAB-DATE.
207 Echo Mayan date unless NOECHO is non-nil."
208 (interactive (list (calendar-read-mayan-haab-date)))
209 (calendar-goto-date
210 (calendar-gregorian-from-absolute
211 (calendar-mayan-haab-on-or-before
212 haab-date
213 (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
214 (or noecho (calendar-print-mayan-date)))
216 (defun calendar-mayan-haab-to-string (haab)
217 "Convert Mayan HAAB date (a pair) into its traditional written form."
218 (let ((month (cdr haab)))
219 (format "%d %s" (car haab) ; day
220 ;; 19th month consists of 5 special days
221 (if (= month 19) "Uayeb"
222 (aref calendar-mayan-haab-month-name-array (1- month))))))
224 (defun calendar-mayan-tzolkin-from-absolute (date)
225 "Convert absolute DATE into a Mayan tzolkin date (a pair)."
226 (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
227 ;; Remainder on division by 13,20 with 13,20 instead of zero.
228 (day (1+ (mod
229 (1- (+ long-count (car calendar-mayan-tzolkin-at-epoch)))
230 13)))
231 (name (1+ (mod
232 (1- (+ long-count (cdr calendar-mayan-tzolkin-at-epoch)))
233 20))))
234 (cons day name)))
236 (defun calendar-mayan-tzolkin-difference (date1 date2)
237 "Number of days from Mayan tzolkin DATE1 to next occurrence of tzolkin DATE2."
238 (let ((number-difference (- (car date2) (car date1)))
239 (name-difference (- (cdr date2) (cdr date1))))
240 (mod (+ number-difference
241 (* 13 (mod (* 3 (- number-difference name-difference))
242 20)))
243 260)))
245 (defun calendar-mayan-tzolkin-on-or-before (tzolkin-date date)
246 "Absolute date of latest TZOLKIN-DATE on or before absolute DATE."
247 (- date
248 (% (- date (calendar-mayan-tzolkin-difference
249 (calendar-mayan-tzolkin-from-absolute 0)
250 tzolkin-date))
251 260)))
253 ;;;###cal-autoload
254 (defun calendar-next-tzolkin-date (tzolkin-date &optional noecho)
255 "Move cursor to next instance of Mayan TZOLKIN-DATE.
256 Echo Mayan date unless NOECHO is non-nil."
257 (interactive (list (calendar-read-mayan-tzolkin-date)))
258 (calendar-goto-date
259 (calendar-gregorian-from-absolute
260 (calendar-mayan-tzolkin-on-or-before
261 tzolkin-date
262 (+ 260
263 (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
264 (or noecho (calendar-print-mayan-date)))
266 ;;;###cal-autoload
267 (defun calendar-previous-tzolkin-date (tzolkin-date &optional noecho)
268 "Move cursor to previous instance of Mayan TZOLKIN-DATE.
269 Echo Mayan date unless NOECHO is non-nil."
270 (interactive (list (calendar-read-mayan-tzolkin-date)))
271 (calendar-goto-date
272 (calendar-gregorian-from-absolute
273 (calendar-mayan-tzolkin-on-or-before
274 tzolkin-date
275 (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
276 (or noecho (calendar-print-mayan-date)))
278 (defun calendar-mayan-tzolkin-to-string (tzolkin)
279 "Convert Mayan TZOLKIN date (a pair) into its traditional written form."
280 (format "%d %s"
281 (car tzolkin)
282 (aref calendar-mayan-tzolkin-names-array (1- (cdr tzolkin)))))
284 (defun calendar-mayan-tzolkin-haab-on-or-before (tzolkin-date haab-date date)
285 "Absolute date that is Mayan TZOLKIN-DATE and HAAB-DATE.
286 Latest such date on or before DATE.
287 Returns nil if such a tzolkin-haab combination is impossible."
288 (let* ((haab-difference
289 (calendar-mayan-haab-difference
290 (calendar-mayan-haab-from-absolute 0)
291 haab-date))
292 (tzolkin-difference
293 (calendar-mayan-tzolkin-difference
294 (calendar-mayan-tzolkin-from-absolute 0)
295 tzolkin-date))
296 (difference (- tzolkin-difference haab-difference)))
297 (if (zerop (% difference 5))
298 (- date
299 (mod (- date
300 (+ haab-difference (* 365 difference)))
301 18980))
302 nil)))
304 ;;;###cal-autoload
305 (defun calendar-next-calendar-round-date (tzolkin-date haab-date
306 &optional noecho)
307 "Move cursor to next instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
308 Echo Mayan date unless NOECHO is non-nil."
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 (+ 18980 (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)))))
322 ;;;###cal-autoload
323 (defun calendar-previous-calendar-round-date
324 (tzolkin-date haab-date &optional noecho)
325 "Move to previous instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
326 Echo Mayan date unless NOECHO is non-nil."
327 (interactive (list (calendar-read-mayan-tzolkin-date)
328 (calendar-read-mayan-haab-date)))
329 (let ((date (calendar-mayan-tzolkin-haab-on-or-before
330 tzolkin-date haab-date
331 (1- (calendar-absolute-from-gregorian
332 (calendar-cursor-to-date))))))
333 (if (not date)
334 (error "%s, %s does not exist in the Mayan calendar round"
335 (calendar-mayan-tzolkin-to-string tzolkin-date)
336 (calendar-mayan-haab-to-string haab-date))
337 (calendar-goto-date (calendar-gregorian-from-absolute date))
338 (or noecho (calendar-print-mayan-date)))))
340 (defun calendar-absolute-from-mayan-long-count (c)
341 "Compute the absolute date corresponding to the Mayan Long Count C.
342 Long count is a list (baktun katun tun uinal kin)"
343 (+ (* (nth 0 c) 144000) ; baktun
344 (* (nth 1 c) 7200) ; katun
345 (* (nth 2 c) 360) ; tun
346 (* (nth 3 c) 20) ; uinal
347 (nth 4 c) ; kin (days)
348 ;; Days before absolute date 0.
349 (- calendar-mayan-days-before-absolute-zero)))
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)))
355 (setq lc (cdr lc)
356 base (cdr base)))
357 (or (null lc) (> (car lc) (car base)))))
359 ;;;###cal-autoload
360 (defun calendar-goto-mayan-long-count-date (date &optional noecho)
361 "Move cursor to Mayan long count DATE.
362 Echo Mayan date unless NOECHO is non-nil."
363 (interactive
364 (let (lc)
365 (while (not lc)
366 (let ((datum
367 (calendar-string-to-mayan-long-count
368 (read-string "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 (if (calendar-mayan-long-count-common-era datum)
374 (setq lc datum))))
375 (list lc)))
376 (calendar-goto-date
377 (calendar-gregorian-from-absolute
378 (calendar-absolute-from-mayan-long-count date)))
379 (or noecho (calendar-print-mayan-date)))
381 (defvar date)
383 ;; To be called from list-sexp-diary-entries, where DATE is bound.
384 ;;;###diary-autoload
385 (defun diary-mayan-date ()
386 "Show the Mayan long count, haab, and tzolkin dates as a diary entry."
387 (format "Mayan date: %s" (calendar-mayan-date-string date)))
389 (provide 'cal-mayan)
391 ;; arch-tag: 54f35144-cd0f-4873-935a-a60129de07df
392 ;;; cal-mayan.el ends here