eww.el: If given a non-domain text, search for the term
[emacs.git] / lisp / calendar / cal-persia.el
blob9a0fb77e7e11a7542ca3aef41cc9f87c04169adc
1 ;;; cal-persia.el --- calendar functions for the Persian calendar
3 ;; Copyright (C) 1996-1997, 2001-2013 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: Persian 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 (defconst calendar-persian-month-name-array
35 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
36 "Azar" "Dey" "Bahman" "Esfand"]
37 "Names of the months in the Persian calendar.")
39 (eval-and-compile
40 (autoload 'calendar-julian-to-absolute "cal-julian"))
42 (defconst calendar-persian-epoch
43 (eval-when-compile (calendar-julian-to-absolute '(3 19 622)))
44 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).")
46 (defun calendar-persian-leap-year-p (year)
47 "True if YEAR is a leap year on the Persian calendar."
48 (< (mod (* (mod (mod (if (<= 0 year)
49 (+ year 2346) ; no year zero
50 (+ year 2347))
51 2820)
52 768)
53 683)
54 2820)
55 683))
57 (defun calendar-persian-last-day-of-month (month year)
58 "Return last day of MONTH, YEAR on the Persian calendar."
59 (cond
60 ((< month 7) 31)
61 ((or (< month 12) (calendar-persian-leap-year-p year)) 30)
62 (t 29)))
64 (defun calendar-persian-to-absolute (date)
65 "Compute absolute date from Persian date DATE.
66 The absolute date is the number of days elapsed since the (imaginary)
67 Gregorian date Sunday, December 31, 1 BC."
68 (let ((month (calendar-extract-month date))
69 (day (calendar-extract-day date))
70 (year (calendar-extract-year date)))
71 (if (< year 0)
72 (+ (calendar-persian-to-absolute
73 (list month day (1+ (mod year 2820))))
74 (* 1029983 (floor year 2820)))
75 (+ (1- calendar-persian-epoch) ; days before epoch
76 (* 365 (1- year)) ; days in prior years
77 (* 683 ; leap days in prior 2820-year cycles
78 (floor (+ year 2345) 2820))
79 (* 186 ; leap days in prior 768 year cycles
80 (floor (mod (+ year 2345) 2820) 768))
81 (floor ; leap years in current 768 or 516 year cycle
82 (* 683 (mod (mod (+ year 2345) 2820) 768))
83 2820)
84 -568 ; leap years in Persian years -2345...-1
85 (calendar-sum ; days in prior months this year
86 m 1 (< m month)
87 (calendar-persian-last-day-of-month m year))
88 day)))) ; days so far this month
90 (define-obsolete-function-alias 'calendar-absolute-from-persian
91 'calendar-persian-to-absolute "23.1")
93 (defun calendar-persian-year-from-absolute (date)
94 "Persian year corresponding to the absolute DATE."
95 (let* ((d0 ; prior days since start of 2820 cycles
96 (- date (calendar-persian-to-absolute (list 1 1 -2345))))
97 (n2820 ; completed 2820-year cycles
98 (floor d0 1029983))
99 (d1 ; prior days not in n2820
100 (mod d0 1029983))
101 (n768 ; 768-year cycles not in n2820
102 (floor d1 280506))
103 (d2 ; prior days not in n2820 or n768
104 (mod d1 280506))
105 (n1 ; years not in n2820 or n768
106 ;; Want:
107 ;; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
108 ;; but that causes overflow, so use the following.
109 ;; Use 366 as the divisor because (2820*366 mod 1029983) is small.
110 (let ((a (floor d2 366))
111 (b (mod d2 366)))
112 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983))))
113 (year (+ (* 2820 n2820) ; complete 2820 year cycles
114 (* 768 n768) ; complete 768 year cycles
115 ;; Remaining years.
116 (if (= d1 1029617) ; last day of 2820 year cycle
117 (1- n1)
119 -2345))) ; years before year 1
120 (if (< year 1)
121 (1- year) ; no year zero
122 year)))
124 (defun calendar-persian-from-absolute (date)
125 "Compute the Persian equivalent for absolute date DATE.
126 The result is a list of the form (MONTH DAY YEAR).
127 The absolute date is the number of days elapsed since the imaginary
128 Gregorian date Sunday, December 31, 1 BC."
129 (let* ((year (calendar-persian-year-from-absolute date))
130 (month ; search forward from Farvardin
131 (1+ (calendar-sum m 1
132 (> date
133 (calendar-persian-to-absolute
134 (list
136 (calendar-persian-last-day-of-month m year)
137 year)))
138 1)))
139 (day ; calculate the day by subtraction
140 (- date (1- (calendar-persian-to-absolute
141 (list month 1 year))))))
142 (list month day year)))
144 ;;;###cal-autoload
145 (defun calendar-persian-date-string (&optional date)
146 "String of Persian date of Gregorian DATE, default today."
147 (let* ((persian-date (calendar-persian-from-absolute
148 (calendar-absolute-from-gregorian
149 (or date (calendar-current-date)))))
150 (y (calendar-extract-year persian-date))
151 (m (calendar-extract-month persian-date))
152 (monthname (aref calendar-persian-month-name-array (1- m)))
153 (day (number-to-string (calendar-extract-day persian-date)))
154 (year (number-to-string y))
155 (month (number-to-string m))
156 dayname)
157 (mapconcat 'eval calendar-date-display-form "")))
159 ;;;###cal-autoload
160 (defun calendar-persian-print-date ()
161 "Show the Persian calendar equivalent of the selected date."
162 (interactive)
163 (message "Persian date: %s"
164 (calendar-persian-date-string (calendar-cursor-to-date t))))
166 (define-obsolete-function-alias 'calendar-print-persian-date
167 'calendar-persian-print-date "23.1")
169 (defun calendar-persian-read-date ()
170 "Interactively read the arguments for a Persian date command.
171 Reads a year, month, and day."
172 (let* ((year (calendar-read
173 "Persian calendar year (not 0): "
174 (lambda (x) (not (zerop x)))
175 (number-to-string
176 (calendar-extract-year
177 (calendar-persian-from-absolute
178 (calendar-absolute-from-gregorian
179 (calendar-current-date)))))))
180 (completion-ignore-case t)
181 (month (cdr (assoc
182 (completing-read
183 "Persian calendar month name: "
184 (mapcar 'list
185 (append calendar-persian-month-name-array nil))
186 nil t)
187 (calendar-make-alist calendar-persian-month-name-array
188 1))))
189 (last (calendar-persian-last-day-of-month month year))
190 (day (calendar-read
191 (format "Persian calendar day (1-%d): " last)
192 (lambda (x) (and (< 0 x) (<= x last))))))
193 (list (list month day year))))
195 (define-obsolete-function-alias 'persian-prompt-for-date
196 'calendar-persian-read-date "23.1")
198 ;;;###cal-autoload
199 (defun calendar-persian-goto-date (date &optional noecho)
200 "Move cursor to Persian date DATE.
201 Echo Persian date unless NOECHO is non-nil."
202 (interactive (calendar-persian-read-date))
203 (calendar-goto-date (calendar-gregorian-from-absolute
204 (calendar-persian-to-absolute date)))
205 (or noecho (calendar-persian-print-date)))
207 (define-obsolete-function-alias 'calendar-goto-persian-date
208 'calendar-persian-goto-date "23.1")
210 (defvar date)
212 ;; To be called from diary-list-sexp-entries, where DATE is bound.
213 ;;;###diary-autoload
214 (defun diary-persian-date ()
215 "Persian calendar equivalent of date diary entry."
216 (format "Persian date: %s" (calendar-persian-date-string date)))
218 (provide 'cal-persia)
220 ;;; cal-persia.el ends here