cal-mayan.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. ;;; cal-mayan.el --- calendar functions for the Mayan calendars
  2. ;; Copyright (C) 1992-1993, 1995, 1997, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Stewart M. Clamen <clamen@cs.cmu.edu>
  5. ;; Edward M. Reingold <reingold@cs.uiuc.edu>
  6. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  7. ;; Keywords: calendar
  8. ;; Human-Keywords: Mayan calendar, Maya, calendar, diary
  9. ;; Package: calendar
  10. ;; This file is part of GNU Emacs.
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;; See calendar.el.
  23. ;;; Code:
  24. (require 'calendar)
  25. (defconst calendar-mayan-days-before-absolute-zero 1137142
  26. "Number of days of the Mayan calendar epoch before absolute day 0.
  27. This is the Goodman-Martinez-Thompson correlation used by almost all experts,
  28. but some use 1137140. Using 1232041 gives you Spinden's correlation; using
  29. 1142840 gives you Hochleitner's correlation.")
  30. (defconst calendar-mayan-haab-at-epoch '(8 . 18)
  31. "Mayan haab date at the epoch.")
  32. (defconst calendar-mayan-haab-month-name-array
  33. ["Pop" "Uo" "Zip" "Zotz" "Tzec" "Xul" "Yaxkin" "Mol" "Chen" "Yax"
  34. "Zac" "Ceh" "Mac" "Kankin" "Muan" "Pax" "Kayab" "Cumku"]
  35. "Names of the Mayan haab months.")
  36. (defconst calendar-mayan-tzolkin-at-epoch '(4 . 20)
  37. "Mayan tzolkin date at the epoch.")
  38. (defconst calendar-mayan-tzolkin-names-array
  39. ["Imix" "Ik" "Akbal" "Kan" "Chicchan" "Cimi" "Manik" "Lamat" "Muluc" "Oc"
  40. "Chuen" "Eb" "Ben" "Ix" "Men" "Cib" "Caban" "Etznab" "Cauac" "Ahau"]
  41. "Names of the Mayan tzolkin months.")
  42. (defun calendar-mayan-long-count-from-absolute (date)
  43. "Compute the Mayan long count corresponding to the absolute DATE."
  44. (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
  45. (baktun (/ long-count 144000))
  46. (remainder (% long-count 144000))
  47. (katun (/ remainder 7200))
  48. (remainder (% remainder 7200))
  49. (tun (/ remainder 360))
  50. (remainder (% remainder 360))
  51. (uinal (/ remainder 20))
  52. (kin (% remainder 20)))
  53. (list baktun katun tun uinal kin)))
  54. (defun calendar-mayan-long-count-to-string (mayan-long-count)
  55. "Convert MAYAN-LONG-COUNT into traditional written form."
  56. (apply 'format (cons "%s.%s.%s.%s.%s" mayan-long-count)))
  57. (defun calendar-mayan-string-from-long-count (str)
  58. "Given STR, a string of format \"%d.%d.%d.%d.%d\", return list of numbers."
  59. (let ((end 0)
  60. rlc)
  61. (condition-case nil
  62. (progn
  63. ;; cf split-string.
  64. (while (string-match "[0-9]+" str end)
  65. (setq rlc (cons (string-to-number (match-string 0 str)) rlc)
  66. end (match-end 0)))
  67. (unless (= (length rlc) 5) (signal 'invalid-read-syntax nil)))
  68. (invalid-read-syntax nil))
  69. (nreverse rlc)))
  70. (defun calendar-mayan-haab-from-absolute (date)
  71. "Convert absolute DATE into a Mayan haab date (a pair)."
  72. (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
  73. (day-of-haab
  74. (% (+ long-count
  75. (car calendar-mayan-haab-at-epoch)
  76. (* 20 (1- (cdr calendar-mayan-haab-at-epoch))))
  77. 365))
  78. (day (% day-of-haab 20))
  79. (month (1+ (/ day-of-haab 20))))
  80. (cons day month)))
  81. (defun calendar-mayan-haab-difference (date1 date2)
  82. "Number of days from Mayan haab DATE1 to next occurrence of haab date DATE2."
  83. (mod (+ (* 20 (- (cdr date2) (cdr date1)))
  84. (- (car date2) (car date1)))
  85. 365))
  86. (defun calendar-mayan-haab-on-or-before (haab-date date)
  87. "Absolute date of latest HAAB-DATE on or before absolute DATE."
  88. (- date
  89. (% (- date
  90. (calendar-mayan-haab-difference
  91. (calendar-mayan-haab-from-absolute 0) haab-date))
  92. 365)))
  93. ;;;###cal-autoload
  94. (defun calendar-mayan-date-string (&optional date)
  95. "String of Mayan date of Gregorian DATE; default today."
  96. (let* ((d (calendar-absolute-from-gregorian
  97. (or date (calendar-current-date))))
  98. (tzolkin (calendar-mayan-tzolkin-from-absolute d))
  99. (haab (calendar-mayan-haab-from-absolute d))
  100. (long-count (calendar-mayan-long-count-from-absolute d)))
  101. (format "Long count = %s; tzolkin = %s; haab = %s"
  102. (calendar-mayan-long-count-to-string long-count)
  103. (calendar-mayan-tzolkin-to-string tzolkin)
  104. (calendar-mayan-haab-to-string haab))))
  105. ;;;###cal-autoload
  106. (defun calendar-mayan-print-date ()
  107. "Show the Mayan long count, tzolkin, and haab equivalents of date."
  108. (interactive)
  109. (message "Mayan date: %s"
  110. (calendar-mayan-date-string (calendar-cursor-to-date t))))
  111. (define-obsolete-function-alias 'calendar-print-mayan-date
  112. 'calendar-mayan-print-date "23.1")
  113. (defun calendar-mayan-read-haab-date ()
  114. "Prompt for a Mayan haab date."
  115. (let* ((completion-ignore-case t)
  116. (haab-day (calendar-read
  117. "Haab kin (0-19): "
  118. (lambda (x) (and (>= x 0) (< x 20)))))
  119. (haab-month-list (append calendar-mayan-haab-month-name-array
  120. (and (< haab-day 5) '("Uayeb"))))
  121. (haab-month (cdr
  122. (assoc-string
  123. (completing-read "Haab uinal: "
  124. (mapcar 'list haab-month-list)
  125. nil t)
  126. (calendar-make-alist haab-month-list 1) t))))
  127. (cons haab-day haab-month)))
  128. (defun calendar-mayan-read-tzolkin-date ()
  129. "Prompt for a Mayan tzolkin date."
  130. (let* ((completion-ignore-case t)
  131. (tzolkin-count (calendar-read
  132. "Tzolkin kin (1-13): "
  133. (lambda (x) (and (> x 0) (< x 14)))))
  134. (tzolkin-name-list (append calendar-mayan-tzolkin-names-array nil))
  135. (tzolkin-name (cdr
  136. (assoc-string
  137. (completing-read "Tzolkin uinal: "
  138. (mapcar 'list tzolkin-name-list)
  139. nil t)
  140. (calendar-make-alist tzolkin-name-list 1) t))))
  141. (cons tzolkin-count tzolkin-name)))
  142. ;;;###cal-autoload
  143. (defun calendar-mayan-next-haab-date (haab-date &optional noecho)
  144. "Move cursor to next instance of Mayan HAAB-DATE.
  145. Echo Mayan date unless NOECHO is non-nil."
  146. (interactive (list (calendar-mayan-read-haab-date)))
  147. (calendar-goto-date
  148. (calendar-gregorian-from-absolute
  149. (calendar-mayan-haab-on-or-before
  150. haab-date
  151. (+ 365
  152. (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
  153. (or noecho (calendar-mayan-print-date)))
  154. (define-obsolete-function-alias 'calendar-next-haab-date
  155. 'calendar-mayan-next-haab-date "23.1")
  156. ;;;###cal-autoload
  157. (defun calendar-mayan-previous-haab-date (haab-date &optional noecho)
  158. "Move cursor to previous instance of Mayan HAAB-DATE.
  159. Echo Mayan date unless NOECHO is non-nil."
  160. (interactive (list (calendar-mayan-read-haab-date)))
  161. (calendar-goto-date
  162. (calendar-gregorian-from-absolute
  163. (calendar-mayan-haab-on-or-before
  164. haab-date
  165. (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
  166. (or noecho (calendar-mayan-print-date)))
  167. (define-obsolete-function-alias 'calendar-previous-haab-date
  168. 'calendar-mayan-previous-haab-date "23.1")
  169. (defun calendar-mayan-haab-to-string (haab)
  170. "Convert Mayan HAAB date (a pair) into its traditional written form."
  171. (let ((month (cdr haab)))
  172. (format "%d %s" (car haab) ; day
  173. ;; 19th month consists of 5 special days
  174. (if (= month 19) "Uayeb"
  175. (aref calendar-mayan-haab-month-name-array (1- month))))))
  176. (defun calendar-mayan-tzolkin-from-absolute (date)
  177. "Convert absolute DATE into a Mayan tzolkin date (a pair)."
  178. (let* ((long-count (+ date calendar-mayan-days-before-absolute-zero))
  179. ;; Remainder on division by 13,20 with 13,20 instead of zero.
  180. (day (1+ (mod
  181. (1- (+ long-count (car calendar-mayan-tzolkin-at-epoch)))
  182. 13)))
  183. (name (1+ (mod
  184. (1- (+ long-count (cdr calendar-mayan-tzolkin-at-epoch)))
  185. 20))))
  186. (cons day name)))
  187. (defun calendar-mayan-tzolkin-difference (date1 date2)
  188. "Number of days from Mayan tzolkin DATE1 to next occurrence of tzolkin DATE2."
  189. (let ((number-difference (- (car date2) (car date1)))
  190. (name-difference (- (cdr date2) (cdr date1))))
  191. (mod (+ number-difference
  192. (* 13 (mod (* 3 (- number-difference name-difference))
  193. 20)))
  194. 260)))
  195. (defun calendar-mayan-tzolkin-on-or-before (tzolkin-date date)
  196. "Absolute date of latest TZOLKIN-DATE on or before absolute DATE."
  197. (- date
  198. (% (- date (calendar-mayan-tzolkin-difference
  199. (calendar-mayan-tzolkin-from-absolute 0)
  200. tzolkin-date))
  201. 260)))
  202. ;;;###cal-autoload
  203. (defun calendar-mayan-next-tzolkin-date (tzolkin-date &optional noecho)
  204. "Move cursor to next instance of Mayan TZOLKIN-DATE.
  205. Echo Mayan date unless NOECHO is non-nil."
  206. (interactive (list (calendar-mayan-read-tzolkin-date)))
  207. (calendar-goto-date
  208. (calendar-gregorian-from-absolute
  209. (calendar-mayan-tzolkin-on-or-before
  210. tzolkin-date
  211. (+ 260
  212. (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
  213. (or noecho (calendar-mayan-print-date)))
  214. (define-obsolete-function-alias 'calendar-next-tzolkin-date
  215. 'calendar-mayan-next-tzolkin-date "23.1")
  216. ;;;###cal-autoload
  217. (defun calendar-mayan-previous-tzolkin-date (tzolkin-date &optional noecho)
  218. "Move cursor to previous instance of Mayan TZOLKIN-DATE.
  219. Echo Mayan date unless NOECHO is non-nil."
  220. (interactive (list (calendar-mayan-read-tzolkin-date)))
  221. (calendar-goto-date
  222. (calendar-gregorian-from-absolute
  223. (calendar-mayan-tzolkin-on-or-before
  224. tzolkin-date
  225. (1- (calendar-absolute-from-gregorian (calendar-cursor-to-date))))))
  226. (or noecho (calendar-mayan-print-date)))
  227. (define-obsolete-function-alias 'calendar-previous-tzolkin-date
  228. 'calendar-mayan-previous-tzolkin-date "23.1")
  229. (defun calendar-mayan-tzolkin-to-string (tzolkin)
  230. "Convert Mayan TZOLKIN date (a pair) into its traditional written form."
  231. (format "%d %s"
  232. (car tzolkin)
  233. (aref calendar-mayan-tzolkin-names-array (1- (cdr tzolkin)))))
  234. (defun calendar-mayan-tzolkin-haab-on-or-before (tzolkin-date haab-date date)
  235. "Absolute date that is Mayan TZOLKIN-DATE and HAAB-DATE.
  236. Latest such date on or before DATE.
  237. Returns nil if such a tzolkin-haab combination is impossible."
  238. (let* ((haab-difference
  239. (calendar-mayan-haab-difference
  240. (calendar-mayan-haab-from-absolute 0)
  241. haab-date))
  242. (tzolkin-difference
  243. (calendar-mayan-tzolkin-difference
  244. (calendar-mayan-tzolkin-from-absolute 0)
  245. tzolkin-date))
  246. (difference (- tzolkin-difference haab-difference)))
  247. (if (zerop (% difference 5))
  248. (- date
  249. (mod (- date
  250. (+ haab-difference (* 365 difference)))
  251. 18980))
  252. nil)))
  253. ;;;###cal-autoload
  254. (defun calendar-mayan-next-round-date (tzolkin-date haab-date
  255. &optional noecho)
  256. "Move cursor to next instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
  257. Echo Mayan date unless NOECHO is non-nil."
  258. (interactive (list (calendar-mayan-read-tzolkin-date)
  259. (calendar-mayan-read-haab-date)))
  260. (let ((date (calendar-mayan-tzolkin-haab-on-or-before
  261. tzolkin-date haab-date
  262. (+ 18980 (calendar-absolute-from-gregorian
  263. (calendar-cursor-to-date))))))
  264. (if (not date)
  265. (error "%s, %s does not exist in the Mayan calendar round"
  266. (calendar-mayan-tzolkin-to-string tzolkin-date)
  267. (calendar-mayan-haab-to-string haab-date))
  268. (calendar-goto-date (calendar-gregorian-from-absolute date))
  269. (or noecho (calendar-mayan-print-date)))))
  270. (define-obsolete-function-alias 'calendar-next-calendar-round-date
  271. 'calendar-mayan-next-round-date "23.1")
  272. ;;;###cal-autoload
  273. (defun calendar-mayan-previous-round-date
  274. (tzolkin-date haab-date &optional noecho)
  275. "Move to previous instance of Mayan TZOLKIN-DATE HAAB-DATE combination.
  276. Echo Mayan date unless NOECHO is non-nil."
  277. (interactive (list (calendar-mayan-read-tzolkin-date)
  278. (calendar-mayan-read-haab-date)))
  279. (let ((date (calendar-mayan-tzolkin-haab-on-or-before
  280. tzolkin-date haab-date
  281. (1- (calendar-absolute-from-gregorian
  282. (calendar-cursor-to-date))))))
  283. (if (not date)
  284. (error "%s, %s does not exist in the Mayan calendar round"
  285. (calendar-mayan-tzolkin-to-string tzolkin-date)
  286. (calendar-mayan-haab-to-string haab-date))
  287. (calendar-goto-date (calendar-gregorian-from-absolute date))
  288. (or noecho (calendar-mayan-print-date)))))
  289. (define-obsolete-function-alias 'calendar-previous-calendar-round-date
  290. 'calendar-mayan-previous-round-date "23.1")
  291. (defun calendar-mayan-long-count-to-absolute (c)
  292. "Compute the absolute date corresponding to the Mayan Long Count C.
  293. Long count is a list (baktun katun tun uinal kin)"
  294. (+ (* (nth 0 c) 144000) ; baktun
  295. (* (nth 1 c) 7200) ; katun
  296. (* (nth 2 c) 360) ; tun
  297. (* (nth 3 c) 20) ; uinal
  298. (nth 4 c) ; kin (days)
  299. ;; Days before absolute date 0.
  300. (- calendar-mayan-days-before-absolute-zero)))
  301. (define-obsolete-function-alias 'calendar-absolute-from-mayan-long-count
  302. 'calendar-mayan-long-count-to-absolute "23.1")
  303. (defun calendar-mayan-long-count-common-era (lc)
  304. "Return non-nil if long count LC represents a date in the Common Era."
  305. (let ((base (calendar-mayan-long-count-from-absolute 1)))
  306. (while (and base (= (car lc) (car base)))
  307. (setq lc (cdr lc)
  308. base (cdr base)))
  309. (or (null lc) (> (car lc) (car base)))))
  310. ;;;###cal-autoload
  311. (defun calendar-mayan-goto-long-count-date (date &optional noecho)
  312. "Move cursor to Mayan long count DATE.
  313. Echo Mayan date unless NOECHO is non-nil."
  314. (interactive
  315. (let (datum)
  316. (while (not (setq datum
  317. (calendar-mayan-string-from-long-count
  318. (read-string
  319. "Mayan long count (baktun.katun.tun.uinal.kin): "
  320. (calendar-mayan-long-count-to-string
  321. (calendar-mayan-long-count-from-absolute
  322. (calendar-absolute-from-gregorian
  323. (calendar-current-date))))))
  324. datum (if (calendar-mayan-long-count-common-era datum)
  325. (list datum)))))
  326. datum))
  327. (calendar-goto-date
  328. (calendar-gregorian-from-absolute
  329. (calendar-mayan-long-count-to-absolute date)))
  330. (or noecho (calendar-mayan-print-date)))
  331. (define-obsolete-function-alias 'calendar-goto-mayan-long-count-date
  332. 'calendar-mayan-goto-long-count-date "23.1")
  333. (defvar date)
  334. ;; To be called from diary-list-sexp-entries, where DATE is bound.
  335. ;;;###diary-autoload
  336. (defun diary-mayan-date ()
  337. "Show the Mayan long count, haab, and tzolkin dates as a diary entry."
  338. (format "Mayan date: %s" (calendar-mayan-date-string date)))
  339. (provide 'cal-mayan)
  340. ;;; cal-mayan.el ends here