Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / calendar / cal-persia.el
blob5c624ddcf01eb17ff4d8026c61731f408c4b1d58
1 ;;; cal-persia.el --- calendar functions for the Persian calendar
3 ;; Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Human-Keywords: Persian calendar, calendar, diary
10 ;; Package: calendar
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; See calendar.el.
31 ;;; Code:
33 (require 'calendar)
35 (defconst calendar-persian-month-name-array
36 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
37 "Azar" "Dey" "Bahman" "Esfand"]
38 "Names of the months in the Persian calendar.")
40 (eval-and-compile
41 (autoload 'calendar-julian-to-absolute "cal-julian"))
43 (defconst calendar-persian-epoch
44 (eval-when-compile (calendar-julian-to-absolute '(3 19 622)))
45 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).")
47 (defun calendar-persian-leap-year-p (year)
48 "True if YEAR is a leap year on the Persian calendar."
49 (< (mod (* (mod (mod (if (<= 0 year)
50 (+ year 2346) ; no year zero
51 (+ year 2347))
52 2820)
53 768)
54 683)
55 2820)
56 683))
58 (defun calendar-persian-last-day-of-month (month year)
59 "Return last day of MONTH, YEAR on the Persian calendar."
60 (cond
61 ((< month 7) 31)
62 ((or (< month 12) (calendar-persian-leap-year-p year)) 30)
63 (t 29)))
65 (defun calendar-persian-to-absolute (date)
66 "Compute absolute date from Persian 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 (if (< year 0)
73 (+ (calendar-persian-to-absolute
74 (list month day (1+ (mod year 2820))))
75 (* 1029983 (floor year 2820)))
76 (+ (1- calendar-persian-epoch) ; days before epoch
77 (* 365 (1- year)) ; days in prior years
78 (* 683 ; leap days in prior 2820-year cycles
79 (floor (+ year 2345) 2820))
80 (* 186 ; leap days in prior 768 year cycles
81 (floor (mod (+ year 2345) 2820) 768))
82 (floor ; leap years in current 768 or 516 year cycle
83 (* 683 (mod (mod (+ year 2345) 2820) 768))
84 2820)
85 -568 ; leap years in Persian years -2345...-1
86 (calendar-sum ; days in prior months this year
87 m 1 (< m month)
88 (calendar-persian-last-day-of-month m year))
89 day)))) ; days so far this month
91 (define-obsolete-function-alias 'calendar-absolute-from-persian
92 'calendar-persian-to-absolute "23.1")
94 (defun calendar-persian-year-from-absolute (date)
95 "Persian year corresponding to the absolute DATE."
96 (let* ((d0 ; prior days since start of 2820 cycles
97 (- date (calendar-persian-to-absolute (list 1 1 -2345))))
98 (n2820 ; completed 2820-year cycles
99 (floor d0 1029983))
100 (d1 ; prior days not in n2820
101 (mod d0 1029983))
102 (n768 ; 768-year cycles not in n2820
103 (floor d1 280506))
104 (d2 ; prior days not in n2820 or n768
105 (mod d1 280506))
106 (n1 ; years not in n2820 or n768
107 ;; Want:
108 ;; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
109 ;; but that causes overflow, so use the following.
110 ;; Use 366 as the divisor because (2820*366 mod 1029983) is small.
111 (let ((a (floor d2 366))
112 (b (mod d2 366)))
113 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983))))
114 (year (+ (* 2820 n2820) ; complete 2820 year cycles
115 (* 768 n768) ; complete 768 year cycles
116 ;; Remaining years.
117 (if (= d1 1029617) ; last day of 2820 year cycle
118 (1- n1)
120 -2345))) ; years before year 1
121 (if (< year 1)
122 (1- year) ; no year zero
123 year)))
125 (defun calendar-persian-from-absolute (date)
126 "Compute the Persian equivalent for absolute date DATE.
127 The result is a list of the form (MONTH DAY YEAR).
128 The absolute date is the number of days elapsed since the imaginary
129 Gregorian date Sunday, December 31, 1 BC."
130 (let* ((year (calendar-persian-year-from-absolute date))
131 (month ; search forward from Farvardin
132 (1+ (calendar-sum m 1
133 (> date
134 (calendar-persian-to-absolute
135 (list
137 (calendar-persian-last-day-of-month m year)
138 year)))
139 1)))
140 (day ; calculate the day by subtraction
141 (- date (1- (calendar-persian-to-absolute
142 (list month 1 year))))))
143 (list month day year)))
145 ;;;###cal-autoload
146 (defun calendar-persian-date-string (&optional date)
147 "String of Persian date of Gregorian DATE, default today."
148 (let* ((persian-date (calendar-persian-from-absolute
149 (calendar-absolute-from-gregorian
150 (or date (calendar-current-date)))))
151 (y (calendar-extract-year persian-date))
152 (m (calendar-extract-month persian-date))
153 (monthname (aref calendar-persian-month-name-array (1- m)))
154 (day (number-to-string (calendar-extract-day persian-date)))
155 (year (number-to-string y))
156 (month (number-to-string m))
157 dayname)
158 (mapconcat 'eval calendar-date-display-form "")))
160 ;;;###cal-autoload
161 (defun calendar-persian-print-date ()
162 "Show the Persian calendar equivalent of the selected date."
163 (interactive)
164 (message "Persian date: %s"
165 (calendar-persian-date-string (calendar-cursor-to-date t))))
167 (define-obsolete-function-alias 'calendar-print-persian-date
168 'calendar-persian-print-date "23.1")
170 (defun calendar-persian-read-date ()
171 "Interactively read the arguments for a Persian date command.
172 Reads a year, month, and day."
173 (let* ((year (calendar-read
174 "Persian calendar year (not 0): "
175 (lambda (x) (not (zerop x)))
176 (number-to-string
177 (calendar-extract-year
178 (calendar-persian-from-absolute
179 (calendar-absolute-from-gregorian
180 (calendar-current-date)))))))
181 (completion-ignore-case t)
182 (month (cdr (assoc
183 (completing-read
184 "Persian calendar month name: "
185 (mapcar 'list
186 (append calendar-persian-month-name-array nil))
187 nil t)
188 (calendar-make-alist calendar-persian-month-name-array
189 1))))
190 (last (calendar-persian-last-day-of-month month year))
191 (day (calendar-read
192 (format "Persian calendar day (1-%d): " last)
193 (lambda (x) (and (< 0 x) (<= x last))))))
194 (list (list month day year))))
196 (define-obsolete-function-alias 'persian-prompt-for-date
197 'calendar-persian-read-date "23.1")
199 ;;;###cal-autoload
200 (defun calendar-persian-goto-date (date &optional noecho)
201 "Move cursor to Persian date DATE.
202 Echo Persian date unless NOECHO is non-nil."
203 (interactive (calendar-persian-read-date))
204 (calendar-goto-date (calendar-gregorian-from-absolute
205 (calendar-persian-to-absolute date)))
206 (or noecho (calendar-persian-print-date)))
208 (define-obsolete-function-alias 'calendar-goto-persian-date
209 'calendar-persian-goto-date "23.1")
211 (defvar date)
213 ;; To be called from diary-list-sexp-entries, where DATE is bound.
214 ;;;###diary-autoload
215 (defun diary-persian-date ()
216 "Persian calendar equivalent of date diary entry."
217 (format "Persian date: %s" (calendar-persian-date-string date)))
219 (provide 'cal-persia)
221 ;; arch-tag: 2832383c-e4b4-4dc2-8ee9-cfbdd53e5e2d
222 ;;; cal-persia.el ends here