Commit | Line | Data |
---|---|---|
3afbc435 | 1 | ;;; cal-persia.el --- calendar functions for the Persian calendar |
0e41f190 | 2 | |
b0b671db GM |
3 | ;; Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
4 | ;; 2008 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 | |
10 | ||
11 | ;; This file is part of GNU Emacs. | |
12 | ||
13 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 | ;; it under the terms of the GNU General Public License as published by | |
075969b4 | 15 | ;; the Free Software Foundation; either version 3, or (at your option) |
0e41f190 ER |
16 | ;; any later version. |
17 | ||
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
6b091ffc | 24 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3a35cf56 LK |
25 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 | ;; Boston, MA 02110-1301, USA. | |
0e41f190 ER |
27 | |
28 | ;;; Commentary: | |
29 | ||
30 | ;; This collection of functions implements the features of calendar.el and | |
31 | ;; diary.el that deal with the Persian calendar. | |
32 | ||
0e41f190 ER |
33 | ;;; Code: |
34 | ||
35 | (require 'cal-julian) | |
36 | ||
b0b671db | 37 | (defconst persian-calendar-month-name-array |
0e41f190 | 38 | ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban" |
d01890ee GM |
39 | "Azar" "Dey" "Bahman" "Esfand"] |
40 | "Names of the months in the Persian calendar.") | |
0e41f190 | 41 | |
b0b671db | 42 | (defconst persian-calendar-epoch (calendar-absolute-from-julian '(3 19 622)) |
d01890ee | 43 | "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).") |
0e41f190 ER |
44 | |
45 | (defun persian-calendar-leap-year-p (year) | |
46 | "True if YEAR is a leap year on the Persian calendar." | |
47 | (< (mod (* (mod (mod (if (<= 0 year) | |
b0b671db | 48 | (+ year 2346) ; no year zero |
0e41f190 ER |
49 | (+ year 2347)) |
50 | 2820) | |
51 | 768) | |
b0b671db GM |
52 | 683) |
53 | 2820) | |
54 | 683)) | |
0e41f190 ER |
55 | |
56 | (defun persian-calendar-last-day-of-month (month year) | |
57 | "Return last day of MONTH, YEAR on the Persian calendar." | |
58 | (cond | |
59 | ((< month 7) 31) | |
60 | ((or (< month 12) (persian-calendar-leap-year-p year)) 30) | |
61 | (t 29))) | |
62 | ||
63 | (defun calendar-absolute-from-persian (date) | |
64 | "Compute absolute date from Persian date DATE. | |
65 | The absolute date is the number of days elapsed since the (imaginary) | |
66 | Gregorian date Sunday, December 31, 1 BC." | |
67 | (let ((month (extract-calendar-month date)) | |
68 | (day (extract-calendar-day date)) | |
69 | (year (extract-calendar-year date))) | |
70 | (if (< year 0) | |
71 | (+ (calendar-absolute-from-persian | |
72 | (list month day (1+ (mod year 2820)))) | |
73 | (* 1029983 (floor year 2820))) | |
d01890ee GM |
74 | (+ (1- persian-calendar-epoch) ; days before epoch |
75 | (* 365 (1- year)) ; days in prior years | |
76 | (* 683 ; leap days in prior 2820-year cycles | |
0e41f190 | 77 | (floor (+ year 2345) 2820)) |
d01890ee | 78 | (* 186 ; leap days in prior 768 year cycles |
0e41f190 | 79 | (floor (mod (+ year 2345) 2820) 768)) |
d01890ee | 80 | (floor ; leap years in current 768 or 516 year cycle |
0e41f190 ER |
81 | (* 683 (mod (mod (+ year 2345) 2820) 768)) |
82 | 2820) | |
d01890ee GM |
83 | -568 ; leap years in Persian years -2345...-1 |
84 | (calendar-sum ; days in prior months this year | |
0e41f190 ER |
85 | m 1 (< m month) |
86 | (persian-calendar-last-day-of-month m year)) | |
d01890ee | 87 | day)))) ; days so far this month |
0e41f190 ER |
88 | |
89 | (defun calendar-persian-year-from-absolute (date) | |
90 | "Persian year corresponding to the absolute DATE." | |
d01890ee | 91 | (let* ((d0 ; prior days since start of 2820 cycles |
0e41f190 | 92 | (- date (calendar-absolute-from-persian (list 1 1 -2345)))) |
d01890ee | 93 | (n2820 ; completed 2820-year cycles |
0e41f190 | 94 | (floor d0 1029983)) |
d01890ee | 95 | (d1 ; prior days not in n2820 |
0e41f190 | 96 | (mod d0 1029983)) |
d01890ee | 97 | (n768 ; 768-year cycles not in n2820 |
0e41f190 | 98 | (floor d1 280506)) |
d01890ee | 99 | (d2 ; prior days not in n2820 or n768 |
0e41f190 | 100 | (mod d1 280506)) |
52e0f59e | 101 | (n1 ; years not in n2820 or n768 |
d01890ee GM |
102 | ;; Want: |
103 | ;; (floor (+ (* 2820 d2) (* 2820 366)) 1029983)) | |
104 | ;; but that causes overflow, so use the following. | |
105 | ;; Use 366 as the divisor because (2820*366 mod 1029983) is small. | |
106 | (let ((a (floor d2 366)) | |
0e41f190 ER |
107 | (b (mod d2 366))) |
108 | (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983)))) | |
d01890ee GM |
109 | (year (+ (* 2820 n2820) ; complete 2820 year cycles |
110 | (* 768 n768) ; complete 768 year cycles | |
111 | ;; Remaining years. | |
112 | (if (= d1 1029617) ; last day of 2820 year cycle | |
0e41f190 ER |
113 | (1- n1) |
114 | n1) | |
d01890ee | 115 | -2345))) ; years before year 1 |
0e41f190 | 116 | (if (< year 1) |
d01890ee | 117 | (1- year) ; no year zero |
0e41f190 ER |
118 | year))) |
119 | ||
120 | (defun calendar-persian-from-absolute (date) | |
121 | "Compute the Persian equivalent for absolute date DATE. | |
122 | The result is a list of the form (MONTH DAY YEAR). | |
123 | The absolute date is the number of days elapsed since the imaginary | |
124 | Gregorian date Sunday, December 31, 1 BC." | |
125 | (let* ((year (calendar-persian-year-from-absolute date)) | |
d01890ee | 126 | (month ; search forward from Farvardin |
0e41f190 ER |
127 | (1+ (calendar-sum m 1 |
128 | (> date | |
129 | (calendar-absolute-from-persian | |
130 | (list | |
131 | m | |
132 | (persian-calendar-last-day-of-month m year) | |
133 | year))) | |
134 | 1))) | |
d01890ee | 135 | (day ; calculate the day by subtraction |
0e41f190 ER |
136 | (- date (1- (calendar-absolute-from-persian |
137 | (list month 1 year)))))) | |
138 | (list month day year))) | |
139 | ||
6c1841ba | 140 | ;;;###cal-autoload |
0e41f190 | 141 | (defun calendar-persian-date-string (&optional date) |
d01890ee | 142 | "String of Persian date of Gregorian DATE, default today." |
0e41f190 | 143 | (let* ((persian-date (calendar-persian-from-absolute |
52e0f59e GM |
144 | (calendar-absolute-from-gregorian |
145 | (or date (calendar-current-date))))) | |
0e41f190 ER |
146 | (y (extract-calendar-year persian-date)) |
147 | (m (extract-calendar-month persian-date))) | |
148 | (let ((monthname (aref persian-calendar-month-name-array (1- m))) | |
149 | (day (int-to-string (extract-calendar-day persian-date))) | |
150 | (dayname nil) | |
151 | (month (int-to-string m)) | |
152 | (year (int-to-string y))) | |
153 | (mapconcat 'eval calendar-date-display-form "")))) | |
154 | ||
6c1841ba | 155 | ;;;###cal-autoload |
0e41f190 ER |
156 | (defun calendar-print-persian-date () |
157 | "Show the Persian calendar equivalent of the selected date." | |
158 | (interactive) | |
159 | (message "Persian date: %s" | |
160 | (calendar-persian-date-string (calendar-cursor-to-date t)))) | |
161 | ||
6c1841ba | 162 | ;;;###cal-autoload |
0e41f190 ER |
163 | (defun calendar-goto-persian-date (date &optional noecho) |
164 | "Move cursor to Persian date DATE. | |
52e0f59e | 165 | Echo Persian date unless NOECHO is non-nil." |
0e41f190 ER |
166 | (interactive (persian-prompt-for-date)) |
167 | (calendar-goto-date (calendar-gregorian-from-absolute | |
168 | (calendar-absolute-from-persian date))) | |
169 | (or noecho (calendar-print-persian-date))) | |
170 | ||
171 | (defun persian-prompt-for-date () | |
172 | "Ask for a Persian date." | |
d01890ee | 173 | (let* ((year (calendar-read |
0e41f190 | 174 | "Persian calendar year (not 0): " |
b0b671db | 175 | (lambda (x) (not (zerop x))) |
0e41f190 ER |
176 | (int-to-string |
177 | (extract-calendar-year | |
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 | |
186 | (append persian-calendar-month-name-array nil)) | |
187 | nil t) | |
0e41f190 | 188 | (calendar-make-alist persian-calendar-month-name-array |
c0982a97 | 189 | 1)))) |
0e41f190 ER |
190 | (last (persian-calendar-last-day-of-month month year)) |
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 | ||
b0b671db GM |
196 | (defvar date) |
197 | ||
198 | ;; To be called from list-sexp-diary-entries, where DATE is bound. | |
6c1841ba | 199 | ;;;###diary-autoload |
0e41f190 ER |
200 | (defun diary-persian-date () |
201 | "Persian calendar equivalent of date diary entry." | |
edbf2694 | 202 | (format "Persian date: %s" (calendar-persian-date-string date))) |
0e41f190 | 203 | |
a978be66 | 204 | (provide 'cal-persia) |
0e41f190 | 205 | |
5942f9af | 206 | ;; arch-tag: 2832383c-e4b4-4dc2-8ee9-cfbdd53e5e2d |
a978be66 | 207 | ;;; cal-persia.el ends here |