Update for `calendar-date-style' replacing `european-calendar'.
[emacs.git] / lisp / calendar / cal-persia.el
blob419286face279e728eee820e474780163921beed
1 ;;; cal-persia.el --- calendar functions for the Persian calendar
3 ;; Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 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
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, or (at your option)
16 ;; 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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This collection of functions implements the features of calendar.el and
31 ;; diary.el that deal with the Persian calendar.
33 ;;; Code:
35 (require 'calendar)
37 (defconst persian-calendar-month-name-array
38 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
39 "Azar" "Dey" "Bahman" "Esfand"]
40 "Names of the months in the Persian calendar.")
42 (eval-and-compile
43 (autoload 'calendar-absolute-from-julian "cal-julian"))
45 (defconst persian-calendar-epoch
46 (eval-when-compile (calendar-absolute-from-julian '(3 19 622)))
47 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).")
49 (defun persian-calendar-leap-year-p (year)
50 "True if YEAR is a leap year on the Persian calendar."
51 (< (mod (* (mod (mod (if (<= 0 year)
52 (+ year 2346) ; no year zero
53 (+ year 2347))
54 2820)
55 768)
56 683)
57 2820)
58 683))
60 (defun persian-calendar-last-day-of-month (month year)
61 "Return last day of MONTH, YEAR on the Persian calendar."
62 (cond
63 ((< month 7) 31)
64 ((or (< month 12) (persian-calendar-leap-year-p year)) 30)
65 (t 29)))
67 (defun calendar-absolute-from-persian (date)
68 "Compute absolute date from Persian date DATE.
69 The absolute date is the number of days elapsed since the (imaginary)
70 Gregorian date Sunday, December 31, 1 BC."
71 (let ((month (extract-calendar-month date))
72 (day (extract-calendar-day date))
73 (year (extract-calendar-year date)))
74 (if (< year 0)
75 (+ (calendar-absolute-from-persian
76 (list month day (1+ (mod year 2820))))
77 (* 1029983 (floor year 2820)))
78 (+ (1- persian-calendar-epoch) ; days before epoch
79 (* 365 (1- year)) ; days in prior years
80 (* 683 ; leap days in prior 2820-year cycles
81 (floor (+ year 2345) 2820))
82 (* 186 ; leap days in prior 768 year cycles
83 (floor (mod (+ year 2345) 2820) 768))
84 (floor ; leap years in current 768 or 516 year cycle
85 (* 683 (mod (mod (+ year 2345) 2820) 768))
86 2820)
87 -568 ; leap years in Persian years -2345...-1
88 (calendar-sum ; days in prior months this year
89 m 1 (< m month)
90 (persian-calendar-last-day-of-month m year))
91 day)))) ; days so far this month
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-absolute-from-persian (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-absolute-from-persian
134 (list
136 (persian-calendar-last-day-of-month m year)
137 year)))
138 1)))
139 (day ; calculate the day by subtraction
140 (- date (1- (calendar-absolute-from-persian
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 (extract-calendar-year persian-date))
151 (m (extract-calendar-month persian-date)))
152 (let ((monthname (aref persian-calendar-month-name-array (1- m)))
153 (day (int-to-string (extract-calendar-day persian-date)))
154 (dayname nil)
155 (month (int-to-string m))
156 (year (int-to-string y)))
157 (mapconcat 'eval calendar-date-display-form ""))))
159 ;;;###cal-autoload
160 (defun calendar-print-persian-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 (defun calendar-persian-read-date ()
167 "Interactively read the arguments for a Persian date command.
168 Reads a year, month, and day."
169 (let* ((year (calendar-read
170 "Persian calendar year (not 0): "
171 (lambda (x) (not (zerop x)))
172 (int-to-string
173 (extract-calendar-year
174 (calendar-persian-from-absolute
175 (calendar-absolute-from-gregorian
176 (calendar-current-date)))))))
177 (completion-ignore-case t)
178 (month (cdr (assoc
179 (completing-read
180 "Persian calendar month name: "
181 (mapcar 'list
182 (append persian-calendar-month-name-array nil))
183 nil t)
184 (calendar-make-alist persian-calendar-month-name-array
185 1))))
186 (last (persian-calendar-last-day-of-month month year))
187 (day (calendar-read
188 (format "Persian calendar day (1-%d): " last)
189 (lambda (x) (and (< 0 x) (<= x last))))))
190 (list (list month day year))))
192 (define-obsolete-function-alias
193 'persian-prompt-for-date 'calendar-persian-read-date "23.1")
195 ;;;###cal-autoload
196 (defun calendar-goto-persian-date (date &optional noecho)
197 "Move cursor to Persian date DATE.
198 Echo Persian date unless NOECHO is non-nil."
199 (interactive (calendar-persian-read-date))
200 (calendar-goto-date (calendar-gregorian-from-absolute
201 (calendar-absolute-from-persian date)))
202 (or noecho (calendar-print-persian-date)))
204 (defvar date)
206 ;; To be called from list-sexp-diary-entries, where DATE is bound.
207 ;;;###diary-autoload
208 (defun diary-persian-date ()
209 "Persian calendar equivalent of date diary entry."
210 (format "Persian date: %s" (calendar-persian-date-string date)))
212 (provide 'cal-persia)
214 ;; arch-tag: 2832383c-e4b4-4dc2-8ee9-cfbdd53e5e2d
215 ;;; cal-persia.el ends here