1 ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars
3 ;; Copyright (C) 1995, 1997, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; 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 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
34 ;; Not constants because they get let-bound.
36 (defvar calendar-coptic-month-name-array
37 ["Tut" "Babah" "Hatur" "Kiyahk" "Tubah" "Amshir" "Baramhat" "Barmundah"
38 "Bashans" "Baunah" "Abib" "Misra" "al-Nasi"]
39 "Array of the month names in the Coptic calendar.")
42 (autoload 'calendar-julian-to-absolute
"cal-julian"))
44 (defvar calendar-coptic-epoch
45 (eval-when-compile (calendar-julian-to-absolute '(8 29 284)))
46 "Absolute date of start of Coptic calendar = August 29, 284 AD (Julian).")
48 (defvar calendar-coptic-name
"Coptic"
49 "Used in some message strings.")
51 (defun calendar-coptic-leap-year-p (year)
52 "True if YEAR is a leap year on the Coptic calendar."
53 (zerop (mod (1+ year
) 4)))
55 (defun calendar-coptic-last-day-of-month (month year
)
56 "Return last day of MONTH, YEAR on the Coptic calendar.
57 The 13th month is not really a month, but the 5 (6 in leap years) day period of
58 Nisi (Kebus) at the end of the year."
61 (if (calendar-coptic-leap-year-p year
)
65 (defun calendar-coptic-to-absolute (date)
66 "Compute absolute date from Coptic date DATE.
67 The absolute date is the number of days elapsed since the (imaginary)
68 Gregorian date Sunday, December 31, 1 BC."
69 (let ((month (calendar-extract-month date
))
70 (day (calendar-extract-day date
))
71 (year (calendar-extract-year date
)))
72 (+ (1- calendar-coptic-epoch
) ; days before start of calendar
73 (* 365 (1- year
)) ; days in prior years
74 (/ year
4) ; leap days in prior years
75 (* 30 (1- month
)) ; days in prior months this year
76 day
))) ; days so far this month
78 (define-obsolete-function-alias 'calendar-absolute-from-coptic
79 'calendar-coptic-to-absolute
"23.1")
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 calendar-coptic-epoch
)
87 (list 0 0 0) ; pre-Coptic date
88 (let* ((approx (/ (- date calendar-coptic-epoch
)
89 366)) ; approximation from below
90 (year ; search forward from the approximation
92 (calendar-sum y approx
93 (>= date
(calendar-coptic-to-absolute
96 (month ; search forward from Tot
99 (calendar-coptic-to-absolute
101 (calendar-coptic-last-day-of-month m
105 (day ; calculate the day by subtraction
107 (1- (calendar-coptic-to-absolute (list month
1 year
))))))
108 (list month day year
))))
111 (defun calendar-coptic-date-string (&optional date
)
112 "String of Coptic date of Gregorian DATE.
113 Returns the empty string if DATE is pre-Coptic calendar.
114 Defaults to today's date if DATE is not given."
115 (let* ((coptic-date (calendar-coptic-from-absolute
116 (calendar-absolute-from-gregorian
117 (or date
(calendar-current-date)))))
118 (y (calendar-extract-year coptic-date
))
119 (m (calendar-extract-month coptic-date
)))
122 (let ((monthname (aref calendar-coptic-month-name-array
(1- m
)))
123 (day (number-to-string (calendar-extract-day coptic-date
)))
125 (month (number-to-string m
))
126 (year (number-to-string y
)))
127 (mapconcat 'eval calendar-date-display-form
"")))))
130 (defun calendar-coptic-print-date ()
131 "Show the Coptic calendar equivalent of the selected date."
133 (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t
))))
134 (if (string-equal f
"")
135 (message "Date is pre-%s calendar" calendar-coptic-name
)
136 (message "%s date: %s" calendar-coptic-name f
))))
138 (define-obsolete-function-alias 'calendar-print-coptic-date
139 'calendar-coptic-print-date
"23.1")
141 (defun calendar-coptic-read-date ()
142 "Interactively read the arguments for a Coptic date command.
143 Reads a year, month, and day."
144 (let* ((today (calendar-current-date))
146 (format "%s calendar year (>0): " calendar-coptic-name
)
149 (calendar-extract-year
150 (calendar-coptic-from-absolute
151 (calendar-absolute-from-gregorian today
))))))
152 (completion-ignore-case t
)
153 (month (cdr (assoc-string
155 (format "%s calendar month name: " calendar-coptic-name
)
157 (append calendar-coptic-month-name-array nil
))
159 (calendar-make-alist calendar-coptic-month-name-array
161 (last (calendar-coptic-last-day-of-month month year
))
163 (format "%s calendar day (1-%d): " calendar-coptic-name last
)
164 (lambda (x) (and (< 0 x
) (<= x last
))))))
165 (list (list month day year
))))
167 (define-obsolete-function-alias 'coptic-prompt-for-date
168 'calendar-coptic-read-date
"23.1")
171 (defun calendar-coptic-goto-date (date &optional noecho
)
172 "Move cursor to Coptic date DATE.
173 Echo Coptic date unless NOECHO is t."
174 (interactive (calendar-coptic-read-date))
175 (calendar-goto-date (calendar-gregorian-from-absolute
176 (calendar-coptic-to-absolute date
)))
177 (or noecho
(calendar-coptic-print-date)))
179 (define-obsolete-function-alias 'calendar-goto-coptic-date
180 'calendar-coptic-goto-date
"23.1")
184 ;; To be called from diary-list-sexp-entries, where DATE is bound.
186 (defun diary-coptic-date ()
187 "Coptic calendar equivalent of date diary entry."
188 (let ((f (calendar-coptic-date-string date
)))
189 (if (string-equal f
"")
190 (format "Date is pre-%s calendar" calendar-coptic-name
)
191 (format "%s date: %s" calendar-coptic-name f
))))
193 (defconst calendar-ethiopic-month-name-array
194 ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
195 "Genbot" "Sane" "Hamle" "Nahas" "Paguem"]
196 "Array of the month names in the Ethiopic calendar.")
198 (defconst calendar-ethiopic-epoch
2796
199 "Absolute date of start of Ethiopic calendar = August 29, 8 C.E. (Julian).")
201 (defconst calendar-ethiopic-name
"Ethiopic"
202 "Used in some message strings.")
204 (defun calendar-ethiopic-to-absolute (date)
205 "Compute absolute date from Ethiopic date DATE.
206 The absolute date is the number of days elapsed since the (imaginary)
207 Gregorian date Sunday, December 31, 1 BC."
208 (let ((calendar-coptic-epoch calendar-ethiopic-epoch
))
209 (calendar-coptic-to-absolute date
)))
211 (define-obsolete-function-alias 'calendar-absolute-from-ethiopic
212 'calendar-ethiopic-to-absolute
"23.1")
214 (defun calendar-ethiopic-from-absolute (date)
215 "Compute the Ethiopic equivalent for absolute date DATE.
216 The result is a list of the form (MONTH DAY YEAR).
217 The absolute date is the number of days elapsed since the imaginary
218 Gregorian date Sunday, December 31, 1 BC."
219 (let ((calendar-coptic-epoch calendar-ethiopic-epoch
))
220 (calendar-coptic-from-absolute date
)))
223 (defun calendar-ethiopic-date-string (&optional date
)
224 "String of Ethiopic date of Gregorian DATE.
225 Returns the empty string if DATE is pre-Ethiopic calendar.
226 Defaults to today's date if DATE is not given."
227 (let ((calendar-coptic-epoch calendar-ethiopic-epoch
)
228 (calendar-coptic-name calendar-ethiopic-name
)
229 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array
))
230 (calendar-coptic-date-string date
)))
233 (defun calendar-ethiopic-print-date ()
234 "Show the Ethiopic calendar equivalent of the selected date."
236 (let ((calendar-coptic-epoch calendar-ethiopic-epoch
)
237 (calendar-coptic-name calendar-ethiopic-name
)
238 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array
))
239 (call-interactively 'calendar-coptic-print-date
)))
241 (define-obsolete-function-alias 'calendar-print-ethiopic-date
242 'calendar-ethiopic-print-date
"23.1")
245 (defun calendar-ethiopic-goto-date (date &optional noecho
)
246 "Move cursor to Ethiopic date DATE.
247 Echo Ethiopic date unless NOECHO is t."
249 (let ((calendar-coptic-epoch calendar-ethiopic-epoch
)
250 (calendar-coptic-name calendar-ethiopic-name
)
251 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array
))
252 (calendar-coptic-read-date)))
253 (calendar-goto-date (calendar-gregorian-from-absolute
254 (calendar-ethiopic-to-absolute date
)))
255 (or noecho
(calendar-ethiopic-print-date)))
257 (define-obsolete-function-alias 'calendar-goto-ethiopic-date
258 'calendar-ethiopic-goto-date
"23.1")
260 ;; To be called from diary-list-sexp-entries, where DATE is bound.
262 (defun diary-ethiopic-date ()
263 "Ethiopic calendar equivalent of date diary entry."
264 (let ((calendar-coptic-epoch calendar-ethiopic-epoch
)
265 (calendar-coptic-name calendar-ethiopic-name
)
266 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array
))
267 (diary-coptic-date)))
269 (provide 'cal-coptic
)
271 ;;; cal-coptic.el ends here