Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / calendar / cal-coptic.el
blob69612edab38ccd37db3f7b2f6f553de4f47b0a16
1 ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 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
10 ;; Package: calendar
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; See calendar.el.
31 ;;; Code:
33 (require 'calendar)
35 ;; Not constants because they get let-bound.
37 (defvar calendar-coptic-month-name-array
38 ["Tut" "Babah" "Hatur" "Kiyahk" "Tubah" "Amshir" "Baramhat" "Barmundah"
39 "Bashans" "Baunah" "Abib" "Misra" "al-Nasi"]
40 "Array of the month names in the Coptic calendar.")
42 (eval-and-compile
43 (autoload 'calendar-julian-to-absolute "cal-julian"))
45 (defvar calendar-coptic-epoch
46 (eval-when-compile (calendar-julian-to-absolute '(8 29 284)))
47 "Absolute date of start of Coptic calendar = August 29, 284 AD (Julian).")
49 (defvar calendar-coptic-name "Coptic"
50 "Used in some message strings.")
52 (defun calendar-coptic-leap-year-p (year)
53 "True if YEAR is a leap year on the Coptic calendar."
54 (zerop (mod (1+ year) 4)))
56 (defun calendar-coptic-last-day-of-month (month year)
57 "Return last day of MONTH, YEAR on the Coptic calendar.
58 The 13th month is not really a month, but the 5 (6 in leap years) day period of
59 Nisi (Kebus) at the end of the year."
60 (if (< month 13)
62 (if (calendar-coptic-leap-year-p year)
64 5)))
66 (defun calendar-coptic-to-absolute (date)
67 "Compute absolute date from Coptic date DATE.
68 The absolute date is the number of days elapsed since the (imaginary)
69 Gregorian date Sunday, December 31, 1 BC."
70 (let ((month (calendar-extract-month date))
71 (day (calendar-extract-day date))
72 (year (calendar-extract-year date)))
73 (+ (1- calendar-coptic-epoch) ; days before start of calendar
74 (* 365 (1- year)) ; days in prior years
75 (/ year 4) ; leap days in prior years
76 (* 30 (1- month)) ; days in prior months this year
77 day))) ; days so far this month
79 (define-obsolete-function-alias 'calendar-absolute-from-coptic
80 'calendar-coptic-to-absolute "23.1")
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 calendar-coptic-epoch)
88 (list 0 0 0) ; pre-Coptic date
89 (let* ((approx (/ (- date calendar-coptic-epoch)
90 366)) ; approximation from below
91 (year ; search forward from the approximation
92 (+ approx
93 (calendar-sum y approx
94 (>= date (calendar-coptic-to-absolute
95 (list 1 1 (1+ y))))
96 1)))
97 (month ; search forward from Tot
98 (1+ (calendar-sum m 1
99 (> date
100 (calendar-coptic-to-absolute
101 (list m
102 (calendar-coptic-last-day-of-month m
103 year)
104 year)))
105 1)))
106 (day ; calculate the day by subtraction
107 (- date
108 (1- (calendar-coptic-to-absolute (list month 1 year))))))
109 (list month day year))))
111 ;;;###cal-autoload
112 (defun calendar-coptic-date-string (&optional date)
113 "String of Coptic date of Gregorian DATE.
114 Returns the empty string if DATE is pre-Coptic calendar.
115 Defaults to today's date if DATE is not given."
116 (let* ((coptic-date (calendar-coptic-from-absolute
117 (calendar-absolute-from-gregorian
118 (or date (calendar-current-date)))))
119 (y (calendar-extract-year coptic-date))
120 (m (calendar-extract-month coptic-date)))
121 (if (< y 1)
123 (let ((monthname (aref calendar-coptic-month-name-array (1- m)))
124 (day (number-to-string (calendar-extract-day coptic-date)))
125 (dayname nil)
126 (month (number-to-string m))
127 (year (number-to-string y)))
128 (mapconcat 'eval calendar-date-display-form "")))))
130 ;;;###cal-autoload
131 (defun calendar-coptic-print-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" calendar-coptic-name)
137 (message "%s date: %s" calendar-coptic-name f))))
139 (define-obsolete-function-alias 'calendar-print-coptic-date
140 'calendar-coptic-print-date "23.1")
142 (defun calendar-coptic-read-date ()
143 "Interactively read the arguments for a Coptic date command.
144 Reads a year, month, and day."
145 (let* ((today (calendar-current-date))
146 (year (calendar-read
147 (format "%s calendar year (>0): " calendar-coptic-name)
148 (lambda (x) (> x 0))
149 (number-to-string
150 (calendar-extract-year
151 (calendar-coptic-from-absolute
152 (calendar-absolute-from-gregorian today))))))
153 (completion-ignore-case t)
154 (month (cdr (assoc-string
155 (completing-read
156 (format "%s calendar month name: " calendar-coptic-name)
157 (mapcar 'list
158 (append calendar-coptic-month-name-array nil))
159 nil t)
160 (calendar-make-alist calendar-coptic-month-name-array
161 1) t)))
162 (last (calendar-coptic-last-day-of-month month year))
163 (day (calendar-read
164 (format "%s calendar day (1-%d): " calendar-coptic-name last)
165 (lambda (x) (and (< 0 x) (<= x last))))))
166 (list (list month day year))))
168 (define-obsolete-function-alias 'coptic-prompt-for-date
169 'calendar-coptic-read-date "23.1")
171 ;;;###cal-autoload
172 (defun calendar-coptic-goto-date (date &optional noecho)
173 "Move cursor to Coptic date DATE.
174 Echo Coptic date unless NOECHO is t."
175 (interactive (calendar-coptic-read-date))
176 (calendar-goto-date (calendar-gregorian-from-absolute
177 (calendar-coptic-to-absolute date)))
178 (or noecho (calendar-coptic-print-date)))
180 (define-obsolete-function-alias 'calendar-goto-coptic-date
181 'calendar-coptic-goto-date "23.1")
183 (defvar date)
185 ;; To be called from diary-list-sexp-entries, where DATE is bound.
186 ;;;###diary-autoload
187 (defun diary-coptic-date ()
188 "Coptic calendar equivalent of date diary entry."
189 (let ((f (calendar-coptic-date-string date)))
190 (if (string-equal f "")
191 (format "Date is pre-%s calendar" calendar-coptic-name)
192 (format "%s date: %s" calendar-coptic-name f))))
194 (defconst calendar-ethiopic-month-name-array
195 ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
196 "Genbot" "Sane" "Hamle" "Nahas" "Paguem"]
197 "Array of the month names in the Ethiopic calendar.")
199 (defconst calendar-ethiopic-epoch 2796
200 "Absolute date of start of Ethiopic calendar = August 29, 8 C.E. (Julian).")
202 (defconst calendar-ethiopic-name "Ethiopic"
203 "Used in some message strings.")
205 (defun calendar-ethiopic-to-absolute (date)
206 "Compute absolute date from Ethiopic date DATE.
207 The absolute date is the number of days elapsed since the (imaginary)
208 Gregorian date Sunday, December 31, 1 BC."
209 (let ((calendar-coptic-epoch calendar-ethiopic-epoch))
210 (calendar-coptic-to-absolute date)))
212 (define-obsolete-function-alias 'calendar-absolute-from-ethiopic
213 'calendar-ethiopic-to-absolute "23.1")
215 (defun calendar-ethiopic-from-absolute (date)
216 "Compute the Ethiopic equivalent for absolute date DATE.
217 The result is a list of the form (MONTH DAY YEAR).
218 The absolute date is the number of days elapsed since the imaginary
219 Gregorian date Sunday, December 31, 1 BC."
220 (let ((calendar-coptic-epoch calendar-ethiopic-epoch))
221 (calendar-coptic-from-absolute date)))
223 ;;;###cal-autoload
224 (defun calendar-ethiopic-date-string (&optional date)
225 "String of Ethiopic date of Gregorian DATE.
226 Returns the empty string if DATE is pre-Ethiopic calendar.
227 Defaults to today's date if DATE is not given."
228 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
229 (calendar-coptic-name calendar-ethiopic-name)
230 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
231 (calendar-coptic-date-string date)))
233 ;;;###cal-autoload
234 (defun calendar-ethiopic-print-date ()
235 "Show the Ethiopic calendar equivalent of the selected date."
236 (interactive)
237 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
238 (calendar-coptic-name calendar-ethiopic-name)
239 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
240 (call-interactively 'calendar-coptic-print-date)))
242 (define-obsolete-function-alias 'calendar-print-ethiopic-date
243 'calendar-ethiopic-print-date "23.1")
245 ;;;###cal-autoload
246 (defun calendar-ethiopic-goto-date (date &optional noecho)
247 "Move cursor to Ethiopic date DATE.
248 Echo Ethiopic date unless NOECHO is t."
249 (interactive
250 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
251 (calendar-coptic-name calendar-ethiopic-name)
252 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
253 (calendar-coptic-read-date)))
254 (calendar-goto-date (calendar-gregorian-from-absolute
255 (calendar-ethiopic-to-absolute date)))
256 (or noecho (calendar-ethiopic-print-date)))
258 (define-obsolete-function-alias 'calendar-goto-ethiopic-date
259 'calendar-ethiopic-goto-date "23.1")
261 ;; To be called from diary-list-sexp-entries, where DATE is bound.
262 ;;;###diary-autoload
263 (defun diary-ethiopic-date ()
264 "Ethiopic calendar equivalent of date diary entry."
265 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
266 (calendar-coptic-name calendar-ethiopic-name)
267 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
268 (diary-coptic-date)))
270 (provide 'cal-coptic)
272 ;; arch-tag: 72d49161-25df-4072-9312-b182cdca7627
273 ;;; cal-coptic.el ends here