Update copyright pending Emacs 22.
[bpt/emacs.git] / lisp / calendar / cal-coptic.el
1 ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars
2
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Human-Keywords: Coptic calendar, Ethiopic 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
15 ;; the Free Software Foundation; either version 2, or (at your option)
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
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;; This collection of functions implements the features of calendar.el and
31 ;; diary.el that deal with the Coptic and Ethiopic calendars.
32
33 ;; Technical details of all the calendrical calculations can be found in
34 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
35 ;; and Nachum Dershowitz, Cambridge University Press (2001).
36
37 ;; Comments, corrections, and improvements should be sent to
38 ;; Edward M. Reingold Department of Computer Science
39 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
40 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
41 ;; Urbana, Illinois 61801
42
43 ;;; Code:
44
45 (defvar date)
46
47 (require 'cal-julian)
48
49 (defvar coptic-calendar-month-name-array
50 ["Tut" "Babah" "Hatur" "Kiyahk" "Tubah" "Amshir" "Baramhat" "Barmundah"
51 "Bashans" "Baunah" "Abib" "Misra" "al-Nasi"])
52
53 (defvar coptic-calendar-epoch (calendar-absolute-from-julian '(8 29 284))
54 "Absolute date of start of Coptic calendar = August 29, 284 A.D. (Julian).")
55
56 (defvar coptic-name "Coptic")
57
58 (defun coptic-calendar-leap-year-p (year)
59 "True if YEAR is a leap year on the Coptic calendar."
60 (zerop (mod (1+ year) 4)))
61
62 (defun coptic-calendar-last-day-of-month (month year)
63 "Return last day of MONTH, YEAR on the Coptic calendar.
64 The 13th month is not really a month, but the 5 (6 in leap years) day period of
65 Nisi (Kebus) at the end of the year."
66 (if (< month 13)
67 30
68 (if (coptic-calendar-leap-year-p year)
69 6
70 5)))
71
72 (defun calendar-absolute-from-coptic (date)
73 "Compute absolute date from Coptic date DATE.
74 The absolute date is the number of days elapsed since the (imaginary)
75 Gregorian date Sunday, December 31, 1 BC."
76 (let ((month (extract-calendar-month date))
77 (day (extract-calendar-day date))
78 (year (extract-calendar-year date)))
79 (+ (1- coptic-calendar-epoch);; Days before start of calendar
80 (* 365 (1- year)) ;; Days in prior years
81 (/ year 4) ;; Leap days in prior years
82 (* 30 (1- month)) ;; Days in prior months this year
83 day))) ;; Days so far this month
84
85
86 (defun calendar-coptic-from-absolute (date)
87 "Compute the Coptic equivalent for absolute date DATE.
88 The result is a list of the form (MONTH DAY YEAR).
89 The absolute date is the number of days elapsed since the imaginary
90 Gregorian date Sunday, December 31, 1 BC."
91 (if (< date coptic-calendar-epoch)
92 (list 0 0 0);; pre-Coptic date
93 (let* ((approx (/ (- date coptic-calendar-epoch)
94 366)) ;; Approximation from below.
95 (year ;; Search forward from the approximation.
96 (+ approx
97 (calendar-sum y approx
98 (>= date (calendar-absolute-from-coptic (list 1 1 (1+ y))))
99 1)))
100 (month ;; Search forward from Tot.
101 (1+ (calendar-sum m 1
102 (> date
103 (calendar-absolute-from-coptic
104 (list m
105 (coptic-calendar-last-day-of-month m year)
106 year)))
107 1)))
108 (day ;; Calculate the day by subtraction.
109 (- date
110 (1- (calendar-absolute-from-coptic (list month 1 year))))))
111 (list month day year))))
112
113 (defun calendar-coptic-date-string (&optional date)
114 "String of Coptic date of Gregorian DATE.
115 Returns the empty string if DATE is pre-Coptic calendar.
116 Defaults to today's date if DATE is not given."
117 (let* ((coptic-date (calendar-coptic-from-absolute
118 (calendar-absolute-from-gregorian
119 (or date (calendar-current-date)))))
120 (y (extract-calendar-year coptic-date))
121 (m (extract-calendar-month coptic-date)))
122 (if (< y 1)
123 ""
124 (let ((monthname (aref coptic-calendar-month-name-array (1- m)))
125 (day (int-to-string (extract-calendar-day coptic-date)))
126 (dayname nil)
127 (month (int-to-string m))
128 (year (int-to-string y)))
129 (mapconcat 'eval calendar-date-display-form "")))))
130
131 (defun calendar-print-coptic-date ()
132 "Show the Coptic calendar equivalent of the selected date."
133 (interactive)
134 (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t))))
135 (if (string-equal f "")
136 (message "Date is pre-%s calendar" coptic-name)
137 (message "%s date: %s" coptic-name f))))
138
139 (defun calendar-goto-coptic-date (date &optional noecho)
140 "Move cursor to Coptic date DATE.
141 Echo Coptic date unless NOECHO is t."
142 (interactive (coptic-prompt-for-date))
143 (calendar-goto-date (calendar-gregorian-from-absolute
144 (calendar-absolute-from-coptic date)))
145 (or noecho (calendar-print-coptic-date)))
146
147 (defun coptic-prompt-for-date ()
148 "Ask for a Coptic date."
149 (let* ((today (calendar-current-date))
150 (year (calendar-read
151 (format "%s calendar year (>0): " coptic-name)
152 '(lambda (x) (> x 0))
153 (int-to-string
154 (extract-calendar-year
155 (calendar-coptic-from-absolute
156 (calendar-absolute-from-gregorian today))))))
157 (completion-ignore-case t)
158 (month (cdr (assoc-string
159 (completing-read
160 (format "%s calendar month name: " coptic-name)
161 (mapcar 'list
162 (append coptic-calendar-month-name-array nil))
163 nil t)
164 (calendar-make-alist coptic-calendar-month-name-array
165 1) t)))
166 (last (coptic-calendar-last-day-of-month month year))
167 (day (calendar-read
168 (format "%s calendar day (1-%d): " coptic-name last)
169 '(lambda (x) (and (< 0 x) (<= x last))))))
170 (list (list month day year))))
171
172 (defun diary-coptic-date ()
173 "Coptic calendar equivalent of date diary entry."
174 (let ((f (calendar-coptic-date-string date)))
175 (if (string-equal f "")
176 (format "Date is pre-%s calendar" coptic-name)
177 (format "%s date: %s" coptic-name f))))
178
179 (defconst ethiopic-calendar-month-name-array
180 ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
181 "Genbot" "Sane" "Hamle" "Nahas" "Paguem"])
182
183 (defconst ethiopic-calendar-epoch 2796
184 "Absolute date of start of Ethiopic calendar = August 29, 8 C.E. (Julian).")
185
186 (defconst ethiopic-name "Ethiopic")
187
188 (defun calendar-absolute-from-ethiopic (date)
189 "Compute absolute date from Ethiopic date DATE.
190 The absolute date is the number of days elapsed since the (imaginary)
191 Gregorian date Sunday, December 31, 1 BC."
192 (let ((coptic-calendar-epoch ethiopic-calendar-epoch))
193 (calendar-absolute-from-coptic date)))
194
195 (defun calendar-ethiopic-from-absolute (date)
196 "Compute the Ethiopic equivalent for absolute date DATE.
197 The result is a list of the form (MONTH DAY YEAR).
198 The absolute date is the number of days elapsed since the imaginary
199 Gregorian date Sunday, December 31, 1 BC."
200 (let ((coptic-calendar-epoch ethiopic-calendar-epoch))
201 (calendar-coptic-from-absolute date)))
202
203 (defun calendar-ethiopic-date-string (&optional date)
204 "String of Ethiopic date of Gregorian DATE.
205 Returns the empty string if DATE is pre-Ethiopic calendar.
206 Defaults to today's date if DATE is not given."
207 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
208 (coptic-name ethiopic-name)
209 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
210 (calendar-coptic-date-string date)))
211
212 (defun calendar-print-ethiopic-date ()
213 "Show the Ethiopic calendar equivalent of the selected date."
214 (interactive)
215 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
216 (coptic-name ethiopic-name)
217 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
218 (call-interactively 'calendar-print-coptic-date)))
219
220 (defun calendar-goto-ethiopic-date (date &optional noecho)
221 "Move cursor to Ethiopic date DATE.
222 Echo Ethiopic date unless NOECHO is t."
223 (interactive
224 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
225 (coptic-name ethiopic-name)
226 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
227 (coptic-prompt-for-date)))
228 (calendar-goto-date (calendar-gregorian-from-absolute
229 (calendar-absolute-from-ethiopic date)))
230 (or noecho (calendar-print-ethiopic-date)))
231
232 (defun diary-ethiopic-date ()
233 "Ethiopic calendar equivalent of date diary entry."
234 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
235 (coptic-name ethiopic-name)
236 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
237 (diary-coptic-date)))
238
239 (provide 'cal-coptic)
240
241 ;;; arch-tag: 72d49161-25df-4072-9312-b182cdca7627
242 ;;; cal-coptic.el ends here