Mercurial > emacs
annotate lisp/calendar/cal-persia.el @ 93734:0a5160e7009f
Update for rename hebrew-diary-entry-symbol to diary-hebrew-entry-symbol.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Sat, 05 Apr 2008 20:21:48 +0000 |
parents | 05a344ce267f |
children | df192d334c0a |
rev | line source |
---|---|
38422
7a94f1c588c4
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
24186
diff
changeset
|
1 ;;; cal-persia.el --- calendar functions for the Persian calendar |
14914 | 2 |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
3 ;; Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
4 ;; 2008 Free Software Foundation, Inc. |
14914 | 5 |
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu> | |
67465
a55ee709ec8d
Update copyright pending Emacs 22.
Glenn Morris <rgm@gnu.org>
parents:
65145
diff
changeset
|
7 ;; Maintainer: Glenn Morris <rgm@gnu.org> |
14914 | 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 | |
78216
93e11478c954
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
77278
diff
changeset
|
15 ;; the Free Software Foundation; either version 3, or (at your option) |
14914 | 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 | |
15070
ec84bec5804e
Update some comments.
Edward M. Reingold <reingold@emr.cs.iit.edu>
parents:
14914
diff
changeset
|
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64085 | 25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 ;; Boston, MA 02110-1301, USA. | |
14914 | 27 |
28 ;;; Commentary: | |
29 | |
93487
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
30 ;; See calendar.el. |
14914 | 31 |
32 ;;; Code: | |
33 | |
93225
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
34 (require 'calendar) |
14914 | 35 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
36 (defconst calendar-persian-month-name-array |
14914 | 37 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban" |
92820 | 38 "Azar" "Dey" "Bahman" "Esfand"] |
39 "Names of the months in the Persian calendar.") | |
14914 | 40 |
93225
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
41 (eval-and-compile |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
42 (autoload 'calendar-julian-to-absolute "cal-julian")) |
93225
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
43 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
44 (defconst calendar-persian-epoch |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
45 (eval-when-compile (calendar-julian-to-absolute '(3 19 622))) |
92820 | 46 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).") |
14914 | 47 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
48 (defun calendar-persian-leap-year-p (year) |
14914 | 49 "True if YEAR is a leap year on the Persian calendar." |
50 (< (mod (* (mod (mod (if (<= 0 year) | |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
51 (+ year 2346) ; no year zero |
14914 | 52 (+ year 2347)) |
53 2820) | |
54 768) | |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
55 683) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
56 2820) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
57 683)) |
14914 | 58 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
59 (defun calendar-persian-last-day-of-month (month year) |
14914 | 60 "Return last day of MONTH, YEAR on the Persian calendar." |
61 (cond | |
62 ((< month 7) 31) | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
63 ((or (< month 12) (calendar-persian-leap-year-p year)) 30) |
14914 | 64 (t 29))) |
65 | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
66 (defun calendar-persian-to-absolute (date) |
14914 | 67 "Compute absolute date from Persian date DATE. |
68 The absolute date is the number of days elapsed since the (imaginary) | |
69 Gregorian 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) | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
74 (+ (calendar-persian-to-absolute |
14914 | 75 (list month day (1+ (mod year 2820)))) |
76 (* 1029983 (floor year 2820))) | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
77 (+ (1- calendar-persian-epoch) ; days before epoch |
92820 | 78 (* 365 (1- year)) ; days in prior years |
79 (* 683 ; leap days in prior 2820-year cycles | |
14914 | 80 (floor (+ year 2345) 2820)) |
92820 | 81 (* 186 ; leap days in prior 768 year cycles |
14914 | 82 (floor (mod (+ year 2345) 2820) 768)) |
92820 | 83 (floor ; leap years in current 768 or 516 year cycle |
14914 | 84 (* 683 (mod (mod (+ year 2345) 2820) 768)) |
85 2820) | |
92820 | 86 -568 ; leap years in Persian years -2345...-1 |
87 (calendar-sum ; days in prior months this year | |
14914 | 88 m 1 (< m month) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
89 (calendar-persian-last-day-of-month m year)) |
92820 | 90 day)))) ; days so far this month |
14914 | 91 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
92 (define-obsolete-function-alias 'calendar-absolute-from-persian |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
93 'calendar-persian-to-absolute "23.1") |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
94 |
14914 | 95 (defun calendar-persian-year-from-absolute (date) |
96 "Persian year corresponding to the absolute DATE." | |
92820 | 97 (let* ((d0 ; prior days since start of 2820 cycles |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
98 (- date (calendar-persian-to-absolute (list 1 1 -2345)))) |
92820 | 99 (n2820 ; completed 2820-year cycles |
14914 | 100 (floor d0 1029983)) |
92820 | 101 (d1 ; prior days not in n2820 |
14914 | 102 (mod d0 1029983)) |
92820 | 103 (n768 ; 768-year cycles not in n2820 |
14914 | 104 (floor d1 280506)) |
92820 | 105 (d2 ; prior days not in n2820 or n768 |
14914 | 106 (mod d1 280506)) |
92903 | 107 (n1 ; years not in n2820 or n768 |
92820 | 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)) | |
14914 | 113 (b (mod d2 366))) |
114 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983)))) | |
92820 | 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 | |
14914 | 119 (1- n1) |
120 n1) | |
92820 | 121 -2345))) ; years before year 1 |
14914 | 122 (if (< year 1) |
92820 | 123 (1- year) ; no year zero |
14914 | 124 year))) |
125 | |
126 (defun calendar-persian-from-absolute (date) | |
127 "Compute the Persian equivalent for absolute date DATE. | |
128 The result is a list of the form (MONTH DAY YEAR). | |
129 The absolute date is the number of days elapsed since the imaginary | |
130 Gregorian date Sunday, December 31, 1 BC." | |
131 (let* ((year (calendar-persian-year-from-absolute date)) | |
92820 | 132 (month ; search forward from Farvardin |
14914 | 133 (1+ (calendar-sum m 1 |
134 (> date | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
135 (calendar-persian-to-absolute |
14914 | 136 (list |
137 m | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
138 (calendar-persian-last-day-of-month m year) |
14914 | 139 year))) |
140 1))) | |
92820 | 141 (day ; calculate the day by subtraction |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
142 (- date (1- (calendar-persian-to-absolute |
14914 | 143 (list month 1 year)))))) |
144 (list month day year))) | |
145 | |
92837
2bcff1e54131
(generated-autoload-file): Don't set, instead use different values of
Glenn Morris <rgm@gnu.org>
parents:
92820
diff
changeset
|
146 ;;;###cal-autoload |
14914 | 147 (defun calendar-persian-date-string (&optional date) |
92820 | 148 "String of Persian date of Gregorian DATE, default today." |
14914 | 149 (let* ((persian-date (calendar-persian-from-absolute |
92903 | 150 (calendar-absolute-from-gregorian |
151 (or date (calendar-current-date))))) | |
14914 | 152 (y (extract-calendar-year persian-date)) |
93487
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
153 (m (extract-calendar-month persian-date)) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
154 (monthname (aref calendar-persian-month-name-array (1- m))) |
93487
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
155 (day (int-to-string (extract-calendar-day persian-date))) |
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
156 (year (int-to-string y)) |
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
157 (month (int-to-string m)) |
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
158 dayname) |
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
159 (mapconcat 'eval calendar-date-display-form ""))) |
14914 | 160 |
92837
2bcff1e54131
(generated-autoload-file): Don't set, instead use different values of
Glenn Morris <rgm@gnu.org>
parents:
92820
diff
changeset
|
161 ;;;###cal-autoload |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
162 (defun calendar-persian-print-date () |
14914 | 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 | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
168 (define-obsolete-function-alias 'calendar-print-persian-date |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
169 'calendar-persian-print-date "23.1") |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
170 |
93186
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
171 (defun calendar-persian-read-date () |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
172 "Interactively read the arguments for a Persian date command. |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
173 Reads a year, month, and day." |
92820 | 174 (let* ((year (calendar-read |
14914 | 175 "Persian calendar year (not 0): " |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
176 (lambda (x) (not (zerop x))) |
14914 | 177 (int-to-string |
178 (extract-calendar-year | |
179 (calendar-persian-from-absolute | |
92820 | 180 (calendar-absolute-from-gregorian |
181 (calendar-current-date))))))) | |
14914 | 182 (completion-ignore-case t) |
183 (month (cdr (assoc | |
92903 | 184 (completing-read |
185 "Persian calendar month name: " | |
186 (mapcar 'list | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
187 (append calendar-persian-month-name-array nil)) |
92903 | 188 nil t) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
189 (calendar-make-alist calendar-persian-month-name-array |
24186
8aae7db1922c
(persian-prompt-for-date): Use assoc-ignore-case and do not capitalize
Richard M. Stallman <rms@gnu.org>
parents:
20462
diff
changeset
|
190 1)))) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
191 (last (calendar-persian-last-day-of-month month year)) |
14914 | 192 (day (calendar-read |
193 (format "Persian calendar day (1-%d): " last) | |
92591
dc0c296afd7e
Unquote lambda functions. Add autoload cookies to functions formerly
Glenn Morris <rgm@gnu.org>
parents:
79703
diff
changeset
|
194 (lambda (x) (and (< 0 x) (<= x last)))))) |
14914 | 195 (list (list month day year)))) |
196 | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
197 (define-obsolete-function-alias 'persian-prompt-for-date |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
198 'calendar-persian-read-date "23.1") |
93186
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
199 |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
200 ;;;###cal-autoload |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
201 (defun calendar-persian-goto-date (date &optional noecho) |
93186
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
202 "Move cursor to Persian date DATE. |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
203 Echo Persian date unless NOECHO is non-nil." |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
204 (interactive (calendar-persian-read-date)) |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
205 (calendar-goto-date (calendar-gregorian-from-absolute |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
206 (calendar-persian-to-absolute date))) |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
207 (or noecho (calendar-persian-print-date))) |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
208 |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
209 (define-obsolete-function-alias 'calendar-goto-persian-date |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
210 'calendar-persian-goto-date "23.1") |
93186
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
211 |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
212 (defvar date) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
213 |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
214 ;; To be called from list-sexp-diary-entries, where DATE is bound. |
92837
2bcff1e54131
(generated-autoload-file): Don't set, instead use different values of
Glenn Morris <rgm@gnu.org>
parents:
92820
diff
changeset
|
215 ;;;###diary-autoload |
14914 | 216 (defun diary-persian-date () |
217 "Persian calendar equivalent of date diary entry." | |
17382
41db5b776fe4
(diary-persian-date): Use `date'.
Richard M. Stallman <rms@gnu.org>
parents:
15259
diff
changeset
|
218 (format "Persian date: %s" (calendar-persian-date-string date))) |
14914 | 219 |
15259
984ea4011d7e
Renamed from cal-persian.el to avoid 14-character limitation.
Karl Heuer <kwzh@gnu.org>
parents:
15070
diff
changeset
|
220 (provide 'cal-persia) |
14914 | 221 |
92591
dc0c296afd7e
Unquote lambda functions. Add autoload cookies to functions formerly
Glenn Morris <rgm@gnu.org>
parents:
79703
diff
changeset
|
222 ;; arch-tag: 2832383c-e4b4-4dc2-8ee9-cfbdd53e5e2d |
15259
984ea4011d7e
Renamed from cal-persian.el to avoid 14-character limitation.
Karl Heuer <kwzh@gnu.org>
parents:
15070
diff
changeset
|
223 ;;; cal-persia.el ends here |