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