Renamed from cal-persian.el to avoid 14-character limitation.
[bpt/emacs.git] / lisp / calendar / cal-persia.el
1 ;;; cal-persia.el --- calendar functions for the Persian calendar.
2
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Keywords: calendar
7 ;; Human-Keywords: Persian calendar, calendar, diary
8
9 ;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This collection of functions implements the features of calendar.el and
29 ;; diary.el that deal with the Persian calendar.
30
31 ;; Comments, corrections, and improvements should be sent to
32 ;; Edward M. Reingold Department of Computer Science
33 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
34 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
35 ;; Urbana, Illinois 61801
36
37 ;;; Code:
38
39 (require 'cal-julian)
40
41 (defvar persian-calendar-month-name-array
42 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
43 "Azar" "Dey" "Bahman" "Esfand"])
44
45 (defvar persian-calendar-epoch (calendar-absolute-from-julian '(3 19 622))
46 "Absolute date of start of Persian calendar = March 19, 622 A.D. (Julian).")
47
48 (defun persian-calendar-leap-year-p (year)
49 "True if YEAR is a leap year on the Persian calendar."
50 (< (mod (* (mod (mod (if (<= 0 year)
51 ; No year zero
52 (+ year 2346)
53 (+ year 2347))
54 2820)
55 768)
56 683)
57 2820)
58 683))
59
60 (defun persian-calendar-last-day-of-month (month year)
61 "Return last day of MONTH, YEAR on the Persian calendar."
62 (cond
63 ((< month 7) 31)
64 ((or (< month 12) (persian-calendar-leap-year-p year)) 30)
65 (t 29)))
66
67 (defun calendar-absolute-from-persian (date)
68 "Compute absolute date from Persian date DATE.
69 The absolute date is the number of days elapsed since the (imaginary)
70 Gregorian date Sunday, December 31, 1 BC."
71 (let ((month (extract-calendar-month date))
72 (day (extract-calendar-day date))
73 (year (extract-calendar-year date)))
74 (if (< year 0)
75 (+ (calendar-absolute-from-persian
76 (list month day (1+ (mod year 2820))))
77 (* 1029983 (floor year 2820)))
78 (+ (1- persian-calendar-epoch); Days before epoch
79 (* 365 (1- year)) ; Days in prior years.
80 (* 683 ; Leap days in prior 2820-year cycles
81 (floor (+ year 2345) 2820))
82 (* 186 ; Leap days in prior 768 year cycles
83 (floor (mod (+ year 2345) 2820) 768))
84 (floor; Leap years in current 768 or 516 year cycle
85 (* 683 (mod (mod (+ year 2345) 2820) 768))
86 2820)
87 -568 ; Leap years in Persian years -2345...-1
88 (calendar-sum ; Days in prior months this year.
89 m 1 (< m month)
90 (persian-calendar-last-day-of-month m year))
91 day)))) ; Days so far this month.
92
93 (defun calendar-persian-year-from-absolute (date)
94 "Persian year corresponding to the absolute DATE."
95 (let* ((d0 ; Prior days since start of 2820 cycles
96 (- date (calendar-absolute-from-persian (list 1 1 -2345))))
97 (n2820 ; Completed 2820-year cycles
98 (floor d0 1029983))
99 (d1 ; Prior days not in n2820
100 (mod d0 1029983))
101 (n768 ; 768-year cycles not in n2820
102 (floor d1 280506))
103 (d2 ; Prior days not in n2820 or n768
104 (mod d1 280506))
105 (n1 ; Years not in n2820 or n768
106 ; we want is
107 ; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
108 ; but that causes overflow, so we use
109 (let ((a (floor d2 366)); we use 366 as the divisor because
110 ; (2820*366 mod 1029983) is small
111 (b (mod d2 366)))
112 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983))))
113 (year (+ (* 2820 n2820); Complete 2820 year cycles
114 (* 768 n768) ; Complete 768 year cycles
115 (if ; Remaining years
116 ; Last day of 2820 year cycle
117 (= d1 1029617)
118 (1- n1)
119 n1)
120 -2345))) ; Years before year 1
121 (if (< year 1)
122 (1- year); No year zero
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))
131 (month ; Search forward from Farvardin
132 (1+ (calendar-sum m 1
133 (> date
134 (calendar-absolute-from-persian
135 (list
136 m
137 (persian-calendar-last-day-of-month m year)
138 year)))
139 1)))
140 (day ; Calculate the day by subtraction
141 (- date (1- (calendar-absolute-from-persian
142 (list month 1 year))))))
143 (list month day year)))
144
145 (defun calendar-persian-date-string (&optional date)
146 "String of Persian date of Gregorian DATE.
147 Defaults to today's date if DATE is not given."
148 (let* ((persian-date (calendar-persian-from-absolute
149 (calendar-absolute-from-gregorian
150 (or date (calendar-current-date)))))
151 (y (extract-calendar-year persian-date))
152 (m (extract-calendar-month persian-date)))
153 (let ((monthname (aref persian-calendar-month-name-array (1- m)))
154 (day (int-to-string (extract-calendar-day persian-date)))
155 (dayname nil)
156 (month (int-to-string m))
157 (year (int-to-string y)))
158 (mapconcat 'eval calendar-date-display-form ""))))
159
160 (defun calendar-print-persian-date ()
161 "Show the Persian calendar equivalent of the selected date."
162 (interactive)
163 (message "Persian date: %s"
164 (calendar-persian-date-string (calendar-cursor-to-date t))))
165
166 (defun calendar-goto-persian-date (date &optional noecho)
167 "Move cursor to Persian date DATE.
168 Echo Persian date unless NOECHO is t."
169 (interactive (persian-prompt-for-date))
170 (calendar-goto-date (calendar-gregorian-from-absolute
171 (calendar-absolute-from-persian date)))
172 (or noecho (calendar-print-persian-date)))
173
174 (defun persian-prompt-for-date ()
175 "Ask for a Persian date."
176 (let* ((today (calendar-current-date))
177 (year (calendar-read
178 "Persian calendar year (not 0): "
179 '(lambda (x) (/= x 0))
180 (int-to-string
181 (extract-calendar-year
182 (calendar-persian-from-absolute
183 (calendar-absolute-from-gregorian today))))))
184 (completion-ignore-case t)
185 (month (cdr (assoc
186 (capitalize
187 (completing-read
188 "Persian calendar month name: "
189 (mapcar 'list
190 (append persian-calendar-month-name-array nil))
191 nil t))
192 (calendar-make-alist persian-calendar-month-name-array
193 1 'capitalize))))
194 (last (persian-calendar-last-day-of-month month year))
195 (day (calendar-read
196 (format "Persian calendar day (1-%d): " last)
197 '(lambda (x) (and (< 0 x) (<= x last))))))
198 (list (list month day year))))
199
200 (defun diary-persian-date ()
201 "Persian calendar equivalent of date diary entry."
202 (calendar-persian-date-string (calendar-cursor-to-date t)))
203
204 (provide 'cal-persia)
205
206 ;;; cal-persia.el ends here