1 ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars
3 ;; Copyright (C) 1995, 1997 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Human-Keywords: Coptic calendar, Ethiopic calendar, calendar, diary
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; This collection of functions implements the features of calendar.el and
29 ;; diary.el that deal with the Coptic and Ethiopic calendars.
31 ;; Technical details of all the calendrical calculations can be found in
32 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
33 ;; Cambridge University Press (1997).
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
45 (defvar coptic-calendar-month-name-array
46 ["Tut" "Babah" "Hatur" "Kiyahk" "Tubah" "Amshir" "Baramhat" "Barmundah"
47 "Bashans" "Baunah" "Abib" "Misra" "al-Nasi"])
49 (defvar coptic-calendar-epoch
(calendar-absolute-from-julian '(8 29 284))
50 "Absolute date of start of Coptic calendar = August 29, 284 A.D. (Julian).")
52 (defconst coptic-name
"Coptic")
54 (defun coptic-calendar-leap-year-p (year)
55 "True if YEAR is a leap year on the Coptic calendar."
56 (zerop (mod (1+ year
) 4)))
58 (defun coptic-calendar-last-day-of-month (month year
)
59 "Return last day of MONTH, YEAR on the Coptic calendar.
60 The 13th month is not really a month, but the 5 (6 in leap years) day period of
61 Nisi (Kebus) at the end of the year."
64 (if (coptic-calendar-leap-year-p year
)
68 (defun calendar-absolute-from-coptic (date)
69 "Compute absolute date from Coptic date DATE.
70 The absolute date is the number of days elapsed since the (imaginary)
71 Gregorian date Sunday, December 31, 1 BC."
72 (let ((month (extract-calendar-month date
))
73 (day (extract-calendar-day date
))
74 (year (extract-calendar-year date
)))
75 (+ (1- coptic-calendar-epoch
);; Days before start of calendar
76 (* 365 (1- year
)) ;; Days in prior years
77 (/ year
4) ;; Leap days in prior years
78 (* 30 (1- month
)) ;; Days in prior months this year
79 day
))) ;; Days so far this month
82 (defun calendar-coptic-from-absolute (date)
83 "Compute the Coptic equivalent for absolute date DATE.
84 The result is a list of the form (MONTH DAY YEAR).
85 The absolute date is the number of days elapsed since the imaginary
86 Gregorian date Sunday, December 31, 1 BC."
87 (if (< date coptic-calendar-epoch
)
88 (list 0 0 0);; pre-Coptic date
89 (let* ((approx (/ (- date coptic-calendar-epoch
)
90 366)) ;; Approximation from below.
91 (year ;; Search forward from the approximation.
93 (calendar-sum y approx
94 (>= date
(calendar-absolute-from-coptic (list 1 1 (1+ y
))))
96 (month ;; Search forward from Tot.
99 (calendar-absolute-from-coptic
101 (coptic-calendar-last-day-of-month m year
)
104 (day ;; Calculate the day by subtraction.
106 (1- (calendar-absolute-from-coptic (list month
1 year
))))))
107 (list month day year
))))
109 (defun calendar-coptic-date-string (&optional date
)
110 "String of Coptic date of Gregorian DATE.
111 Returns the empty string if DATE is pre-Coptic calendar.
112 Defaults to today's date if DATE is not given."
113 (let* ((coptic-date (calendar-coptic-from-absolute
114 (calendar-absolute-from-gregorian
115 (or date
(calendar-current-date)))))
116 (y (extract-calendar-year coptic-date
))
117 (m (extract-calendar-month coptic-date
)))
120 (let ((monthname (aref coptic-calendar-month-name-array
(1- m
)))
121 (day (int-to-string (extract-calendar-day coptic-date
)))
123 (month (int-to-string m
))
124 (year (int-to-string y
)))
125 (mapconcat 'eval calendar-date-display-form
"")))))
127 (defun calendar-print-coptic-date ()
128 "Show the Coptic calendar equivalent of the selected date."
130 (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t
))))
131 (if (string-equal f
"")
132 (message "Date is pre-%s calendar" coptic-name
)
133 (message "%s date: %s" coptic-name f
))))
135 (defun calendar-goto-coptic-date (date &optional noecho
)
136 "Move cursor to Coptic date DATE.
137 Echo Coptic date unless NOECHO is t."
138 (interactive (coptic-prompt-for-date))
139 (calendar-goto-date (calendar-gregorian-from-absolute
140 (calendar-absolute-from-coptic date
)))
141 (or noecho
(calendar-print-coptic-date)))
143 (defun coptic-prompt-for-date ()
144 "Ask for a Coptic date."
145 (let* ((today (calendar-current-date))
147 (format "%s calendar year (>0): " coptic-name
)
148 '(lambda (x) (> x
0))
150 (extract-calendar-year
151 (calendar-coptic-from-absolute
152 (calendar-absolute-from-gregorian today
))))))
153 (completion-ignore-case t
)
154 (month (cdr (assoc-ignore-case
156 (format "%s calendar month name: " coptic-name
)
158 (append coptic-calendar-month-name-array nil
))
160 (calendar-make-alist coptic-calendar-month-name-array
162 (last (coptic-calendar-last-day-of-month month year
))
164 (format "%s calendar day (1-%d): " coptic-name last
)
165 '(lambda (x) (and (< 0 x
) (<= x last
))))))
166 (list (list month day year
))))
168 (defun diary-coptic-date ()
169 "Coptic calendar equivalent of date diary entry."
170 (let ((f (calendar-coptic-date-string date
)))
171 (if (string-equal f
"")
172 (format "Date is pre-%s calendar" coptic-name
)
173 (format "%s date: %s" coptic-name f
))))
175 (defconst ethiopic-calendar-month-name-array
176 ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
177 "Genbot" "Sane" "Hamle" "Nahas" "Paguem"])
179 (defconst ethiopic-calendar-epoch
2796
180 "Absolute date of start of Ethiopic calendar = August 29, 8 C.E. (Julian).")
182 (defconst ethiopic-name
"Ethiopic")
184 (defun calendar-absolute-from-ethiopic (date)
185 "Compute absolute date from Ethiopic date DATE.
186 The absolute date is the number of days elapsed since the (imaginary)
187 Gregorian date Sunday, December 31, 1 BC."
188 (let ((coptic-calendar-epoch ethiopic-calendar-epoch
))
189 (calendar-absolute-from-coptic date
)))
191 (defun calendar-ethiopic-from-absolute (date)
192 "Compute the Ethiopic equivalent for absolute date DATE.
193 The result is a list of the form (MONTH DAY YEAR).
194 The absolute date is the number of days elapsed since the imaginary
195 Gregorian date Sunday, December 31, 1 BC."
196 (let ((coptic-calendar-epoch ethiopic-calendar-epoch
))
197 (calendar-coptic-from-absolute date
)))
199 (defun calendar-ethiopic-date-string (&optional date
)
200 "String of Ethiopic date of Gregorian DATE.
201 Returns the empty string if DATE is pre-Ethiopic calendar.
202 Defaults to today's date if DATE is not given."
203 (let ((coptic-calendar-epoch ethiopic-calendar-epoch
)
204 (coptic-name ethiopic-name
)
205 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array
))
206 (calendar-coptic-date-string date
)))
208 (defun calendar-print-ethiopic-date ()
209 "Show the Ethiopic calendar equivalent of the selected date."
211 (let ((coptic-calendar-epoch ethiopic-calendar-epoch
)
212 (coptic-name ethiopic-name
)
213 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array
))
214 (call-interactively 'calendar-print-coptic-date
)))
216 (defun calendar-goto-ethiopic-date (date &optional noecho
)
217 "Move cursor to Ethiopic date DATE.
218 Echo Ethiopic date unless NOECHO is t."
220 (let ((coptic-calendar-epoch ethiopic-calendar-epoch
)
221 (coptic-name ethiopic-name
)
222 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array
))
223 (coptic-prompt-for-date)))
224 (calendar-goto-date (calendar-gregorian-from-absolute
225 (calendar-absolute-from-ethiopic date
)))
226 (or noecho
(calendar-print-ethiopic-date)))
228 (defun diary-ethiopic-date ()
229 "Ethiopic calendar equivalent of date diary entry."
230 (let ((coptic-calendar-epoch ethiopic-calendar-epoch
)
231 (coptic-name ethiopic-name
)
232 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array
))
233 (diary-coptic-date)))
235 (provide 'cal-coptic
)
237 ;;; cal-coptic.el ends here