Formatting changes only.
[emacs.git] / lisp / calendar / cal-coptic.el
blob3a206d9866a00f72f29c2fa2f991aa7674074d95
1 ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
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
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 3, or (at your option)
16 ;; any later version.
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.
28 ;;; Commentary:
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 ;;; Code:
39 (require 'cal-julian)
41 ;; Not constants because they get let-bound.
43 (defvar coptic-calendar-month-name-array
44 ["Tut" "Babah" "Hatur" "Kiyahk" "Tubah" "Amshir" "Baramhat" "Barmundah"
45 "Bashans" "Baunah" "Abib" "Misra" "al-Nasi"])
47 (defvar coptic-calendar-epoch (calendar-absolute-from-julian '(8 29 284))
48 "Absolute date of start of Coptic calendar = August 29, 284 A.D. (Julian).")
50 (defvar coptic-name "Coptic"
51 "Used in some message strings.")
53 (defun coptic-calendar-leap-year-p (year)
54 "True if YEAR is a leap year on the Coptic calendar."
55 (zerop (mod (1+ year) 4)))
57 (defun coptic-calendar-last-day-of-month (month year)
58 "Return last day of MONTH, YEAR on the Coptic calendar.
59 The 13th month is not really a month, but the 5 (6 in leap years) day period of
60 Nisi (Kebus) at the end of the year."
61 (if (< month 13)
63 (if (coptic-calendar-leap-year-p year)
65 5)))
67 (defun calendar-absolute-from-coptic (date)
68 "Compute absolute date from Coptic date DATE.
69 The absolute date is the number of days elapsed since the (imaginary)
70 Gregorian date Sunday, December 31, 1 BC."
71 (let ((month (extract-calendar-month date))
72 (day (extract-calendar-day date))
73 (year (extract-calendar-year date)))
74 (+ (1- coptic-calendar-epoch) ; days before start of calendar
75 (* 365 (1- year)) ; days in prior years
76 (/ year 4) ; leap days in prior years
77 (* 30 (1- month)) ; days in prior months this year
78 day))) ; days so far this month
81 (defun calendar-coptic-from-absolute (date)
82 "Compute the Coptic equivalent for absolute date DATE.
83 The result is a list of the form (MONTH DAY YEAR).
84 The absolute date is the number of days elapsed since the imaginary
85 Gregorian date Sunday, December 31, 1 BC."
86 (if (< date coptic-calendar-epoch)
87 (list 0 0 0) ; pre-Coptic date
88 (let* ((approx (/ (- date coptic-calendar-epoch)
89 366)) ; approximation from below
90 (year ; search forward from the approximation
91 (+ approx
92 (calendar-sum y approx
93 (>= date (calendar-absolute-from-coptic (list 1 1 (1+ y))))
94 1)))
95 (month ; search forward from Tot
96 (1+ (calendar-sum m 1
97 (> date
98 (calendar-absolute-from-coptic
99 (list m
100 (coptic-calendar-last-day-of-month m year)
101 year)))
102 1)))
103 (day ; calculate the day by subtraction
104 (- date
105 (1- (calendar-absolute-from-coptic (list month 1 year))))))
106 (list month day year))))
108 ;;;###autoload
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)))
118 (if (< y 1)
120 (let ((monthname (aref coptic-calendar-month-name-array (1- m)))
121 (day (int-to-string (extract-calendar-day coptic-date)))
122 (dayname nil)
123 (month (int-to-string m))
124 (year (int-to-string y)))
125 (mapconcat 'eval calendar-date-display-form "")))))
127 ;;;###autoload
128 (defun calendar-print-coptic-date ()
129 "Show the Coptic calendar equivalent of the selected date."
130 (interactive)
131 (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t))))
132 (if (string-equal f "")
133 (message "Date is pre-%s calendar" coptic-name)
134 (message "%s date: %s" coptic-name f))))
136 ;;;###autoload
137 (defun calendar-goto-coptic-date (date &optional noecho)
138 "Move cursor to Coptic date DATE.
139 Echo Coptic date unless NOECHO is t."
140 (interactive (coptic-prompt-for-date))
141 (calendar-goto-date (calendar-gregorian-from-absolute
142 (calendar-absolute-from-coptic date)))
143 (or noecho (calendar-print-coptic-date)))
145 (defun coptic-prompt-for-date ()
146 "Ask for a Coptic date."
147 (let* ((today (calendar-current-date))
148 (year (calendar-read
149 (format "%s calendar year (>0): " coptic-name)
150 (lambda (x) (> x 0))
151 (int-to-string
152 (extract-calendar-year
153 (calendar-coptic-from-absolute
154 (calendar-absolute-from-gregorian today))))))
155 (completion-ignore-case t)
156 (month (cdr (assoc-string
157 (completing-read
158 (format "%s calendar month name: " coptic-name)
159 (mapcar 'list
160 (append coptic-calendar-month-name-array nil))
161 nil t)
162 (calendar-make-alist coptic-calendar-month-name-array
163 1) t)))
164 (last (coptic-calendar-last-day-of-month month year))
165 (day (calendar-read
166 (format "%s calendar day (1-%d): " coptic-name last)
167 (lambda (x) (and (< 0 x) (<= x last))))))
168 (list (list month day year))))
170 (defvar date)
172 ;; To be called from list-sexp-diary-entries, where DATE is bound.
173 (defun diary-coptic-date ()
174 "Coptic calendar equivalent of date diary entry."
175 (let ((f (calendar-coptic-date-string date)))
176 (if (string-equal f "")
177 (format "Date is pre-%s calendar" coptic-name)
178 (format "%s date: %s" coptic-name f))))
180 (defconst ethiopic-calendar-month-name-array
181 ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
182 "Genbot" "Sane" "Hamle" "Nahas" "Paguem"])
184 (defconst ethiopic-calendar-epoch 2796
185 "Absolute date of start of Ethiopic calendar = August 29, 8 C.E. (Julian).")
187 (defconst ethiopic-name "Ethiopic")
189 (defun calendar-absolute-from-ethiopic (date)
190 "Compute absolute date from Ethiopic date DATE.
191 The absolute date is the number of days elapsed since the (imaginary)
192 Gregorian date Sunday, December 31, 1 BC."
193 (let ((coptic-calendar-epoch ethiopic-calendar-epoch))
194 (calendar-absolute-from-coptic date)))
196 (defun calendar-ethiopic-from-absolute (date)
197 "Compute the Ethiopic equivalent for absolute date DATE.
198 The result is a list of the form (MONTH DAY YEAR).
199 The absolute date is the number of days elapsed since the imaginary
200 Gregorian date Sunday, December 31, 1 BC."
201 (let ((coptic-calendar-epoch ethiopic-calendar-epoch))
202 (calendar-coptic-from-absolute date)))
204 ;;;###autoload
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)))
214 ;;;###autoload
215 (defun calendar-print-ethiopic-date ()
216 "Show the Ethiopic calendar equivalent of the selected date."
217 (interactive)
218 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
219 (coptic-name ethiopic-name)
220 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
221 (call-interactively 'calendar-print-coptic-date)))
223 ;;;###autoload
224 (defun calendar-goto-ethiopic-date (date &optional noecho)
225 "Move cursor to Ethiopic date DATE.
226 Echo Ethiopic date unless NOECHO is t."
227 (interactive
228 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
229 (coptic-name ethiopic-name)
230 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
231 (coptic-prompt-for-date)))
232 (calendar-goto-date (calendar-gregorian-from-absolute
233 (calendar-absolute-from-ethiopic date)))
234 (or noecho (calendar-print-ethiopic-date)))
236 ;; To be called from list-sexp-diary-entries, where DATE is bound.
237 (defun diary-ethiopic-date ()
238 "Ethiopic calendar equivalent of date diary entry."
239 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
240 (coptic-name ethiopic-name)
241 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
242 (diary-coptic-date)))
244 (provide 'cal-coptic)
246 ;; Local Variables:
247 ;; generated-autoload-file: "cal-loaddefs.el"
248 ;; End:
250 ;; arch-tag: 72d49161-25df-4072-9312-b182cdca7627
251 ;;; cal-coptic.el ends here