cal-mayan.el 15.8 KB
Newer Older
1
;;; cal-mayan.el --- calendar functions for the Mayan calendars
Jim Blandy's avatar
Jim Blandy committed
2

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1992, 1993, 1995, 1997, 2001, 2002, 2003, 2004, 2005,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2006, 2007, 2008  Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6

;; Author: Stewart M. Clamen <clamen@cs.cmu.edu>
Glenn Morris's avatar
Glenn Morris committed
7
;;         Edward M. Reingold <reingold@cs.uiuc.edu>
8
;; Maintainer: Glenn Morris <rgm@gnu.org>
Eric S. Raymond's avatar
Eric S. Raymond committed
9 10
;; Keywords: calendar
;; Human-Keywords: Mayan calendar, Maya, calendar, diary
Jim Blandy's avatar
Jim Blandy committed
11 12 13

;; This file is part of GNU Emacs.

14 15
;; GNU Emacs is free software; you can redistribute it and/or modify
;; 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 18
;; any later version.

Jim Blandy's avatar
Jim Blandy committed
19
;; GNU Emacs is distributed in the hope that it will be useful,
20 21 22 23 24
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
25
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
26 27
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Jim Blandy's avatar
Jim Blandy committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

;;; Commentary:

;; This collection of functions implements the features of calendar.el and
;; diary.el that deal with the Mayan calendar.  It was written jointly by

;;  Stewart M. Clamen                School of Computer Science
;;  clamen@cs.cmu.edu                Carnegie Mellon University
;;                                   5000 Forbes Avenue
;;                                   Pittsburgh, PA 15213

;; and

;;  Edward M. Reingold               Department of Computer Science
;;  (217) 333-6733                   University of Illinois at Urbana-Champaign
;;  reingold@cs.uiuc.edu             1304 West Springfield Avenue
;;                                   Urbana, Illinois 61801

;; Technical details of the Mayan calendrical calculations can be found in
47 48
;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
;; and Nachum Dershowitz, Cambridge University Press (2001), and in
Jim Blandy's avatar
Jim Blandy committed
49 50
;; ``Calendrical Calculations, Part II: Three Historical Calendars''
;; by E. M. Reingold,  N. Dershowitz, and S. M. Clamen,
51 52
;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
;; pages 383-404.
Jim Blandy's avatar
Jim Blandy committed
53 54 55 56 57

;;; Code:

(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 61 62
This is the Goodman-Martinez-Thompson correlation used by almost all experts,
but some use 1137140.  Using 1232041 gives you Spinden's correlation; using
1142840 gives you Hochleitner's correlation.")
Jim Blandy's avatar
Jim Blandy committed
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

(defconst calendar-mayan-haab-at-epoch '(8 . 18)
  "Mayan haab date at the epoch.")

(defconst calendar-mayan-haab-month-name-array
  ["Pop" "Uo" "Zip" "Zotz" "Tzec" "Xul" "Yaxkin" "Mol" "Chen" "Yax"
   "Zac" "Ceh" "Mac" "Kankin" "Muan" "Pax" "Kayab" "Cumku"])

(defconst calendar-mayan-tzolkin-at-epoch '(4 . 20)
  "Mayan tzolkin date at the epoch.")

(defconst calendar-mayan-tzolkin-names-array
  ["Imix" "Ik" "Akbal" "Kan" "Chicchan" "Cimi" "Manik" "Lamat" "Muluc" "Oc"
   "Chuen" "Eb" "Ben" "Ix" "Men" "Cib" "Caban" "Etznab" "Cauac" "Ahau"])

(defun calendar-mayan-long-count-from-absolute (date)
  "Compute the Mayan long count corresponding to the absolute DATE."
  (let ((long-count (+ date calendar-mayan-days-before-absolute-zero)))
    (let* ((baktun (/ long-count 144000))
           (remainder (% long-count 144000))
           (katun (/ remainder 7200))
           (remainder (% remainder 7200))
           (tun (/ remainder 360))
           (remainder (% remainder 360))
           (uinal (/ remainder 20))
           (kin (% remainder 20)))
      (list baktun katun tun uinal kin))))

(defun calendar-mayan-long-count-to-string (mayan-long-count)
  "Convert MAYAN-LONG-COUNT into traditional written form."
  (apply 'format (cons "%s.%s.%s.%s.%s" mayan-long-count)))

(defun calendar-string-to-mayan-long-count (str)
96
  "Given STR, a string of format \"%d.%d.%d.%d.%d\", return list of numbers."
Jim Blandy's avatar
Jim Blandy committed
97 98 99 100 101 102
  (let ((rlc nil)
        (c (length str))
        (cc 0))
    (condition-case condition
        (progn
          (while (< cc c)
Glenn Morris's avatar
Glenn Morris committed
103 104 105 106 107 108
            (let* ((start (string-match "[0-9]+" str cc))
                   (end (match-end 0))
                   datum)
              (setq datum (read (substring str start end)))
              (setq rlc (cons datum rlc))
              (setq cc end)))
Jim Blandy's avatar
Jim Blandy committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
          (if (not (= (length rlc) 5)) (signal 'invalid-read-syntax nil)))
      (invalid-read-syntax nil))
    (reverse rlc)))

(defun calendar-mayan-haab-from-absolute (date)
  "Convert absolute DATE into a Mayan haab date (a pair)."
  (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
         (day-of-haab
          (% (+ long-count
                (car calendar-mayan-haab-at-epoch)
                (* 20 (1- (cdr calendar-mayan-haab-at-epoch))))
             365))
         (day (% day-of-haab 20))
         (month (1+ (/ day-of-haab 20))))
    (cons day month)))

(defun calendar-mayan-haab-difference (date1 date2)
126
  "Number of days from Mayan haab DATE1 to next occurrence of haab date DATE2."
127
  (mod (+ (* 20 (- (cdr date2) (cdr date1)))
Glenn Morris's avatar
Glenn Morris committed
128
          (- (car date2) (car date1)))
129
       365))
Jim Blandy's avatar
Jim Blandy committed
130 131 132

(defun calendar-mayan-haab-on-or-before (haab-date date)
  "Absolute date of latest HAAB-DATE on or before absolute DATE."
133 134
  (- date
     (% (- date
Glenn Morris's avatar
Glenn Morris committed
135 136 137
           (calendar-mayan-haab-difference
            (calendar-mayan-haab-from-absolute 0) haab-date))
        365)))
Jim Blandy's avatar
Jim Blandy committed
138

139
;;;###cal-autoload
Jim Blandy's avatar
Jim Blandy committed
140
(defun calendar-next-haab-date (haab-date &optional noecho)
141
  "Move cursor to next instance of Mayan HAAB-DATE.
Jim Blandy's avatar
Jim Blandy committed
142 143 144 145 146 147 148 149 150 151
Echo Mayan date if NOECHO is t."
  (interactive (list (calendar-read-mayan-haab-date)))
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-mayan-haab-on-or-before
     haab-date
     (+ 365
        (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
  (or noecho (calendar-print-mayan-date)))

152
;;;###cal-autoload
Jim Blandy's avatar
Jim Blandy committed
153
(defun calendar-previous-haab-date (haab-date &optional noecho)
154
  "Move cursor to previous instance of Mayan HAAB-DATE.
Jim Blandy's avatar
Jim Blandy committed
155 156 157 158 159 160 161 162 163 164
Echo Mayan date if NOECHO is t."
  (interactive (list (calendar-read-mayan-haab-date)))
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-mayan-haab-on-or-before
     haab-date
     (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
  (or noecho (calendar-print-mayan-date)))

(defun calendar-mayan-haab-to-string (haab)
165
  "Convert Mayan HAAB date (a pair) into its traditional written form."
Jim Blandy's avatar
Jim Blandy committed
166 167
  (let ((month (cdr haab))
        (day (car haab)))
Glenn Morris's avatar
Glenn Morris committed
168 169 170 171 172 173
    ;; 19th month consists of 5 special days
    (if (= month 19)
        (format "%d Uayeb" day)
      (format "%d %s"
              day
              (aref calendar-mayan-haab-month-name-array (1- month))))))
Jim Blandy's avatar
Jim Blandy committed
174 175 176 177

(defun calendar-mayan-tzolkin-from-absolute (date)
  "Convert absolute DATE into a Mayan tzolkin date (a pair)."
  (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
Edward M. Reingold's avatar
Edward M. Reingold committed
178
         (day (calendar-mod
Jim Blandy's avatar
Jim Blandy committed
179 180
               (+ long-count (car calendar-mayan-tzolkin-at-epoch))
               13))
Edward M. Reingold's avatar
Edward M. Reingold committed
181
         (name (calendar-mod
Jim Blandy's avatar
Jim Blandy committed
182 183 184 185 186
                (+ long-count (cdr calendar-mayan-tzolkin-at-epoch))
                20)))
    (cons day name)))

(defun calendar-mayan-tzolkin-difference (date1 date2)
187
  "Number of days from Mayan tzolkin DATE1 to next occurrence of tzolkin DATE2."
Jim Blandy's avatar
Jim Blandy committed
188 189
  (let ((number-difference (- (car date2) (car date1)))
        (name-difference (- (cdr date2) (cdr date1))))
190
    (mod (+ number-difference
Glenn Morris's avatar
Glenn Morris committed
191 192 193
            (* 13 (mod (* 3 (- number-difference name-difference))
                       20)))
         260)))
Jim Blandy's avatar
Jim Blandy committed
194 195 196

(defun calendar-mayan-tzolkin-on-or-before (tzolkin-date date)
  "Absolute date of latest TZOLKIN-DATE on or before absolute DATE."
197 198
  (- date
     (% (- date (calendar-mayan-tzolkin-difference
Glenn Morris's avatar
Glenn Morris committed
199 200 201
                 (calendar-mayan-tzolkin-from-absolute 0)
                 tzolkin-date))
        260)))
Jim Blandy's avatar
Jim Blandy committed
202

203
;;;###cal-autoload
Jim Blandy's avatar
Jim Blandy committed
204
(defun calendar-next-tzolkin-date (tzolkin-date &optional noecho)
205
  "Move cursor to next instance of Mayan TZOLKIN-DATE.
Jim Blandy's avatar
Jim Blandy committed
206 207 208 209 210 211 212 213 214 215
Echo Mayan date if NOECHO is t."
  (interactive (list (calendar-read-mayan-tzolkin-date)))
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-mayan-tzolkin-on-or-before
     tzolkin-date
     (+ 260
        (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
  (or noecho (calendar-print-mayan-date)))

216
;;;###cal-autoload
Jim Blandy's avatar
Jim Blandy committed
217
(defun calendar-previous-tzolkin-date (tzolkin-date &optional noecho)
218
  "Move cursor to previous instance of Mayan TZOLKIN-DATE.
Jim Blandy's avatar
Jim Blandy committed
219 220 221 222 223 224 225 226 227 228
Echo Mayan date if NOECHO is t."
  (interactive (list (calendar-read-mayan-tzolkin-date)))
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-mayan-tzolkin-on-or-before
     tzolkin-date
     (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
  (or noecho (calendar-print-mayan-date)))

(defun calendar-mayan-tzolkin-to-string (tzolkin)
229
  "Convert Mayan TZOLKIN date (a pair) into its traditional written form."
Jim Blandy's avatar
Jim Blandy committed
230 231 232 233 234
  (format "%d %s"
          (car tzolkin)
          (aref calendar-mayan-tzolkin-names-array (1- (cdr tzolkin)))))

(defun calendar-mayan-tzolkin-haab-on-or-before (tzolkin-date haab-date date)
235 236
  "Absolute date that is Mayan TZOLKIN-DATE and HAAB-DATE.
Latest such date on or before DATE.
237
Returns nil if such a tzolkin-haab combination is impossible."
Jim Blandy's avatar
Jim Blandy committed
238 239 240 241 242 243 244 245 246 247 248
  (let* ((haab-difference
          (calendar-mayan-haab-difference
           (calendar-mayan-haab-from-absolute 0)
           haab-date))
         (tzolkin-difference
          (calendar-mayan-tzolkin-difference
           (calendar-mayan-tzolkin-from-absolute 0)
           tzolkin-date))
         (difference (- tzolkin-difference haab-difference)))
    (if (= (% difference 5) 0)
        (- date
249
           (mod (- date
Glenn Morris's avatar
Glenn Morris committed
250 251
                   (+ haab-difference (* 365 difference)))
                18980))
Jim Blandy's avatar
Jim Blandy committed
252 253 254
      nil)))

(defun calendar-read-mayan-haab-date ()
255
  "Prompt for a Mayan haab date."
Jim Blandy's avatar
Jim Blandy committed
256 257 258
  (let* ((completion-ignore-case t)
         (haab-day (calendar-read
                    "Haab kin (0-19): "
259
                    (lambda (x) (and (>= x 0) (< x 20)))))
260
         (haab-month-list (append calendar-mayan-haab-month-name-array
Jim Blandy's avatar
Jim Blandy committed
261 262
                                  (and (< haab-day 5) '("Uayeb"))))
         (haab-month (cdr
263
                      (assoc-string
264 265 266
                       (completing-read "Haab uinal: "
                                        (mapcar 'list haab-month-list)
                                        nil t)
267
                       (calendar-make-alist haab-month-list 1) t))))
Jim Blandy's avatar
Jim Blandy committed
268 269 270
    (cons haab-day haab-month)))

(defun calendar-read-mayan-tzolkin-date ()
271
  "Prompt for a Mayan tzolkin date."
Jim Blandy's avatar
Jim Blandy committed
272 273 274
  (let* ((completion-ignore-case t)
         (tzolkin-count (calendar-read
                         "Tzolkin kin (1-13): "
275
                         (lambda (x) (and (> x 0) (< x 14)))))
Jim Blandy's avatar
Jim Blandy committed
276 277
         (tzolkin-name-list (append calendar-mayan-tzolkin-names-array nil))
         (tzolkin-name (cdr
278
                        (assoc-string
Glenn Morris's avatar
Glenn Morris committed
279 280 281
                         (completing-read "Tzolkin uinal: "
                                          (mapcar 'list tzolkin-name-list)
                                          nil t)
282
                         (calendar-make-alist tzolkin-name-list 1) t))))
Jim Blandy's avatar
Jim Blandy committed
283 284
    (cons tzolkin-count tzolkin-name)))

285
;;;###cal-autoload
286 287 288 289
(defun calendar-next-calendar-round-date (tzolkin-date haab-date
                                                       &optional noecho)
  "Move cursor to next instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
Echo Mayan date unless NOECHO is non-nil."
Jim Blandy's avatar
Jim Blandy committed
290 291 292 293 294 295 296 297 298 299 300 301 302
  (interactive (list (calendar-read-mayan-tzolkin-date)
                     (calendar-read-mayan-haab-date)))
  (let ((date (calendar-mayan-tzolkin-haab-on-or-before
               tzolkin-date haab-date
               (+ 18980 (calendar-absolute-from-gregorian
                         (calendar-cursor-to-date))))))
    (if (not date)
        (error "%s, %s does not exist in the Mayan calendar round"
               (calendar-mayan-tzolkin-to-string tzolkin-date)
               (calendar-mayan-haab-to-string haab-date))
      (calendar-goto-date (calendar-gregorian-from-absolute date))
      (or noecho (calendar-print-mayan-date)))))

303
;;;###cal-autoload
Jim Blandy's avatar
Jim Blandy committed
304 305
(defun calendar-previous-calendar-round-date
  (tzolkin-date haab-date &optional noecho)
306
  "Move to previous instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
307
Echo Mayan date if NOECHO is t."
Jim Blandy's avatar
Jim Blandy committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321
  (interactive (list (calendar-read-mayan-tzolkin-date)
                     (calendar-read-mayan-haab-date)))
  (let ((date (calendar-mayan-tzolkin-haab-on-or-before
               tzolkin-date haab-date
               (1- (calendar-absolute-from-gregorian
                    (calendar-cursor-to-date))))))
    (if (not date)
        (error "%s, %s does not exist in the Mayan calendar round"
               (calendar-mayan-tzolkin-to-string tzolkin-date)
               (calendar-mayan-haab-to-string haab-date))
      (calendar-goto-date (calendar-gregorian-from-absolute date))
      (or noecho (calendar-print-mayan-date)))))

(defun calendar-absolute-from-mayan-long-count (c)
322 323
  "Compute the absolute date corresponding to the Mayan Long Count C.
Long count is a list (baktun katun tun uinal kin)"
Glenn Morris's avatar
Glenn Morris committed
324 325 326 327 328 329
  (+ (* (nth 0 c) 144000)               ; baktun
     (* (nth 1 c) 7200)                 ; katun
     (* (nth 2 c) 360)                  ; tun
     (* (nth 3 c) 20)                   ; uinal
     (nth 4 c)                          ; kin (days)
     (-                                 ; days before absolute date 0
Jim Blandy's avatar
Jim Blandy committed
330 331
      calendar-mayan-days-before-absolute-zero)))

332
;;;###cal-autoload
333 334 335
(defun calendar-mayan-date-string (&optional date)
  "String of Mayan date of Gregorian DATE.
Defaults to today's date if DATE is not given."
336
  (let* ((d (calendar-absolute-from-gregorian
337 338 339 340
             (or date (calendar-current-date))))
         (tzolkin (calendar-mayan-tzolkin-from-absolute d))
         (haab (calendar-mayan-haab-from-absolute d))
         (long-count (calendar-mayan-long-count-from-absolute d)))
Glenn Morris's avatar
Glenn Morris committed
341 342 343 344
    (format "Long count = %s; tzolkin = %s; haab = %s"
            (calendar-mayan-long-count-to-string long-count)
            (calendar-mayan-tzolkin-to-string tzolkin)
            (calendar-mayan-haab-to-string haab))))
345

346
;;;###cal-autoload
Jim Blandy's avatar
Jim Blandy committed
347
(defun calendar-print-mayan-date ()
348
  "Show the Mayan long count, tzolkin, and haab equivalents of date."
Jim Blandy's avatar
Jim Blandy committed
349
  (interactive)
350
  (message "Mayan date: %s"
351
           (calendar-mayan-date-string (calendar-cursor-to-date t))))
Jim Blandy's avatar
Jim Blandy committed
352

353
;;;###cal-autoload
Jim Blandy's avatar
Jim Blandy committed
354 355 356 357 358 359
(defun calendar-goto-mayan-long-count-date (date &optional noecho)
  "Move cursor to Mayan long count DATE.  Echo Mayan date unless NOECHO is t."
  (interactive
   (let (lc)
     (while (not lc)
       (let ((datum
360
              (calendar-string-to-mayan-long-count
Jim Blandy's avatar
Jim Blandy committed
361 362 363
               (read-string "Mayan long count (baktun.katun.tun.uinal.kin): "
                            (calendar-mayan-long-count-to-string
                             (calendar-mayan-long-count-from-absolute
Glenn Morris's avatar
Glenn Morris committed
364 365
                              (calendar-absolute-from-gregorian
                               (calendar-current-date))))))))
Jim Blandy's avatar
Jim Blandy committed
366 367 368 369 370 371 372
         (if (calendar-mayan-long-count-common-era datum)
             (setq lc datum))))
     (list lc)))
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-absolute-from-mayan-long-count date)))
  (or noecho (calendar-print-mayan-date)))
373

Jim Blandy's avatar
Jim Blandy committed
374
(defun calendar-mayan-long-count-common-era (lc)
375
  "Return non-nil if long count LC represents a date in the Common Era."
Jim Blandy's avatar
Jim Blandy committed
376 377 378 379 380 381
  (let ((base (calendar-mayan-long-count-from-absolute 1)))
    (while (and (not (null base)) (= (car lc) (car base)))
      (setq lc (cdr lc)
            base (cdr base)))
    (or (null lc) (> (car lc) (car base)))))

Glenn Morris's avatar
Glenn Morris committed
382 383 384
(defvar date)

;; To be called from list-sexp-diary-entries, where DATE is bound.
385
;;;###diary-autoload
Jim Blandy's avatar
Jim Blandy committed
386 387
(defun diary-mayan-date ()
  "Show the Mayan long count, haab, and tzolkin dates as a diary entry."
388
  (format "Mayan date: %s" (calendar-mayan-date-string date)))
Jim Blandy's avatar
Jim Blandy committed
389 390 391

(provide 'cal-mayan)

392
;; arch-tag: 54f35144-cd0f-4873-935a-a60129de07df
Jim Blandy's avatar
Jim Blandy committed
393
;;; cal-mayan.el ends here