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