cal-mayan.el 15.7 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, 2009, 2010  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
;; 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 17
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
18

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
25
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Jim Blandy's avatar
Jim Blandy committed
26 27 28

;;; Commentary:

29
;; See calendar.el.
Jim Blandy's avatar
Jim Blandy committed
30 31 32 33 34

;;; Code:

(require 'calendar)

35
(defconst calendar-mayan-days-before-absolute-zero 1137142
36
  "Number of days of the Mayan calendar epoch before absolute day 0.
37 38 39
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
40 41 42 43 44 45

(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"
46 47
   "Zac" "Ceh" "Mac" "Kankin" "Muan" "Pax" "Kayab" "Cumku"]
  "Names of the Mayan haab months.")
Jim Blandy's avatar
Jim Blandy committed
48 49 50 51 52 53

(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"
54 55
   "Chuen" "Eb" "Ben" "Ix" "Men" "Cib" "Caban" "Etznab" "Cauac" "Ahau"]
  "Names of the Mayan tzolkin months.")
Jim Blandy's avatar
Jim Blandy committed
56 57 58

(defun calendar-mayan-long-count-from-absolute (date)
  "Compute the Mayan long count corresponding to the absolute DATE."
59 60 61 62 63 64 65 66 67 68
  (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
         (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)))
Jim Blandy's avatar
Jim Blandy committed
69 70 71 72 73

(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)))

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."
76
  (let ((end 0)
77
        rlc)
78
    (condition-case nil
Jim Blandy's avatar
Jim Blandy committed
79
        (progn
80 81 82 83
          ;; cf split-string.
          (while (string-match "[0-9]+" str end)
            (setq rlc (cons (string-to-number (match-string 0 str)) rlc)
                  end (match-end 0)))
84
          (unless (= (length rlc) 5) (signal 'invalid-read-syntax nil)))
Jim Blandy's avatar
Jim Blandy committed
85
      (invalid-read-syntax nil))
86
    (nreverse rlc)))
Jim Blandy's avatar
Jim Blandy committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100

(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)
101
  "Number of days from Mayan haab DATE1 to next occurrence of haab date DATE2."
102
  (mod (+ (* 20 (- (cdr date2) (cdr date1)))
Glenn Morris's avatar
Glenn Morris committed
103
          (- (car date2) (car date1)))
104
       365))
Jim Blandy's avatar
Jim Blandy committed
105 106 107

(defun calendar-mayan-haab-on-or-before (haab-date date)
  "Absolute date of latest HAAB-DATE on or before absolute DATE."
108 109
  (- date
     (% (- date
Glenn Morris's avatar
Glenn Morris committed
110 111 112
           (calendar-mayan-haab-difference
            (calendar-mayan-haab-from-absolute 0) haab-date))
        365)))
Jim Blandy's avatar
Jim Blandy committed
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127
;;;###cal-autoload
(defun calendar-mayan-date-string (&optional date)
  "String of Mayan date of Gregorian DATE; default today."
  (let* ((d (calendar-absolute-from-gregorian
             (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)))
    (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))))

;;;###cal-autoload
128
(defun calendar-mayan-print-date ()
129 130 131 132 133
  "Show the Mayan long count, tzolkin, and haab equivalents of date."
  (interactive)
  (message "Mayan date: %s"
           (calendar-mayan-date-string (calendar-cursor-to-date t))))

134 135 136 137
(define-obsolete-function-alias 'calendar-print-mayan-date
  'calendar-mayan-print-date "23.1")

(defun calendar-mayan-read-haab-date ()
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  "Prompt for a Mayan haab date."
  (let* ((completion-ignore-case t)
         (haab-day (calendar-read
                    "Haab kin (0-19): "
                    (lambda (x) (and (>= x 0) (< x 20)))))
         (haab-month-list (append calendar-mayan-haab-month-name-array
                                  (and (< haab-day 5) '("Uayeb"))))
         (haab-month (cdr
                      (assoc-string
                       (completing-read "Haab uinal: "
                                        (mapcar 'list haab-month-list)
                                        nil t)
                       (calendar-make-alist haab-month-list 1) t))))
    (cons haab-day haab-month)))

153
(defun calendar-mayan-read-tzolkin-date ()
154 155 156 157 158 159 160 161 162 163 164 165 166 167
  "Prompt for a Mayan tzolkin date."
  (let* ((completion-ignore-case t)
         (tzolkin-count (calendar-read
                         "Tzolkin kin (1-13): "
                         (lambda (x) (and (> x 0) (< x 14)))))
         (tzolkin-name-list (append calendar-mayan-tzolkin-names-array nil))
         (tzolkin-name (cdr
                        (assoc-string
                         (completing-read "Tzolkin uinal: "
                                          (mapcar 'list tzolkin-name-list)
                                          nil t)
                         (calendar-make-alist tzolkin-name-list 1) t))))
    (cons tzolkin-count tzolkin-name)))

168
;;;###cal-autoload
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)))
Jim Blandy's avatar
Jim Blandy committed
173 174 175 176 177 178
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-mayan-haab-on-or-before
     haab-date
     (+ 365
        (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
179 180 181 182
  (or noecho (calendar-mayan-print-date)))

(define-obsolete-function-alias 'calendar-next-haab-date
  'calendar-mayan-next-haab-date "23.1")
Jim Blandy's avatar
Jim Blandy committed
183

184
;;;###cal-autoload
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)))
Jim Blandy's avatar
Jim Blandy committed
189 190 191 192 193
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-mayan-haab-on-or-before
     haab-date
     (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
194 195 196 197
  (or noecho (calendar-mayan-print-date)))

(define-obsolete-function-alias 'calendar-previous-haab-date
  'calendar-mayan-previous-haab-date "23.1")
Jim Blandy's avatar
Jim Blandy committed
198 199

(defun calendar-mayan-haab-to-string (haab)
200
  "Convert Mayan HAAB date (a pair) into its traditional written form."
201 202 203 204
  (let ((month (cdr haab)))
    (format "%d %s" (car haab)          ; day
            ;; 19th month consists of 5 special days
            (if (= month 19) "Uayeb"
Glenn Morris's avatar
Glenn Morris committed
205
              (aref calendar-mayan-haab-month-name-array (1- month))))))
Jim Blandy's avatar
Jim Blandy committed
206 207 208 209

(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))
210 211 212 213 214 215 216
         ;; Remainder on division by 13,20 with 13,20 instead of zero.
         (day (1+ (mod
                   (1- (+ long-count (car calendar-mayan-tzolkin-at-epoch)))
                   13)))
         (name (1+ (mod
                    (1- (+ long-count (cdr calendar-mayan-tzolkin-at-epoch)))
                    20))))
Jim Blandy's avatar
Jim Blandy committed
217 218 219
    (cons day name)))

(defun calendar-mayan-tzolkin-difference (date1 date2)
220
  "Number of days from Mayan tzolkin DATE1 to next occurrence of tzolkin DATE2."
Jim Blandy's avatar
Jim Blandy committed
221 222
  (let ((number-difference (- (car date2) (car date1)))
        (name-difference (- (cdr date2) (cdr date1))))
223
    (mod (+ number-difference
Glenn Morris's avatar
Glenn Morris committed
224 225 226
            (* 13 (mod (* 3 (- number-difference name-difference))
                       20)))
         260)))
Jim Blandy's avatar
Jim Blandy committed
227 228 229

(defun calendar-mayan-tzolkin-on-or-before (tzolkin-date date)
  "Absolute date of latest TZOLKIN-DATE on or before absolute DATE."
230 231
  (- date
     (% (- date (calendar-mayan-tzolkin-difference
Glenn Morris's avatar
Glenn Morris committed
232 233 234
                 (calendar-mayan-tzolkin-from-absolute 0)
                 tzolkin-date))
        260)))
Jim Blandy's avatar
Jim Blandy committed
235

236
;;;###cal-autoload
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)))
Jim Blandy's avatar
Jim Blandy committed
241 242 243 244 245 246
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-mayan-tzolkin-on-or-before
     tzolkin-date
     (+ 260
        (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
247 248 249 250
  (or noecho (calendar-mayan-print-date)))

(define-obsolete-function-alias 'calendar-next-tzolkin-date
  'calendar-mayan-next-tzolkin-date "23.1")
Jim Blandy's avatar
Jim Blandy committed
251

252
;;;###cal-autoload
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)))
Jim Blandy's avatar
Jim Blandy committed
257 258 259 260 261
  (calendar-goto-date
   (calendar-gregorian-from-absolute
    (calendar-mayan-tzolkin-on-or-before
     tzolkin-date
     (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
262 263 264 265
  (or noecho (calendar-mayan-print-date)))

(define-obsolete-function-alias 'calendar-previous-tzolkin-date
  'calendar-mayan-previous-tzolkin-date "23.1")
Jim Blandy's avatar
Jim Blandy committed
266 267

(defun calendar-mayan-tzolkin-to-string (tzolkin)
268
  "Convert Mayan TZOLKIN date (a pair) into its traditional written form."
Jim Blandy's avatar
Jim Blandy committed
269 270 271 272 273
  (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)
274 275
  "Absolute date that is Mayan TZOLKIN-DATE and HAAB-DATE.
Latest such date on or before DATE.
276
Returns nil if such a tzolkin-haab combination is impossible."
Jim Blandy's avatar
Jim Blandy committed
277 278 279 280 281 282 283 284 285
  (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)))
286
    (if (zerop (% difference 5))
Jim Blandy's avatar
Jim Blandy committed
287
        (- date
288
           (mod (- date
Glenn Morris's avatar
Glenn Morris committed
289 290
                   (+ haab-difference (* 365 difference)))
                18980))
Jim Blandy's avatar
Jim Blandy committed
291 292
      nil)))

293
;;;###cal-autoload
294
(defun calendar-mayan-next-round-date (tzolkin-date haab-date
295 296 297
                                                       &optional noecho)
  "Move cursor to next instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
Echo Mayan date unless NOECHO is non-nil."
298 299
  (interactive (list (calendar-mayan-read-tzolkin-date)
                     (calendar-mayan-read-haab-date)))
Jim Blandy's avatar
Jim Blandy committed
300 301 302 303 304 305 306 307 308
  (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))
309 310 311 312
      (or noecho (calendar-mayan-print-date)))))

(define-obsolete-function-alias 'calendar-next-calendar-round-date
  'calendar-mayan-next-round-date "23.1")
Jim Blandy's avatar
Jim Blandy committed
313

314
;;;###cal-autoload
315
(defun calendar-mayan-previous-round-date
Jim Blandy's avatar
Jim Blandy committed
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 320
  (interactive (list (calendar-mayan-read-tzolkin-date)
                     (calendar-mayan-read-haab-date)))
Jim Blandy's avatar
Jim Blandy committed
321 322 323 324 325 326 327 328 329
  (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))
330
      (or noecho (calendar-mayan-print-date)))))
Jim Blandy's avatar
Jim Blandy committed
331

332 333 334 335
(define-obsolete-function-alias 'calendar-previous-calendar-round-date
  'calendar-mayan-previous-round-date "23.1")

(defun calendar-mayan-long-count-to-absolute (c)
336 337
  "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
338 339 340 341 342
  (+ (* (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)
343 344
     ;; Days before absolute date 0.
     (- calendar-mayan-days-before-absolute-zero)))
Jim Blandy's avatar
Jim Blandy committed
345

346 347 348
(define-obsolete-function-alias 'calendar-absolute-from-mayan-long-count
  'calendar-mayan-long-count-to-absolute "23.1")

349 350 351 352 353 354 355
(defun calendar-mayan-long-count-common-era (lc)
  "Return non-nil if long count LC represents a date in the Common Era."
  (let ((base (calendar-mayan-long-count-from-absolute 1)))
    (while (and base (= (car lc) (car base)))
      (setq lc (cdr lc)
            base (cdr base)))
    (or (null lc) (> (car lc) (car base)))))
Jim Blandy's avatar
Jim Blandy committed
356

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

(define-obsolete-function-alias 'calendar-goto-mayan-long-count-date
  'calendar-mayan-goto-long-count-date "23.1")
381

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

Glenn Morris's avatar
Glenn Morris committed
384
;; To be called from diary-list-sexp-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