]> code.delx.au - gnu-emacs/blob - lisp/calendar/cal-coptic.el
(solar-longitude): Doc fix.
[gnu-emacs] / lisp / calendar / cal-coptic.el
1 ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars.
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Keywords: calendar
7 ;; Human-Keywords: Coptic calendar, Ethiopic calendar, calendar, diary
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;; This collection of functions implements the features of calendar.el and
28 ;; diary.el that deal with the Coptic and Ethiopic calendars.
29
30 ;; Comments, corrections, and improvements should be sent to
31 ;; Edward M. Reingold Department of Computer Science
32 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
33 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
34 ;; Urbana, Illinois 61801
35
36 ;;; Code:
37
38 (require 'cal-julian)
39
40 (defvar coptic-calendar-month-name-array
41 ["Tot" "Babe" "Hatur" "Kihak" "Tube" "Amshir" "Baramhat" "Barmuda"
42 "Bashnas" "Bauna" "Abib" "Misra" "Nisi"])
43
44 (defvar coptic-calendar-epoch (calendar-absolute-from-julian '(8 29 284))
45 "Absolute date of start of Coptic calendar = August 29, 284 A.D. (Julian).")
46
47 (defconst coptic-name "Coptic")
48
49 (defun coptic-calendar-leap-year-p (year)
50 "True if YEAR is a leap year on the Coptic calendar."
51 (zerop (mod (1+ year) 4)))
52
53 (defun coptic-calendar-last-day-of-month (month year)
54 "Return last day of MONTH, YEAR on the Coptic calendar.
55 The 13th month is not really a month, but the 5 (6 in leap years) day period of
56 Nisi (Kebus) at the end of the year."
57 (if (< month 13)
58 30
59 (if (coptic-calendar-leap-year-p year)
60 6
61 5)))
62
63 (defun calendar-absolute-from-coptic (date)
64 "Compute absolute date from Coptic date DATE.
65 The absolute date is the number of days elapsed since the (imaginary)
66 Gregorian date Sunday, December 31, 1 BC."
67 (let ((month (extract-calendar-month date))
68 (day (extract-calendar-day date))
69 (year (extract-calendar-year date)))
70 (+ (1- coptic-calendar-epoch);; Days before start of calendar
71 (* 365 (1- year)) ;; Days in prior years
72 (/ year 4) ;; Leap days in prior years
73 (* 30 (1- month)) ;; Days in prior months this year
74 day))) ;; Days so far this month
75
76
77 (defun calendar-coptic-from-absolute (date)
78 "Compute the Coptic equivalent for absolute date DATE.
79 The result is a list of the form (MONTH DAY YEAR).
80 The absolute date is the number of days elapsed since the imaginary
81 Gregorian date Sunday, December 31, 1 BC."
82 (if (< date coptic-calendar-epoch)
83 (list 0 0 0);; pre-Coptic date
84 (let* ((approx (/ (- date coptic-calendar-epoch)
85 366)) ;; Approximation from below.
86 (year ;; Search forward from the approximation.
87 (+ approx
88 (calendar-sum y approx
89 (>= date (calendar-absolute-from-coptic (list 1 1 (1+ y))))
90 1)))
91 (month ;; Search forward from Tot.
92 (1+ (calendar-sum m 1
93 (> date
94 (calendar-absolute-from-coptic
95 (list m
96 (coptic-calendar-last-day-of-month m year)
97 year)))
98 1)))
99 (day ;; Calculate the day by subtraction.
100 (- date
101 (1- (calendar-absolute-from-coptic (list month 1 year))))))
102 (list month day year))))
103
104 (defun calendar-coptic-date-string (&optional date)
105 "String of Coptic date of Gregorian DATE.
106 Returns the empty string if DATE is pre-Coptic calendar.
107 Defaults to today's date if DATE is not given."
108 (let* ((coptic-date (calendar-coptic-from-absolute
109 (calendar-absolute-from-gregorian
110 (or date (calendar-current-date)))))
111 (y (extract-calendar-year coptic-date))
112 (m (extract-calendar-month coptic-date)))
113 (if (< y 1)
114 ""
115 (let ((monthname (aref coptic-calendar-month-name-array (1- m)))
116 (day (int-to-string (extract-calendar-day coptic-date)))
117 (dayname nil)
118 (month (int-to-string m))
119 (year (int-to-string y)))
120 (mapconcat 'eval calendar-date-display-form "")))))
121
122 (defun calendar-print-coptic-date ()
123 "Show the Coptic calendar equivalent of the selected date."
124 (interactive)
125 (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t))))
126 (if (string-equal f "")
127 (message "Date is pre-%s calendar" coptic-name)
128 (message f))))
129
130 (defun calendar-goto-coptic-date (date &optional noecho)
131 "Move cursor to Coptic date DATE.
132 Echo Coptic date unless NOECHO is t."
133 (interactive (coptic-prompt-for-date))
134 (calendar-goto-date (calendar-gregorian-from-absolute
135 (calendar-absolute-from-coptic date)))
136 (or noecho (calendar-print-coptic-date)))
137
138 (defun coptic-prompt-for-date ()
139 "Ask for a Coptic date."
140 (let* ((today (calendar-current-date))
141 (year (calendar-read
142 (format "%s calendar year (>0): " coptic-name)
143 '(lambda (x) (> x 0))
144 (int-to-string
145 (extract-calendar-year
146 (calendar-coptic-from-absolute
147 (calendar-absolute-from-gregorian today))))))
148 (completion-ignore-case t)
149 (month (cdr (assoc
150 (capitalize
151 (completing-read
152 (format "%s calendar month name: " coptic-name)
153 (mapcar 'list
154 (append coptic-calendar-month-name-array nil))
155 nil t))
156 (calendar-make-alist coptic-calendar-month-name-array
157 1 'capitalize))))
158 (last (coptic-calendar-last-day-of-month month year))
159 (day (calendar-read
160 (format "%s calendar day (1-%d): " coptic-name last)
161 '(lambda (x) (and (< 0 x) (<= x last))))))
162 (list (list month day year))))
163
164 (defun diary-coptic-date ()
165 "Coptic calendar equivalent of date diary entry."
166 (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t))))
167 (if (string-equal f "")
168 (format "Date is pre-%s calendar" coptic-name)
169 f)))
170
171 (defconst ethiopic-calendar-month-name-array
172 ["Maskarram" "Tekemt" "Hadar" "Tahsas" "Tarr" "Yekatit" "Magawit" "Miaziah"
173 "Genbot" "Sanni" "Hamle" "Nas'hi" "Pagnem"])
174
175 (defconst ethiopic-calendar-epoch -2006079
176 "Absolute date of start of Ethiopic calendar = August 29, 5493 B.C.E. (Julian).")
177
178 (defconst ethiopic-name "Ethiopic")
179
180 (defun calendar-absolute-from-ethiopic (date)
181 "Compute absolute date from Ethiopic date DATE.
182 The absolute date is the number of days elapsed since the (imaginary)
183 Gregorian date Sunday, December 31, 1 BC."
184 (let ((coptic-calendar-epoch ethiopic-calendar-epoch))
185 (calendar-absolute-from-coptic date)))
186
187 (defun calendar-ethiopic-from-absolute (date)
188 "Compute the Ethiopic equivalent for absolute date DATE.
189 The result is a list of the form (MONTH DAY YEAR).
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-coptic-from-absolute date)))
194
195 (defun calendar-ethiopic-date-string (&optional date)
196 "String of Ethiopic date of Gregorian DATE.
197 Returns the empty string if DATE is pre-Ethiopic calendar.
198 Defaults to today's date if DATE is not given."
199 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
200 (coptic-name ethiopic-name)
201 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
202 (calendar-coptic-date-string date)))
203
204 (defun calendar-print-ethiopic-date ()
205 "Show the Ethiopic calendar equivalent of the selected date."
206 (interactive)
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 (call-interactively 'calendar-print-coptic-date)))
211
212 (defun calendar-goto-ethiopic-date (date &optional noecho)
213 "Move cursor to Ethiopic date DATE.
214 Echo Ethiopic date unless NOECHO is t."
215 (interactive
216 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
217 (coptic-name ethiopic-name)
218 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
219 (coptic-prompt-for-date)))
220 (calendar-goto-date (calendar-gregorian-from-absolute
221 (calendar-absolute-from-ethiopic date)))
222 (or noecho (calendar-print-ethiopic-date)))
223
224 (defun diary-ethiopic-date ()
225 "Ethiopic calendar equivalent of date diary entry."
226 (let ((coptic-calendar-epoch ethiopic-calendar-epoch)
227 (coptic-name ethiopic-name)
228 (coptic-calendar-month-name-array ethiopic-calendar-month-name-array))
229 (diary-coptic-date)))
230
231 (provide 'cal-coptic)
232
233 ;;; cal-coptic.el ends here