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