Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / calendar / cal-persia.el
CommitLineData
3afbc435 1;;; cal-persia.el --- calendar functions for the Persian calendar
0e41f190 2
acaf905b 3;; Copyright (C) 1996-1997, 2001-2012 Free Software Foundation, Inc.
0e41f190
ER
4
5;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
dbfca9c4 6;; Maintainer: Glenn Morris <rgm@gnu.org>
0e41f190
ER
7;; Keywords: calendar
8;; Human-Keywords: Persian calendar, calendar, diary
bd78fa1d 9;; Package: calendar
0e41f190
ER
10
11;; This file is part of GNU Emacs.
12
2ed66575 13;; GNU Emacs is free software: you can redistribute it and/or modify
0e41f190 14;; it under the terms of the GNU General Public License as published by
2ed66575
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
0e41f190
ER
17
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.
22
23;; You should have received a copy of the GNU General Public License
2ed66575 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
0e41f190
ER
25
26;;; Commentary:
27
e5e99776 28;; See calendar.el.
0e41f190 29
0e41f190
ER
30;;; Code:
31
73f7eb59 32(require 'calendar)
0e41f190 33
fdc22b38 34(defconst calendar-persian-month-name-array
0e41f190 35 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
d01890ee
GM
36 "Azar" "Dey" "Bahman" "Esfand"]
37 "Names of the months in the Persian calendar.")
0e41f190 38
73f7eb59 39(eval-and-compile
fdc22b38 40 (autoload 'calendar-julian-to-absolute "cal-julian"))
73f7eb59 41
fdc22b38
GM
42(defconst calendar-persian-epoch
43 (eval-when-compile (calendar-julian-to-absolute '(3 19 622)))
d01890ee 44 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).")
0e41f190 45
fdc22b38 46(defun calendar-persian-leap-year-p (year)
0e41f190
ER
47 "True if YEAR is a leap year on the Persian calendar."
48 (< (mod (* (mod (mod (if (<= 0 year)
b0b671db 49 (+ year 2346) ; no year zero
0e41f190
ER
50 (+ year 2347))
51 2820)
52 768)
b0b671db
GM
53 683)
54 2820)
55 683))
0e41f190 56
fdc22b38 57(defun calendar-persian-last-day-of-month (month year)
0e41f190
ER
58 "Return last day of MONTH, YEAR on the Persian calendar."
59 (cond
60 ((< month 7) 31)
fdc22b38 61 ((or (< month 12) (calendar-persian-leap-year-p year)) 30)
0e41f190
ER
62 (t 29)))
63
fdc22b38 64(defun calendar-persian-to-absolute (date)
0e41f190
ER
65 "Compute absolute date from Persian date DATE.
66The absolute date is the number of days elapsed since the (imaginary)
67Gregorian date Sunday, December 31, 1 BC."
e803eab7
GM
68 (let ((month (calendar-extract-month date))
69 (day (calendar-extract-day date))
70 (year (calendar-extract-year date)))
0e41f190 71 (if (< year 0)
fdc22b38 72 (+ (calendar-persian-to-absolute
0e41f190
ER
73 (list month day (1+ (mod year 2820))))
74 (* 1029983 (floor year 2820)))
fdc22b38 75 (+ (1- calendar-persian-epoch) ; days before epoch
d01890ee
GM
76 (* 365 (1- year)) ; days in prior years
77 (* 683 ; leap days in prior 2820-year cycles
0e41f190 78 (floor (+ year 2345) 2820))
d01890ee 79 (* 186 ; leap days in prior 768 year cycles
0e41f190 80 (floor (mod (+ year 2345) 2820) 768))
d01890ee 81 (floor ; leap years in current 768 or 516 year cycle
0e41f190
ER
82 (* 683 (mod (mod (+ year 2345) 2820) 768))
83 2820)
d01890ee
GM
84 -568 ; leap years in Persian years -2345...-1
85 (calendar-sum ; days in prior months this year
0e41f190 86 m 1 (< m month)
fdc22b38 87 (calendar-persian-last-day-of-month m year))
d01890ee 88 day)))) ; days so far this month
0e41f190 89
fdc22b38
GM
90(define-obsolete-function-alias 'calendar-absolute-from-persian
91 'calendar-persian-to-absolute "23.1")
92
0e41f190
ER
93(defun calendar-persian-year-from-absolute (date)
94 "Persian year corresponding to the absolute DATE."
d01890ee 95 (let* ((d0 ; prior days since start of 2820 cycles
fdc22b38 96 (- date (calendar-persian-to-absolute (list 1 1 -2345))))
d01890ee 97 (n2820 ; completed 2820-year cycles
0e41f190 98 (floor d0 1029983))
d01890ee 99 (d1 ; prior days not in n2820
0e41f190 100 (mod d0 1029983))
d01890ee 101 (n768 ; 768-year cycles not in n2820
0e41f190 102 (floor d1 280506))
d01890ee 103 (d2 ; prior days not in n2820 or n768
0e41f190 104 (mod d1 280506))
52e0f59e 105 (n1 ; years not in n2820 or n768
d01890ee
GM
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))
0e41f190
ER
111 (b (mod d2 366)))
112 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983))))
d01890ee
GM
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
0e41f190
ER
117 (1- n1)
118 n1)
d01890ee 119 -2345))) ; years before year 1
0e41f190 120 (if (< year 1)
d01890ee 121 (1- year) ; no year zero
0e41f190
ER
122 year)))
123
124(defun calendar-persian-from-absolute (date)
125 "Compute the Persian equivalent for absolute date DATE.
126The result is a list of the form (MONTH DAY YEAR).
127The absolute date is the number of days elapsed since the imaginary
128Gregorian date Sunday, December 31, 1 BC."
129 (let* ((year (calendar-persian-year-from-absolute date))
d01890ee 130 (month ; search forward from Farvardin
0e41f190
ER
131 (1+ (calendar-sum m 1
132 (> date
fdc22b38 133 (calendar-persian-to-absolute
0e41f190
ER
134 (list
135 m
fdc22b38 136 (calendar-persian-last-day-of-month m year)
0e41f190
ER
137 year)))
138 1)))
d01890ee 139 (day ; calculate the day by subtraction
fdc22b38 140 (- date (1- (calendar-persian-to-absolute
0e41f190
ER
141 (list month 1 year))))))
142 (list month day year)))
143
6c1841ba 144;;;###cal-autoload
0e41f190 145(defun calendar-persian-date-string (&optional date)
d01890ee 146 "String of Persian date of Gregorian DATE, default today."
0e41f190 147 (let* ((persian-date (calendar-persian-from-absolute
52e0f59e
GM
148 (calendar-absolute-from-gregorian
149 (or date (calendar-current-date)))))
e803eab7
GM
150 (y (calendar-extract-year persian-date))
151 (m (calendar-extract-month persian-date))
fdc22b38 152 (monthname (aref calendar-persian-month-name-array (1- m)))
d92bcf94
GM
153 (day (number-to-string (calendar-extract-day persian-date)))
154 (year (number-to-string y))
155 (month (number-to-string m))
e5e99776
GM
156 dayname)
157 (mapconcat 'eval calendar-date-display-form "")))
0e41f190 158
6c1841ba 159;;;###cal-autoload
fdc22b38 160(defun calendar-persian-print-date ()
0e41f190
ER
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))))
165
fdc22b38
GM
166(define-obsolete-function-alias 'calendar-print-persian-date
167 'calendar-persian-print-date "23.1")
168
0075b4a2
GM
169(defun calendar-persian-read-date ()
170 "Interactively read the arguments for a Persian date command.
171Reads a year, month, and day."
d01890ee 172 (let* ((year (calendar-read
0e41f190 173 "Persian calendar year (not 0): "
b0b671db 174 (lambda (x) (not (zerop x)))
d92bcf94 175 (number-to-string
e803eab7 176 (calendar-extract-year
0e41f190 177 (calendar-persian-from-absolute
d01890ee
GM
178 (calendar-absolute-from-gregorian
179 (calendar-current-date)))))))
0e41f190
ER
180 (completion-ignore-case t)
181 (month (cdr (assoc
52e0f59e
GM
182 (completing-read
183 "Persian calendar month name: "
184 (mapcar 'list
fdc22b38 185 (append calendar-persian-month-name-array nil))
52e0f59e 186 nil t)
fdc22b38 187 (calendar-make-alist calendar-persian-month-name-array
c0982a97 188 1))))
fdc22b38 189 (last (calendar-persian-last-day-of-month month year))
0e41f190
ER
190 (day (calendar-read
191 (format "Persian calendar day (1-%d): " last)
5942f9af 192 (lambda (x) (and (< 0 x) (<= x last))))))
0e41f190
ER
193 (list (list month day year))))
194
fdc22b38
GM
195(define-obsolete-function-alias 'persian-prompt-for-date
196 'calendar-persian-read-date "23.1")
0075b4a2
GM
197
198;;;###cal-autoload
fdc22b38 199(defun calendar-persian-goto-date (date &optional noecho)
0075b4a2
GM
200 "Move cursor to Persian date DATE.
201Echo Persian date unless NOECHO is non-nil."
202 (interactive (calendar-persian-read-date))
203 (calendar-goto-date (calendar-gregorian-from-absolute
fdc22b38
GM
204 (calendar-persian-to-absolute date)))
205 (or noecho (calendar-persian-print-date)))
206
207(define-obsolete-function-alias 'calendar-goto-persian-date
208 'calendar-persian-goto-date "23.1")
0075b4a2 209
b0b671db
GM
210(defvar date)
211
8c34d83e 212;; To be called from diary-list-sexp-entries, where DATE is bound.
6c1841ba 213;;;###diary-autoload
0e41f190
ER
214(defun diary-persian-date ()
215 "Persian calendar equivalent of date diary entry."
edbf2694 216 (format "Persian date: %s" (calendar-persian-date-string date)))
0e41f190 217
a978be66 218(provide 'cal-persia)
0e41f190 219
a978be66 220;;; cal-persia.el ends here