Update copyright info.
[bpt/emacs.git] / lisp / calendar / cal-persia.el
CommitLineData
3afbc435 1;;; cal-persia.el --- calendar functions for the Persian calendar
0e41f190 2
a96a5fca 3;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
0e41f190
ER
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
6b091ffc 22;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
0e41f190
ER
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
a96a5fca 31;; Technical details of all the calendrical calculations can be found in
fffaba77
PE
32;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
33;; and Nachum Dershowitz, Cambridge University Press (2001).
a96a5fca 34
0e41f190
ER
35;; Comments, corrections, and improvements should be sent to
36;; Edward M. Reingold Department of Computer Science
37;; (217) 333-6733 University of Illinois at Urbana-Champaign
38;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
39;; Urbana, Illinois 61801
40
41;;; Code:
42
04ec3414
JB
43(defvar date)
44
0e41f190
ER
45(require 'cal-julian)
46
47(defvar persian-calendar-month-name-array
48 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
49 "Azar" "Dey" "Bahman" "Esfand"])
50
51(defvar persian-calendar-epoch (calendar-absolute-from-julian '(3 19 622))
52 "Absolute date of start of Persian calendar = March 19, 622 A.D. (Julian).")
53
54(defun persian-calendar-leap-year-p (year)
55 "True if YEAR is a leap year on the Persian calendar."
56 (< (mod (* (mod (mod (if (<= 0 year)
57 ; No year zero
58 (+ year 2346)
59 (+ year 2347))
60 2820)
61 768)
62 683)
63 2820)
64 683))
65
66(defun persian-calendar-last-day-of-month (month year)
67 "Return last day of MONTH, YEAR on the Persian calendar."
68 (cond
69 ((< month 7) 31)
70 ((or (< month 12) (persian-calendar-leap-year-p year)) 30)
71 (t 29)))
72
73(defun calendar-absolute-from-persian (date)
74 "Compute absolute date from Persian date DATE.
75The absolute date is the number of days elapsed since the (imaginary)
76Gregorian date Sunday, December 31, 1 BC."
77 (let ((month (extract-calendar-month date))
78 (day (extract-calendar-day date))
79 (year (extract-calendar-year date)))
80 (if (< year 0)
81 (+ (calendar-absolute-from-persian
82 (list month day (1+ (mod year 2820))))
83 (* 1029983 (floor year 2820)))
84 (+ (1- persian-calendar-epoch); Days before epoch
85 (* 365 (1- year)) ; Days in prior years.
86 (* 683 ; Leap days in prior 2820-year cycles
87 (floor (+ year 2345) 2820))
88 (* 186 ; Leap days in prior 768 year cycles
89 (floor (mod (+ year 2345) 2820) 768))
90 (floor; Leap years in current 768 or 516 year cycle
91 (* 683 (mod (mod (+ year 2345) 2820) 768))
92 2820)
93 -568 ; Leap years in Persian years -2345...-1
94 (calendar-sum ; Days in prior months this year.
95 m 1 (< m month)
96 (persian-calendar-last-day-of-month m year))
97 day)))) ; Days so far this month.
98
99(defun calendar-persian-year-from-absolute (date)
100 "Persian year corresponding to the absolute DATE."
101 (let* ((d0 ; Prior days since start of 2820 cycles
102 (- date (calendar-absolute-from-persian (list 1 1 -2345))))
103 (n2820 ; Completed 2820-year cycles
104 (floor d0 1029983))
105 (d1 ; Prior days not in n2820
106 (mod d0 1029983))
107 (n768 ; 768-year cycles not in n2820
108 (floor d1 280506))
109 (d2 ; Prior days not in n2820 or n768
110 (mod d1 280506))
111 (n1 ; Years not in n2820 or n768
112 ; we want is
113 ; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
114 ; but that causes overflow, so we use
115 (let ((a (floor d2 366)); we use 366 as the divisor because
116 ; (2820*366 mod 1029983) is small
117 (b (mod d2 366)))
118 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983))))
119 (year (+ (* 2820 n2820); Complete 2820 year cycles
120 (* 768 n768) ; Complete 768 year cycles
121 (if ; Remaining years
122 ; Last day of 2820 year cycle
123 (= d1 1029617)
124 (1- n1)
125 n1)
126 -2345))) ; Years before year 1
127 (if (< year 1)
128 (1- year); No year zero
129 year)))
130
131(defun calendar-persian-from-absolute (date)
132 "Compute the Persian equivalent for absolute date DATE.
133The result is a list of the form (MONTH DAY YEAR).
134The absolute date is the number of days elapsed since the imaginary
135Gregorian date Sunday, December 31, 1 BC."
136 (let* ((year (calendar-persian-year-from-absolute date))
137 (month ; Search forward from Farvardin
138 (1+ (calendar-sum m 1
139 (> date
140 (calendar-absolute-from-persian
141 (list
142 m
143 (persian-calendar-last-day-of-month m year)
144 year)))
145 1)))
146 (day ; Calculate the day by subtraction
147 (- date (1- (calendar-absolute-from-persian
148 (list month 1 year))))))
149 (list month day year)))
150
151(defun calendar-persian-date-string (&optional date)
152 "String of Persian date of Gregorian DATE.
153Defaults to today's date if DATE is not given."
154 (let* ((persian-date (calendar-persian-from-absolute
155 (calendar-absolute-from-gregorian
156 (or date (calendar-current-date)))))
157 (y (extract-calendar-year persian-date))
158 (m (extract-calendar-month persian-date)))
159 (let ((monthname (aref persian-calendar-month-name-array (1- m)))
160 (day (int-to-string (extract-calendar-day persian-date)))
161 (dayname nil)
162 (month (int-to-string m))
163 (year (int-to-string y)))
164 (mapconcat 'eval calendar-date-display-form ""))))
165
166(defun calendar-print-persian-date ()
167 "Show the Persian calendar equivalent of the selected date."
168 (interactive)
169 (message "Persian date: %s"
170 (calendar-persian-date-string (calendar-cursor-to-date t))))
171
172(defun calendar-goto-persian-date (date &optional noecho)
173 "Move cursor to Persian date DATE.
174Echo Persian date unless NOECHO is t."
175 (interactive (persian-prompt-for-date))
176 (calendar-goto-date (calendar-gregorian-from-absolute
177 (calendar-absolute-from-persian date)))
178 (or noecho (calendar-print-persian-date)))
179
180(defun persian-prompt-for-date ()
181 "Ask for a Persian date."
182 (let* ((today (calendar-current-date))
183 (year (calendar-read
184 "Persian calendar year (not 0): "
185 '(lambda (x) (/= x 0))
186 (int-to-string
187 (extract-calendar-year
188 (calendar-persian-from-absolute
189 (calendar-absolute-from-gregorian today))))))
190 (completion-ignore-case t)
191 (month (cdr (assoc
0e41f190
ER
192 (completing-read
193 "Persian calendar month name: "
194 (mapcar 'list
195 (append persian-calendar-month-name-array nil))
c0982a97 196 nil t)
0e41f190 197 (calendar-make-alist persian-calendar-month-name-array
c0982a97 198 1))))
0e41f190
ER
199 (last (persian-calendar-last-day-of-month month year))
200 (day (calendar-read
201 (format "Persian calendar day (1-%d): " last)
202 '(lambda (x) (and (< 0 x) (<= x last))))))
203 (list (list month day year))))
204
205(defun diary-persian-date ()
206 "Persian calendar equivalent of date diary entry."
edbf2694 207 (format "Persian date: %s" (calendar-persian-date-string date)))
0e41f190 208
a978be66 209(provide 'cal-persia)
0e41f190 210
ab5796a9 211;;; arch-tag: 2832383c-e4b4-4dc2-8ee9-cfbdd53e5e2d
a978be66 212;;; cal-persia.el ends here