Fix Bug#20637. Do not merge to master
[emacs.git] / lisp / calendar / cal-coptic.el
blob4a55d938beb6422f394634f3ad78b3a386fed960
1 ;;; cal-coptic.el --- calendar functions for the Coptic/Ethiopic calendars
3 ;; Copyright (C) 1995, 1997, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
7 ;; Keywords: calendar
8 ;; Human-Keywords: Coptic calendar, Ethiopic calendar, calendar, diary
9 ;; Package: calendar
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/>.
26 ;;; Commentary:
28 ;; See calendar.el.
30 ;;; Code:
32 (require 'calendar)
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.")
41 (eval-and-compile
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."
59 (if (< month 13)
61 (if (calendar-coptic-leap-year-p year)
63 5)))
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 (defun calendar-coptic-from-absolute (date)
79 "Compute the Coptic equivalent for absolute date DATE.
80 The result is a list of the form (MONTH DAY YEAR).
81 The absolute date is the number of days elapsed since the imaginary
82 Gregorian date Sunday, December 31, 1 BC."
83 (if (< date calendar-coptic-epoch)
84 (list 0 0 0) ; pre-Coptic date
85 (let* ((approx (/ (- date calendar-coptic-epoch)
86 366)) ; approximation from below
87 (year ; search forward from the approximation
88 (+ approx
89 (calendar-sum y approx
90 (>= date (calendar-coptic-to-absolute
91 (list 1 1 (1+ y))))
92 1)))
93 (month ; search forward from Tot
94 (1+ (calendar-sum m 1
95 (> date
96 (calendar-coptic-to-absolute
97 (list m
98 (calendar-coptic-last-day-of-month m
99 year)
100 year)))
101 1)))
102 (day ; calculate the day by subtraction
103 (- date
104 (1- (calendar-coptic-to-absolute (list month 1 year))))))
105 (list month day year))))
107 ;;;###cal-autoload
108 (defun calendar-coptic-date-string (&optional date)
109 "String of Coptic date of Gregorian DATE.
110 Returns the empty string if DATE is pre-Coptic calendar.
111 Defaults to today's date if DATE is not given."
112 (let* ((coptic-date (calendar-coptic-from-absolute
113 (calendar-absolute-from-gregorian
114 (or date (calendar-current-date)))))
115 (y (calendar-extract-year coptic-date))
116 (m (calendar-extract-month coptic-date)))
117 (if (< y 1)
119 (let ((monthname (aref calendar-coptic-month-name-array (1- m)))
120 (day (number-to-string (calendar-extract-day coptic-date)))
121 (dayname nil)
122 (month (number-to-string m))
123 (year (number-to-string y)))
124 (mapconcat 'eval calendar-date-display-form "")))))
126 ;;;###cal-autoload
127 (defun calendar-coptic-print-date ()
128 "Show the Coptic calendar equivalent of the selected date."
129 (interactive)
130 (let ((f (calendar-coptic-date-string (calendar-cursor-to-date t))))
131 (if (string-equal f "")
132 (message "Date is pre-%s calendar" calendar-coptic-name)
133 (message "%s date: %s" calendar-coptic-name f))))
135 (defun calendar-coptic-read-date ()
136 "Interactively read the arguments for a Coptic date command.
137 Reads a year, month, and day."
138 (let* ((today (calendar-current-date))
139 (year (calendar-read
140 (format "%s calendar year (>0): " calendar-coptic-name)
141 (lambda (x) (> x 0))
142 (number-to-string
143 (calendar-extract-year
144 (calendar-coptic-from-absolute
145 (calendar-absolute-from-gregorian today))))))
146 (completion-ignore-case t)
147 (month (cdr (assoc-string
148 (completing-read
149 (format "%s calendar month name: " calendar-coptic-name)
150 (mapcar 'list
151 (append calendar-coptic-month-name-array nil))
152 nil t)
153 (calendar-make-alist calendar-coptic-month-name-array
154 1) t)))
155 (last (calendar-coptic-last-day-of-month month year))
156 (day (calendar-read
157 (format "%s calendar day (1-%d): " calendar-coptic-name last)
158 (lambda (x) (and (< 0 x) (<= x last))))))
159 (list (list month day year))))
161 ;;;###cal-autoload
162 (defun calendar-coptic-goto-date (date &optional noecho)
163 "Move cursor to Coptic date DATE.
164 Echo Coptic date unless NOECHO is t."
165 (interactive (calendar-coptic-read-date))
166 (calendar-goto-date (calendar-gregorian-from-absolute
167 (calendar-coptic-to-absolute date)))
168 (or noecho (calendar-coptic-print-date)))
171 (defvar date)
173 ;; To be called from diary-list-sexp-entries, where DATE is bound.
174 ;;;###diary-autoload
175 (defun diary-coptic-date ()
176 "Coptic calendar equivalent of date diary entry."
177 (let ((f (calendar-coptic-date-string date)))
178 (if (string-equal f "")
179 (format "Date is pre-%s calendar" calendar-coptic-name)
180 (format "%s date: %s" calendar-coptic-name f))))
182 (defconst calendar-ethiopic-month-name-array
183 ["Maskaram" "Teqemt" "Khedar" "Takhsas" "Ter" "Yakatit" "Magabit" "Miyazya"
184 "Genbot" "Sane" "Hamle" "Nahas" "Paguem"]
185 "Array of the month names in the Ethiopic calendar.")
187 (defconst calendar-ethiopic-epoch 2796
188 "Absolute date of start of Ethiopic calendar = August 29, 8 C.E. (Julian).")
190 (defconst calendar-ethiopic-name "Ethiopic"
191 "Used in some message strings.")
193 (defun calendar-ethiopic-to-absolute (date)
194 "Compute absolute date from Ethiopic date DATE.
195 The absolute date is the number of days elapsed since the (imaginary)
196 Gregorian date Sunday, December 31, 1 BC."
197 (let ((calendar-coptic-epoch calendar-ethiopic-epoch))
198 (calendar-coptic-to-absolute date)))
200 (defun calendar-ethiopic-from-absolute (date)
201 "Compute the Ethiopic equivalent for absolute date DATE.
202 The result is a list of the form (MONTH DAY YEAR).
203 The absolute date is the number of days elapsed since the imaginary
204 Gregorian date Sunday, December 31, 1 BC."
205 (let ((calendar-coptic-epoch calendar-ethiopic-epoch))
206 (calendar-coptic-from-absolute date)))
208 ;;;###cal-autoload
209 (defun calendar-ethiopic-date-string (&optional date)
210 "String of Ethiopic date of Gregorian DATE.
211 Returns the empty string if DATE is pre-Ethiopic calendar.
212 Defaults to today's date if DATE is not given."
213 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
214 (calendar-coptic-name calendar-ethiopic-name)
215 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
216 (calendar-coptic-date-string date)))
218 ;;;###cal-autoload
219 (defun calendar-ethiopic-print-date ()
220 "Show the Ethiopic calendar equivalent of the selected date."
221 (interactive)
222 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
223 (calendar-coptic-name calendar-ethiopic-name)
224 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
225 (call-interactively 'calendar-coptic-print-date)))
227 ;;;###cal-autoload
228 (defun calendar-ethiopic-goto-date (date &optional noecho)
229 "Move cursor to Ethiopic date DATE.
230 Echo Ethiopic date unless NOECHO is t."
231 (interactive
232 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
233 (calendar-coptic-name calendar-ethiopic-name)
234 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
235 (calendar-coptic-read-date)))
236 (calendar-goto-date (calendar-gregorian-from-absolute
237 (calendar-ethiopic-to-absolute date)))
238 (or noecho (calendar-ethiopic-print-date)))
240 ;; To be called from diary-list-sexp-entries, where DATE is bound.
241 ;;;###diary-autoload
242 (defun diary-ethiopic-date ()
243 "Ethiopic calendar equivalent of date diary entry."
244 (let ((calendar-coptic-epoch calendar-ethiopic-epoch)
245 (calendar-coptic-name calendar-ethiopic-name)
246 (calendar-coptic-month-name-array calendar-ethiopic-month-name-array))
247 (diary-coptic-date)))
249 (provide 'cal-coptic)
251 ;;; cal-coptic.el ends here