1 ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;; Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Human-Keywords: Coptic calendar, Ethiopic calendar, calendar, diary
11 ;; This file is part of GNU Emacs.
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)
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.
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.
30 ;; This collection of functions implements the features of calendar.el and
31 ;; diary.el that deal with the Coptic and Ethiopic calendars.
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).
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
49 (defvar coptic-calendar-month-name-array
50 ["Tut" "Babah" "Hatur" "Kiyahk" "Tubah" "Amshir" "Baramhat" "Barmundah"
51 "Bashans" "Baunah" "Abib" "Misra" "al-Nasi"])
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).")
56 (defvar coptic-name
"Coptic")
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)))
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."
68 (if (coptic-calendar-leap-year-p year
)
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
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.
97 (calendar-sum y approx
98 (>= date
(calendar-absolute-from-coptic (list 1 1 (1+ y
))))
100 (month ;; Search forward from Tot.
101 (1+ (calendar-sum m
1
103 (calendar-absolute-from-coptic
105 (coptic-calendar-last-day-of-month m year
)
108 (day ;; Calculate the day by subtraction.
110 (1- (calendar-absolute-from-coptic (list month
1 year
))))))
111 (list month day year
))))
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
)))
124 (let ((monthname (aref coptic-calendar-month-name-array
(1- m
)))
125 (day (int-to-string (extract-calendar-day coptic-date
)))
127 (month (int-to-string m
))
128 (year (int-to-string y
)))
129 (mapconcat 'eval calendar-date-display-form
"")))))
131 (defun calendar-print-coptic-date ()
132 "Show the Coptic calendar equivalent of the selected date."
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
))))
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)))
147 (defun coptic-prompt-for-date ()
148 "Ask for a Coptic date."
149 (let* ((today (calendar-current-date))
151 (format "%s calendar year (>0): " coptic-name
)
152 '(lambda (x) (> x
0))
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
160 (format "%s calendar month name: " coptic-name
)
162 (append coptic-calendar-month-name-array nil
))
164 (calendar-make-alist coptic-calendar-month-name-array
166 (last (coptic-calendar-last-day-of-month month year
))
168 (format "%s calendar day (1-%d): " coptic-name last
)
169 '(lambda (x) (and (< 0 x
) (<= x last
))))))
170 (list (list month day year
))))
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
))))
179 (defconst ethiopic-calendar-month-name-array
180 ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
181 "Genbot" "Sane" "Hamle" "Nahas" "Paguem"])
183 (defconst ethiopic-calendar-epoch
2796
184 "Absolute date of start of Ethiopic calendar = August 29, 8 C.E. (Julian).")
186 (defconst ethiopic-name
"Ethiopic")
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
)))
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
)))
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
)))
212 (defun calendar-print-ethiopic-date ()
213 "Show the Ethiopic calendar equivalent of the selected date."
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
)))
220 (defun calendar-goto-ethiopic-date (date &optional noecho
)
221 "Move cursor to Ethiopic date DATE.
222 Echo Ethiopic date unless NOECHO is t."
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)))
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)))
239 (provide 'cal-coptic
)
241 ;;; arch-tag: 72d49161-25df-4072-9312-b182cdca7627
242 ;;; cal-coptic.el ends here