cal-persia.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ;;; cal-persia.el --- calendar functions for the Persian calendar
  2. ;; Copyright (C) 1996-1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  4. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  5. ;; Keywords: calendar
  6. ;; Human-Keywords: Persian calendar, calendar, diary
  7. ;; Package: calendar
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; See calendar.el.
  21. ;;; Code:
  22. (require 'calendar)
  23. (defconst calendar-persian-month-name-array
  24. ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
  25. "Azar" "Dey" "Bahman" "Esfand"]
  26. "Names of the months in the Persian calendar.")
  27. (eval-and-compile
  28. (autoload 'calendar-julian-to-absolute "cal-julian"))
  29. (defconst calendar-persian-epoch
  30. (eval-when-compile (calendar-julian-to-absolute '(3 19 622)))
  31. "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).")
  32. (defun calendar-persian-leap-year-p (year)
  33. "True if YEAR is a leap year on the Persian calendar."
  34. (< (mod (* (mod (mod (if (<= 0 year)
  35. (+ year 2346) ; no year zero
  36. (+ year 2347))
  37. 2820)
  38. 768)
  39. 683)
  40. 2820)
  41. 683))
  42. (defun calendar-persian-last-day-of-month (month year)
  43. "Return last day of MONTH, YEAR on the Persian calendar."
  44. (cond
  45. ((< month 7) 31)
  46. ((or (< month 12) (calendar-persian-leap-year-p year)) 30)
  47. (t 29)))
  48. (defun calendar-persian-to-absolute (date)
  49. "Compute absolute date from Persian date DATE.
  50. The absolute date is the number of days elapsed since the (imaginary)
  51. Gregorian date Sunday, December 31, 1 BC."
  52. (let ((month (calendar-extract-month date))
  53. (day (calendar-extract-day date))
  54. (year (calendar-extract-year date)))
  55. (if (< year 0)
  56. (+ (calendar-persian-to-absolute
  57. (list month day (1+ (mod year 2820))))
  58. (* 1029983 (floor year 2820)))
  59. (+ (1- calendar-persian-epoch) ; days before epoch
  60. (* 365 (1- year)) ; days in prior years
  61. (* 683 ; leap days in prior 2820-year cycles
  62. (floor (+ year 2345) 2820))
  63. (* 186 ; leap days in prior 768 year cycles
  64. (floor (mod (+ year 2345) 2820) 768))
  65. (floor ; leap years in current 768 or 516 year cycle
  66. (* 683 (mod (mod (+ year 2345) 2820) 768))
  67. 2820)
  68. -568 ; leap years in Persian years -2345...-1
  69. (calendar-sum ; days in prior months this year
  70. m 1 (< m month)
  71. (calendar-persian-last-day-of-month m year))
  72. day)))) ; days so far this month
  73. (define-obsolete-function-alias 'calendar-absolute-from-persian
  74. 'calendar-persian-to-absolute "23.1")
  75. (defun calendar-persian-year-from-absolute (date)
  76. "Persian year corresponding to the absolute DATE."
  77. (let* ((d0 ; prior days since start of 2820 cycles
  78. (- date (calendar-persian-to-absolute (list 1 1 -2345))))
  79. (n2820 ; completed 2820-year cycles
  80. (floor d0 1029983))
  81. (d1 ; prior days not in n2820
  82. (mod d0 1029983))
  83. (n768 ; 768-year cycles not in n2820
  84. (floor d1 280506))
  85. (d2 ; prior days not in n2820 or n768
  86. (mod d1 280506))
  87. (n1 ; years not in n2820 or n768
  88. ;; Want:
  89. ;; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
  90. ;; but that causes overflow, so use the following.
  91. ;; Use 366 as the divisor because (2820*366 mod 1029983) is small.
  92. (let ((a (floor d2 366))
  93. (b (mod d2 366)))
  94. (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983))))
  95. (year (+ (* 2820 n2820) ; complete 2820 year cycles
  96. (* 768 n768) ; complete 768 year cycles
  97. ;; Remaining years.
  98. (if (= d1 1029617) ; last day of 2820 year cycle
  99. (1- n1)
  100. n1)
  101. -2345))) ; years before year 1
  102. (if (< year 1)
  103. (1- year) ; no year zero
  104. year)))
  105. (defun calendar-persian-from-absolute (date)
  106. "Compute the Persian equivalent for absolute date DATE.
  107. The result is a list of the form (MONTH DAY YEAR).
  108. The absolute date is the number of days elapsed since the imaginary
  109. Gregorian date Sunday, December 31, 1 BC."
  110. (let* ((year (calendar-persian-year-from-absolute date))
  111. (month ; search forward from Farvardin
  112. (1+ (calendar-sum m 1
  113. (> date
  114. (calendar-persian-to-absolute
  115. (list
  116. m
  117. (calendar-persian-last-day-of-month m year)
  118. year)))
  119. 1)))
  120. (day ; calculate the day by subtraction
  121. (- date (1- (calendar-persian-to-absolute
  122. (list month 1 year))))))
  123. (list month day year)))
  124. ;;;###cal-autoload
  125. (defun calendar-persian-date-string (&optional date)
  126. "String of Persian date of Gregorian DATE, default today."
  127. (let* ((persian-date (calendar-persian-from-absolute
  128. (calendar-absolute-from-gregorian
  129. (or date (calendar-current-date)))))
  130. (y (calendar-extract-year persian-date))
  131. (m (calendar-extract-month persian-date))
  132. (monthname (aref calendar-persian-month-name-array (1- m)))
  133. (day (number-to-string (calendar-extract-day persian-date)))
  134. (year (number-to-string y))
  135. (month (number-to-string m))
  136. dayname)
  137. (mapconcat 'eval calendar-date-display-form "")))
  138. ;;;###cal-autoload
  139. (defun calendar-persian-print-date ()
  140. "Show the Persian calendar equivalent of the selected date."
  141. (interactive)
  142. (message "Persian date: %s"
  143. (calendar-persian-date-string (calendar-cursor-to-date t))))
  144. (define-obsolete-function-alias 'calendar-print-persian-date
  145. 'calendar-persian-print-date "23.1")
  146. (defun calendar-persian-read-date ()
  147. "Interactively read the arguments for a Persian date command.
  148. Reads a year, month, and day."
  149. (let* ((year (calendar-read
  150. "Persian calendar year (not 0): "
  151. (lambda (x) (not (zerop x)))
  152. (number-to-string
  153. (calendar-extract-year
  154. (calendar-persian-from-absolute
  155. (calendar-absolute-from-gregorian
  156. (calendar-current-date)))))))
  157. (completion-ignore-case t)
  158. (month (cdr (assoc
  159. (completing-read
  160. "Persian calendar month name: "
  161. (mapcar 'list
  162. (append calendar-persian-month-name-array nil))
  163. nil t)
  164. (calendar-make-alist calendar-persian-month-name-array
  165. 1))))
  166. (last (calendar-persian-last-day-of-month month year))
  167. (day (calendar-read
  168. (format "Persian calendar day (1-%d): " last)
  169. (lambda (x) (and (< 0 x) (<= x last))))))
  170. (list (list month day year))))
  171. (define-obsolete-function-alias 'persian-prompt-for-date
  172. 'calendar-persian-read-date "23.1")
  173. ;;;###cal-autoload
  174. (defun calendar-persian-goto-date (date &optional noecho)
  175. "Move cursor to Persian date DATE.
  176. Echo Persian date unless NOECHO is non-nil."
  177. (interactive (calendar-persian-read-date))
  178. (calendar-goto-date (calendar-gregorian-from-absolute
  179. (calendar-persian-to-absolute date)))
  180. (or noecho (calendar-persian-print-date)))
  181. (define-obsolete-function-alias 'calendar-goto-persian-date
  182. 'calendar-persian-goto-date "23.1")
  183. (defvar date)
  184. ;; To be called from diary-list-sexp-entries, where DATE is bound.
  185. ;;;###diary-autoload
  186. (defun diary-persian-date ()
  187. "Persian calendar equivalent of date diary entry."
  188. (format "Persian date: %s" (calendar-persian-date-string date)))
  189. (provide 'cal-persia)
  190. ;;; cal-persia.el ends here