1 ;;; cal-persia.el --- calendar functions for the Persian calendar
3 ;; Copyright (C) 1996-1997, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; 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 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
34 (defconst calendar-persian-month-name-array
35 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
36 "Azar" "Dey" "Bahman" "Esfand"]
37 "Names of the months in the Persian calendar.")
40 (autoload 'calendar-julian-to-absolute
"cal-julian"))
42 (defconst calendar-persian-epoch
43 (eval-when-compile (calendar-julian-to-absolute '(3 19 622)))
44 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).")
46 (defun calendar-persian-leap-year-p (year)
47 "True if YEAR is a leap year on the Persian calendar."
48 (< (mod (* (mod (mod (if (<= 0 year
)
49 (+ year
2346) ; no year zero
57 (defun calendar-persian-last-day-of-month (month year
)
58 "Return last day of MONTH, YEAR on the Persian calendar."
61 ((or (< month
12) (calendar-persian-leap-year-p year
)) 30)
64 (defun calendar-persian-to-absolute (date)
65 "Compute absolute date from Persian date DATE.
66 The absolute date is the number of days elapsed since the (imaginary)
67 Gregorian date Sunday, December 31, 1 BC."
68 (let ((month (calendar-extract-month date
))
69 (day (calendar-extract-day date
))
70 (year (calendar-extract-year date
)))
72 (+ (calendar-persian-to-absolute
73 (list month day
(1+ (mod year
2820))))
74 (* 1029983 (floor year
2820)))
75 (+ (1- calendar-persian-epoch
) ; days before epoch
76 (* 365 (1- year
)) ; days in prior years
77 (* 683 ; leap days in prior 2820-year cycles
78 (floor (+ year
2345) 2820))
79 (* 186 ; leap days in prior 768 year cycles
80 (floor (mod (+ year
2345) 2820) 768))
81 (floor ; leap years in current 768 or 516 year cycle
82 (* 683 (mod (mod (+ year
2345) 2820) 768))
84 -
568 ; leap years in Persian years -2345...-1
85 (calendar-sum ; days in prior months this year
87 (calendar-persian-last-day-of-month m year
))
88 day
)))) ; days so far this month
90 (defun calendar-persian-year-from-absolute (date)
91 "Persian year corresponding to the absolute DATE."
92 (let* ((d0 ; prior days since start of 2820 cycles
93 (- date
(calendar-persian-to-absolute (list 1 1 -
2345))))
94 (n2820 ; completed 2820-year cycles
96 (d1 ; prior days not in n2820
98 (n768 ; 768-year cycles not in n2820
100 (d2 ; prior days not in n2820 or n768
102 (n1 ; years not in n2820 or n768
104 ;; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
105 ;; but that causes overflow, so use the following.
106 ;; Use 366 as the divisor because (2820*366 mod 1029983) is small.
107 (let ((a (floor d2
366))
109 (+ 1 a
(floor (+ (* 2137 a
) (* 2820 b
) 2137) 1029983))))
110 (year (+ (* 2820 n2820
) ; complete 2820 year cycles
111 (* 768 n768
) ; complete 768 year cycles
113 (if (= d1
1029617) ; last day of 2820 year cycle
116 -
2345))) ; years before year 1
118 (1- year
) ; no year zero
121 (defun calendar-persian-from-absolute (date)
122 "Compute the Persian equivalent for absolute date DATE.
123 The result is a list of the form (MONTH DAY YEAR).
124 The absolute date is the number of days elapsed since the imaginary
125 Gregorian date Sunday, December 31, 1 BC."
126 (let* ((year (calendar-persian-year-from-absolute date
))
127 (month ; search forward from Farvardin
128 (1+ (calendar-sum m
1
130 (calendar-persian-to-absolute
133 (calendar-persian-last-day-of-month m year
)
136 (day ; calculate the day by subtraction
137 (- date
(1- (calendar-persian-to-absolute
138 (list month
1 year
))))))
139 (list month day year
)))
142 (defun calendar-persian-date-string (&optional date
)
143 "String of Persian date of Gregorian DATE, default today."
144 (let* ((persian-date (calendar-persian-from-absolute
145 (calendar-absolute-from-gregorian
146 (or date
(calendar-current-date)))))
147 (y (calendar-extract-year persian-date
))
148 (m (calendar-extract-month persian-date
))
149 (monthname (aref calendar-persian-month-name-array
(1- m
)))
150 (day (number-to-string (calendar-extract-day persian-date
)))
151 (year (number-to-string y
))
152 (month (number-to-string m
))
154 (mapconcat 'eval calendar-date-display-form
"")))
157 (defun calendar-persian-print-date ()
158 "Show the Persian calendar equivalent of the selected date."
160 (message "Persian date: %s"
161 (calendar-persian-date-string (calendar-cursor-to-date t
))))
163 (defun calendar-persian-read-date ()
164 "Interactively read the arguments for a Persian date command.
165 Reads a year, month, and day."
166 (let* ((year (calendar-read
167 "Persian calendar year (not 0): "
168 (lambda (x) (not (zerop x
)))
170 (calendar-extract-year
171 (calendar-persian-from-absolute
172 (calendar-absolute-from-gregorian
173 (calendar-current-date)))))))
174 (completion-ignore-case t
)
177 "Persian calendar month name: "
179 (append calendar-persian-month-name-array nil
))
181 (calendar-make-alist calendar-persian-month-name-array
183 (last (calendar-persian-last-day-of-month month year
))
185 (format "Persian calendar day (1-%d): " last
)
186 (lambda (x) (and (< 0 x
) (<= x last
))))))
187 (list (list month day year
))))
190 (defun calendar-persian-goto-date (date &optional noecho
)
191 "Move cursor to Persian date DATE.
192 Echo Persian date unless NOECHO is non-nil."
193 (interactive (calendar-persian-read-date))
194 (calendar-goto-date (calendar-gregorian-from-absolute
195 (calendar-persian-to-absolute date
)))
196 (or noecho
(calendar-persian-print-date)))
201 ;; To be called from diary-list-sexp-entries, where DATE is bound.
203 (defun diary-persian-date ()
204 "Persian calendar equivalent of date diary entry."
205 (format "Persian date: %s" (calendar-persian-date-string date
)))
207 (provide 'cal-persia
)
209 ;;; cal-persia.el ends here