(calendar-iso-to-absolute): Rename calendar-absolute-from-iso.
[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
b0b671db 36(defconst persian-calendar-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
GM
41(eval-and-compile
42 (autoload 'calendar-absolute-from-julian "cal-julian"))
43
44(defconst persian-calendar-epoch
45 (eval-when-compile (calendar-absolute-from-julian '(3 19 622)))
d01890ee 46 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).")
0e41f190
ER
47
48(defun persian-calendar-leap-year-p (year)
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
ER
58
59(defun persian-calendar-last-day-of-month (month year)
60 "Return last day of MONTH, YEAR on the Persian calendar."
61 (cond
62 ((< month 7) 31)
63 ((or (< month 12) (persian-calendar-leap-year-p year)) 30)
64 (t 29)))
65
66(defun calendar-absolute-from-persian (date)
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."
70 (let ((month (extract-calendar-month date))
71 (day (extract-calendar-day date))
72 (year (extract-calendar-year date)))
73 (if (< year 0)
74 (+ (calendar-absolute-from-persian
75 (list month day (1+ (mod year 2820))))
76 (* 1029983 (floor year 2820)))
d01890ee
GM
77 (+ (1- persian-calendar-epoch) ; days before epoch
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
ER
88 m 1 (< m month)
89 (persian-calendar-last-day-of-month m year))
d01890ee 90 day)))) ; days so far this month
0e41f190
ER
91
92(defun calendar-persian-year-from-absolute (date)
93 "Persian year corresponding to the absolute DATE."
d01890ee 94 (let* ((d0 ; prior days since start of 2820 cycles
0e41f190 95 (- date (calendar-absolute-from-persian (list 1 1 -2345))))
d01890ee 96 (n2820 ; completed 2820-year cycles
0e41f190 97 (floor d0 1029983))
d01890ee 98 (d1 ; prior days not in n2820
0e41f190 99 (mod d0 1029983))
d01890ee 100 (n768 ; 768-year cycles not in n2820
0e41f190 101 (floor d1 280506))
d01890ee 102 (d2 ; prior days not in n2820 or n768
0e41f190 103 (mod d1 280506))
52e0f59e 104 (n1 ; years not in n2820 or n768
d01890ee
GM
105 ;; Want:
106 ;; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
107 ;; but that causes overflow, so use the following.
108 ;; Use 366 as the divisor because (2820*366 mod 1029983) is small.
109 (let ((a (floor d2 366))
0e41f190
ER
110 (b (mod d2 366)))
111 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983))))
d01890ee
GM
112 (year (+ (* 2820 n2820) ; complete 2820 year cycles
113 (* 768 n768) ; complete 768 year cycles
114 ;; Remaining years.
115 (if (= d1 1029617) ; last day of 2820 year cycle
0e41f190
ER
116 (1- n1)
117 n1)
d01890ee 118 -2345))) ; years before year 1
0e41f190 119 (if (< year 1)
d01890ee 120 (1- year) ; no year zero
0e41f190
ER
121 year)))
122
123(defun calendar-persian-from-absolute (date)
124 "Compute the Persian equivalent for absolute date DATE.
125The result is a list of the form (MONTH DAY YEAR).
126The absolute date is the number of days elapsed since the imaginary
127Gregorian date Sunday, December 31, 1 BC."
128 (let* ((year (calendar-persian-year-from-absolute date))
d01890ee 129 (month ; search forward from Farvardin
0e41f190
ER
130 (1+ (calendar-sum m 1
131 (> date
132 (calendar-absolute-from-persian
133 (list
134 m
135 (persian-calendar-last-day-of-month m year)
136 year)))
137 1)))
d01890ee 138 (day ; calculate the day by subtraction
0e41f190
ER
139 (- date (1- (calendar-absolute-from-persian
140 (list month 1 year))))))
141 (list month day year)))
142
6c1841ba 143;;;###cal-autoload
0e41f190 144(defun calendar-persian-date-string (&optional date)
d01890ee 145 "String of Persian date of Gregorian DATE, default today."
0e41f190 146 (let* ((persian-date (calendar-persian-from-absolute
52e0f59e
GM
147 (calendar-absolute-from-gregorian
148 (or date (calendar-current-date)))))
0e41f190 149 (y (extract-calendar-year persian-date))
e5e99776
GM
150 (m (extract-calendar-month persian-date))
151 (monthname (aref persian-calendar-month-name-array (1- m)))
152 (day (int-to-string (extract-calendar-day persian-date)))
153 (year (int-to-string y))
154 (month (int-to-string m))
155 dayname)
156 (mapconcat 'eval calendar-date-display-form "")))
0e41f190 157
6c1841ba 158;;;###cal-autoload
0e41f190
ER
159(defun calendar-print-persian-date ()
160 "Show the Persian calendar equivalent of the selected date."
161 (interactive)
162 (message "Persian date: %s"
163 (calendar-persian-date-string (calendar-cursor-to-date t))))
164
0075b4a2
GM
165(defun calendar-persian-read-date ()
166 "Interactively read the arguments for a Persian date command.
167Reads a year, month, and day."
d01890ee 168 (let* ((year (calendar-read
0e41f190 169 "Persian calendar year (not 0): "
b0b671db 170 (lambda (x) (not (zerop x)))
0e41f190
ER
171 (int-to-string
172 (extract-calendar-year
173 (calendar-persian-from-absolute
d01890ee
GM
174 (calendar-absolute-from-gregorian
175 (calendar-current-date)))))))
0e41f190
ER
176 (completion-ignore-case t)
177 (month (cdr (assoc
52e0f59e
GM
178 (completing-read
179 "Persian calendar month name: "
180 (mapcar 'list
181 (append persian-calendar-month-name-array nil))
182 nil t)
0e41f190 183 (calendar-make-alist persian-calendar-month-name-array
c0982a97 184 1))))
0e41f190
ER
185 (last (persian-calendar-last-day-of-month month year))
186 (day (calendar-read
187 (format "Persian calendar day (1-%d): " last)
5942f9af 188 (lambda (x) (and (< 0 x) (<= x last))))))
0e41f190
ER
189 (list (list month day year))))
190
0075b4a2
GM
191(define-obsolete-function-alias
192 'persian-prompt-for-date 'calendar-persian-read-date "23.1")
193
194;;;###cal-autoload
195(defun calendar-goto-persian-date (date &optional noecho)
196 "Move cursor to Persian date DATE.
197Echo Persian date unless NOECHO is non-nil."
198 (interactive (calendar-persian-read-date))
199 (calendar-goto-date (calendar-gregorian-from-absolute
200 (calendar-absolute-from-persian date)))
201 (or noecho (calendar-print-persian-date)))
202
b0b671db
GM
203(defvar date)
204
205;; To be called from list-sexp-diary-entries, where DATE is bound.
6c1841ba 206;;;###diary-autoload
0e41f190
ER
207(defun diary-persian-date ()
208 "Persian calendar equivalent of date diary entry."
edbf2694 209 (format "Persian date: %s" (calendar-persian-date-string date)))
0e41f190 210
a978be66 211(provide 'cal-persia)
0e41f190 212
5942f9af 213;; arch-tag: 2832383c-e4b4-4dc2-8ee9-cfbdd53e5e2d
a978be66 214;;; cal-persia.el ends here