1 ;;; cal-persia.el --- calendar functions for the Persian calendar.
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Human-Keywords: Persian calendar, calendar, diary
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This collection of functions implements the features of calendar.el and
29 ;; diary.el that deal with the Persian calendar.
31 ;; Comments, corrections, and improvements should be sent to
32 ;; Edward M. Reingold Department of Computer Science
33 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
34 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
35 ;; Urbana, Illinois 61801
41 (defvar persian-calendar-month-name-array
42 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
43 "Azar" "Dey" "Bahman" "Esfand"])
45 (defvar persian-calendar-epoch
(calendar-absolute-from-julian '(3 19 622))
46 "Absolute date of start of Persian calendar = March 19, 622 A.D. (Julian).")
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
)
60 (defun persian-calendar-last-day-of-month (month year
)
61 "Return last day of MONTH, YEAR on the Persian calendar."
64 ((or (< month
12) (persian-calendar-leap-year-p year
)) 30)
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
)))
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))
87 -
568 ; Leap years in Persian years -2345...-1
88 (calendar-sum ; Days in prior months this year.
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
99 (d1 ; Prior days not in n2820
101 (n768 ; 768-year cycles not in n2820
103 (d2 ; Prior days not in n2820 or n768
105 (n1 ; Years not in n2820 or n768
107 ; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
108 ; but that causes overflow, so we use
109 (let ((a (floor d2
366)); we use 366 as the divisor because
110 ; (2820*366 mod 1029983) is small
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 (if ; Remaining years
116 ; Last day of 2820 year cycle
120 -
2345))) ; Years before year 1
122 (1- year
); No year zero
125 (defun calendar-persian-from-absolute (date)
126 "Compute the Persian equivalent for absolute date DATE.
127 The result is a list of the form (MONTH DAY YEAR).
128 The absolute date is the number of days elapsed since the imaginary
129 Gregorian date Sunday, December 31, 1 BC."
130 (let* ((year (calendar-persian-year-from-absolute date
))
131 (month ; Search forward from Farvardin
132 (1+ (calendar-sum m
1
134 (calendar-absolute-from-persian
137 (persian-calendar-last-day-of-month m year
)
140 (day ; Calculate the day by subtraction
141 (- date
(1- (calendar-absolute-from-persian
142 (list month
1 year
))))))
143 (list month day year
)))
145 (defun calendar-persian-date-string (&optional date
)
146 "String of Persian date of Gregorian DATE.
147 Defaults to today's date if DATE is not given."
148 (let* ((persian-date (calendar-persian-from-absolute
149 (calendar-absolute-from-gregorian
150 (or date
(calendar-current-date)))))
151 (y (extract-calendar-year persian-date
))
152 (m (extract-calendar-month persian-date
)))
153 (let ((monthname (aref persian-calendar-month-name-array
(1- m
)))
154 (day (int-to-string (extract-calendar-day persian-date
)))
156 (month (int-to-string m
))
157 (year (int-to-string y
)))
158 (mapconcat 'eval calendar-date-display-form
""))))
160 (defun calendar-print-persian-date ()
161 "Show the Persian calendar equivalent of the selected date."
163 (message "Persian date: %s"
164 (calendar-persian-date-string (calendar-cursor-to-date t
))))
166 (defun calendar-goto-persian-date (date &optional noecho
)
167 "Move cursor to Persian date DATE.
168 Echo Persian date unless NOECHO is t."
169 (interactive (persian-prompt-for-date))
170 (calendar-goto-date (calendar-gregorian-from-absolute
171 (calendar-absolute-from-persian date
)))
172 (or noecho
(calendar-print-persian-date)))
174 (defun persian-prompt-for-date ()
175 "Ask for a Persian date."
176 (let* ((today (calendar-current-date))
178 "Persian calendar year (not 0): "
179 '(lambda (x) (/= x
0))
181 (extract-calendar-year
182 (calendar-persian-from-absolute
183 (calendar-absolute-from-gregorian today
))))))
184 (completion-ignore-case t
)
188 "Persian calendar month name: "
190 (append persian-calendar-month-name-array nil
))
192 (calendar-make-alist persian-calendar-month-name-array
194 (last (persian-calendar-last-day-of-month month year
))
196 (format "Persian calendar day (1-%d): " last
)
197 '(lambda (x) (and (< 0 x
) (<= x last
))))))
198 (list (list month day year
))))
200 (defun diary-persian-date ()
201 "Persian calendar equivalent of date diary entry."
202 (calendar-persian-date-string (calendar-cursor-to-date t
)))
204 (provide 'cal-persia
)
206 ;;; cal-persia.el ends here