Update for calendar.el name changes.
[bpt/emacs.git] / lisp / calendar / cal-coptic.el
CommitLineData
3afbc435 1;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars
0808d911 2
cc7fff4f
GM
3;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4;; 2008 Free Software Foundation, Inc.
0808d911
ER
5
6;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
dbfca9c4 7;; Maintainer: Glenn Morris <rgm@gnu.org>
0808d911
ER
8;; Keywords: calendar
9;; Human-Keywords: Coptic calendar, Ethiopic calendar, calendar, diary
10
11;; This file is part of GNU Emacs.
12
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
075969b4 15;; the Free Software Foundation; either version 3, or (at your option)
0808d911
ER
16;; any later version.
17
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.
22
23;; You should have received a copy of the GNU General Public License
b578f267 24;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
0808d911
ER
27
28;;; Commentary:
29
b1c57079 30;; See calendar.el.
a96a5fca 31
0808d911
ER
32;;; Code:
33
dad6b4ee 34(require 'calendar)
0808d911 35
6fd6d1d8
GM
36;; Not constants because they get let-bound.
37
990121a3 38(defvar calendar-coptic-month-name-array
179bd1b3 39 ["Tut" "Babah" "Hatur" "Kiyahk" "Tubah" "Amshir" "Baramhat" "Barmundah"
ef473719
GM
40 "Bashans" "Baunah" "Abib" "Misra" "al-Nasi"]
41 "Array of the month names in the Coptic calendar.")
0808d911 42
dad6b4ee 43(eval-and-compile
5c645a20 44 (autoload 'calendar-julian-to-absolute "cal-julian"))
dad6b4ee 45
990121a3 46(defvar calendar-coptic-epoch
5c645a20 47 (eval-when-compile (calendar-julian-to-absolute '(8 29 284)))
cc7fff4f 48 "Absolute date of start of Coptic calendar = August 29, 284 AD (Julian).")
0808d911 49
990121a3 50(defvar calendar-coptic-name "Coptic"
6fd6d1d8 51 "Used in some message strings.")
0808d911 52
990121a3 53(defun calendar-coptic-leap-year-p (year)
0808d911
ER
54 "True if YEAR is a leap year on the Coptic calendar."
55 (zerop (mod (1+ year) 4)))
56
990121a3 57(defun calendar-coptic-last-day-of-month (month year)
0808d911
ER
58 "Return last day of MONTH, YEAR on the Coptic calendar.
59The 13th month is not really a month, but the 5 (6 in leap years) day period of
d3bdb2a4 60Nisi (Kebus) at the end of the year."
0808d911
ER
61 (if (< month 13)
62 30
990121a3 63 (if (calendar-coptic-leap-year-p year)
0808d911
ER
64 6
65 5)))
66
990121a3 67(defun calendar-coptic-to-absolute (date)
0808d911
ER
68 "Compute absolute date from Coptic date DATE.
69The absolute date is the number of days elapsed since the (imaginary)
70Gregorian date Sunday, December 31, 1 BC."
e803eab7
GM
71 (let ((month (calendar-extract-month date))
72 (day (calendar-extract-day date))
73 (year (calendar-extract-year date)))
990121a3 74 (+ (1- calendar-coptic-epoch) ; days before start of calendar
ef473719
GM
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
0808d911 79
990121a3
GM
80(define-obsolete-function-alias 'calendar-absolute-from-coptic
81 'calendar-coptic-to-absolute "23.1")
82
0808d911
ER
83(defun calendar-coptic-from-absolute (date)
84 "Compute the Coptic equivalent for absolute date DATE.
85The result is a list of the form (MONTH DAY YEAR).
86The absolute date is the number of days elapsed since the imaginary
87Gregorian date Sunday, December 31, 1 BC."
990121a3 88 (if (< date calendar-coptic-epoch)
d3bdb2a4 89 (list 0 0 0) ; pre-Coptic date
990121a3 90 (let* ((approx (/ (- date calendar-coptic-epoch)
ef473719
GM
91 366)) ; approximation from below
92 (year ; search forward from the approximation
0808d911
ER
93 (+ approx
94 (calendar-sum y approx
990121a3 95 (>= date (calendar-coptic-to-absolute
ef473719
GM
96 (list 1 1 (1+ y))))
97 1)))
d3bdb2a4 98 (month ; search forward from Tot
0808d911 99 (1+ (calendar-sum m 1
ef473719 100 (> date
990121a3 101 (calendar-coptic-to-absolute
ef473719 102 (list m
990121a3 103 (calendar-coptic-last-day-of-month m
ef473719
GM
104 year)
105 year)))
106 1)))
d3bdb2a4 107 (day ; calculate the day by subtraction
0808d911 108 (- date
990121a3 109 (1- (calendar-coptic-to-absolute (list month 1 year))))))
ef473719 110 (list month day year))))
0808d911 111
1d0c7fdf 112;;;###cal-autoload
0808d911
ER
113(defun calendar-coptic-date-string (&optional date)
114 "String of Coptic date of Gregorian DATE.
115Returns the empty string if DATE is pre-Coptic calendar.
116Defaults 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)))))
e803eab7
GM
120 (y (calendar-extract-year coptic-date))
121 (m (calendar-extract-month coptic-date)))
0808d911
ER
122 (if (< y 1)
123 ""
990121a3 124 (let ((monthname (aref calendar-coptic-month-name-array (1- m)))
e803eab7 125 (day (int-to-string (calendar-extract-day coptic-date)))
0808d911
ER
126 (dayname nil)
127 (month (int-to-string m))
128 (year (int-to-string y)))
129 (mapconcat 'eval calendar-date-display-form "")))))
130
1d0c7fdf 131;;;###cal-autoload
990121a3 132(defun calendar-coptic-print-date ()
0808d911
ER
133 "Show the Coptic calendar equivalent of the selected date."
134 (interactive)
135 (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t))))
136 (if (string-equal f "")
990121a3
GM
137 (message "Date is pre-%s calendar" calendar-coptic-name)
138 (message "%s date: %s" calendar-coptic-name f))))
139
140(define-obsolete-function-alias 'calendar-print-coptic-date
141 'calendar-coptic-print-date "23.1")
0808d911 142
af795681
GM
143(defun calendar-coptic-read-date ()
144 "Interactively read the arguments for a Coptic date command.
145Reads a year, month, and day."
0808d911
ER
146 (let* ((today (calendar-current-date))
147 (year (calendar-read
990121a3 148 (format "%s calendar year (>0): " calendar-coptic-name)
2c47d99f 149 (lambda (x) (> x 0))
0808d911 150 (int-to-string
e803eab7 151 (calendar-extract-year
0808d911
ER
152 (calendar-coptic-from-absolute
153 (calendar-absolute-from-gregorian today))))))
154 (completion-ignore-case t)
3be2dc28 155 (month (cdr (assoc-string
50830fad 156 (completing-read
990121a3 157 (format "%s calendar month name: " calendar-coptic-name)
50830fad 158 (mapcar 'list
990121a3 159 (append calendar-coptic-month-name-array nil))
50830fad 160 nil t)
990121a3 161 (calendar-make-alist calendar-coptic-month-name-array
3be2dc28 162 1) t)))
990121a3 163 (last (calendar-coptic-last-day-of-month month year))
0808d911 164 (day (calendar-read
990121a3 165 (format "%s calendar day (1-%d): " calendar-coptic-name last)
2c47d99f 166 (lambda (x) (and (< 0 x) (<= x last))))))
0808d911
ER
167 (list (list month day year))))
168
990121a3
GM
169(define-obsolete-function-alias 'coptic-prompt-for-date
170 'calendar-coptic-read-date "23.1")
af795681 171
ef473719 172;;;###cal-autoload
990121a3 173(defun calendar-coptic-goto-date (date &optional noecho)
ef473719
GM
174 "Move cursor to Coptic date DATE.
175Echo Coptic date unless NOECHO is t."
af795681 176 (interactive (calendar-coptic-read-date))
ef473719 177 (calendar-goto-date (calendar-gregorian-from-absolute
990121a3
GM
178 (calendar-coptic-to-absolute date)))
179 (or noecho (calendar-coptic-print-date)))
180
181(define-obsolete-function-alias 'calendar-goto-coptic-date
182 'calendar-coptic-goto-date "23.1")
ef473719 183
4692b6a0
GM
184(defvar date)
185
8c34d83e 186;; To be called from diary-list-sexp-entries, where DATE is bound.
1d0c7fdf 187;;;###diary-autoload
0808d911
ER
188(defun diary-coptic-date ()
189 "Coptic calendar equivalent of date diary entry."
7801463c 190 (let ((f (calendar-coptic-date-string date)))
0808d911 191 (if (string-equal f "")
990121a3
GM
192 (format "Date is pre-%s calendar" calendar-coptic-name)
193 (format "%s date: %s" calendar-coptic-name f))))
0808d911 194
990121a3 195(defconst calendar-ethiopic-month-name-array
179bd1b3 196 ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
ef473719
GM
197 "Genbot" "Sane" "Hamle" "Nahas" "Paguem"]
198 "Array of the month names in the Ethiopic calendar.")
0808d911 199
990121a3 200(defconst calendar-ethiopic-epoch 2796
d5824b35 201 "Absolute date of start of Ethiopic calendar = August 29, 8 C.E. (Julian).")
0808d911 202
990121a3 203(defconst calendar-ethiopic-name "Ethiopic"
ef473719 204 "Used in some message strings.")
0808d911 205
990121a3 206(defun calendar-ethiopic-to-absolute (date)
0808d911
ER
207 "Compute absolute date from Ethiopic date DATE.
208The absolute date is the number of days elapsed since the (imaginary)
209Gregorian date Sunday, December 31, 1 BC."
990121a3
GM
210 (let ((calendar-coptic-epoch calendar-ethiopic-epoch))
211 (calendar-coptic-to-absolute date)))
212
213(define-obsolete-function-alias 'calendar-absolute-from-ethiopic
214 'calendar-ethiopic-to-absolute "23.1")
0808d911
ER
215
216(defun calendar-ethiopic-from-absolute (date)
217 "Compute the Ethiopic equivalent for absolute date DATE.
218The result is a list of the form (MONTH DAY YEAR).
219The absolute date is the number of days elapsed since the imaginary
220Gregorian date Sunday, December 31, 1 BC."
990121a3 221 (let ((calendar-coptic-epoch calendar-ethiopic-epoch))
0808d911
ER
222 (calendar-coptic-from-absolute date)))
223
1d0c7fdf 224;;;###cal-autoload
0808d911
ER
225(defun calendar-ethiopic-date-string (&optional date)
226 "String of Ethiopic date of Gregorian DATE.
227Returns the empty string if DATE is pre-Ethiopic calendar.
228Defaults to today's date if DATE is not given."
990121a3
GM
229 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
230 (calendar-coptic-name calendar-ethiopic-name)
231 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
0808d911
ER
232 (calendar-coptic-date-string date)))
233
1d0c7fdf 234;;;###cal-autoload
990121a3 235(defun calendar-ethiopic-print-date ()
0808d911
ER
236 "Show the Ethiopic calendar equivalent of the selected date."
237 (interactive)
990121a3
GM
238 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
239 (calendar-coptic-name calendar-ethiopic-name)
240 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
241 (call-interactively 'calendar-coptic-print-date)))
242
243(define-obsolete-function-alias 'calendar-print-ethiopic-date
244 'calendar-ethiopic-print-date "23.1")
0808d911 245
1d0c7fdf 246;;;###cal-autoload
990121a3 247(defun calendar-ethiopic-goto-date (date &optional noecho)
0808d911
ER
248 "Move cursor to Ethiopic date DATE.
249Echo Ethiopic date unless NOECHO is t."
250 (interactive
990121a3
GM
251 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
252 (calendar-coptic-name calendar-ethiopic-name)
253 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
af795681 254 (calendar-coptic-read-date)))
0808d911 255 (calendar-goto-date (calendar-gregorian-from-absolute
990121a3
GM
256 (calendar-ethiopic-to-absolute date)))
257 (or noecho (calendar-ethiopic-print-date)))
258
259(define-obsolete-function-alias 'calendar-goto-ethiopic-date
260 'calendar-ethiopic-goto-date "23.1")
0808d911 261
8c34d83e 262;; To be called from diary-list-sexp-entries, where DATE is bound.
1d0c7fdf 263;;;###diary-autoload
0808d911
ER
264(defun diary-ethiopic-date ()
265 "Ethiopic calendar equivalent of date diary entry."
990121a3
GM
266 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
267 (calendar-coptic-name calendar-ethiopic-name)
268 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
0808d911
ER
269 (diary-coptic-date)))
270
271(provide 'cal-coptic)
272
2c47d99f 273;; arch-tag: 72d49161-25df-4072-9312-b182cdca7627
0808d911 274;;; cal-coptic.el ends here