Mercurial > emacs
annotate lisp/calendar/cal-persia.el @ 110149:4ac2b883320e
Forgor to save ChangeLog for previous commit.
author | Jan D. <jan.h.d@swipnet.se> |
---|---|
date | Thu, 02 Sep 2010 12:20:20 +0200 |
parents | 280c8ae2476d |
children | 417b1e4d63cd |
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, |
106815 | 4 ;; 2008, 2009, 2010 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 | |
110015
280c8ae2476d
Add "Package:" file headers to denote built-in packages.
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
10 ;; Package: calendar |
14914 | 11 |
12 ;; This file is part of GNU Emacs. | |
13 | |
94653
e49abd957e81
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93844
diff
changeset
|
14 ;; GNU Emacs is free software: you can redistribute it and/or modify |
14914 | 15 ;; it under the terms of the GNU General Public License as published by |
94653
e49abd957e81
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93844
diff
changeset
|
16 ;; the Free Software Foundation, either version 3 of the License, or |
e49abd957e81
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93844
diff
changeset
|
17 ;; (at your option) any later version. |
14914 | 18 |
19 ;; GNU Emacs is distributed in the hope that it will be useful, | |
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 ;; GNU General Public License for more details. | |
23 | |
24 ;; You should have received a copy of the GNU General Public License | |
94653
e49abd957e81
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93844
diff
changeset
|
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
14914 | 26 |
27 ;;; Commentary: | |
28 | |
93487
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
29 ;; See calendar.el. |
14914 | 30 |
31 ;;; Code: | |
32 | |
93225
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
33 (require 'calendar) |
14914 | 34 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
35 (defconst calendar-persian-month-name-array |
14914 | 36 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban" |
92820 | 37 "Azar" "Dey" "Bahman" "Esfand"] |
38 "Names of the months in the Persian calendar.") | |
14914 | 39 |
93225
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
40 (eval-and-compile |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
41 (autoload 'calendar-julian-to-absolute "cal-julian")) |
93225
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
42 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
43 (defconst calendar-persian-epoch |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
44 (eval-when-compile (calendar-julian-to-absolute '(3 19 622))) |
92820 | 45 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).") |
14914 | 46 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
47 (defun calendar-persian-leap-year-p (year) |
14914 | 48 "True if YEAR is a leap year on the Persian calendar." |
49 (< (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
|
50 (+ year 2346) ; no year zero |
14914 | 51 (+ year 2347)) |
52 2820) | |
53 768) | |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
54 683) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
55 2820) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
56 683)) |
14914 | 57 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
58 (defun calendar-persian-last-day-of-month (month year) |
14914 | 59 "Return last day of MONTH, YEAR on the Persian calendar." |
60 (cond | |
61 ((< month 7) 31) | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
62 ((or (< month 12) (calendar-persian-leap-year-p year)) 30) |
14914 | 63 (t 29))) |
64 | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
65 (defun calendar-persian-to-absolute (date) |
14914 | 66 "Compute absolute date from Persian date DATE. |
67 The absolute date is the number of days elapsed since the (imaginary) | |
68 Gregorian date Sunday, December 31, 1 BC." | |
93809
3ff2b47de8f2
Update for calendar.el name changes.
Glenn Morris <rgm@gnu.org>
parents:
93785
diff
changeset
|
69 (let ((month (calendar-extract-month date)) |
3ff2b47de8f2
Update for calendar.el name changes.
Glenn Morris <rgm@gnu.org>
parents:
93785
diff
changeset
|
70 (day (calendar-extract-day date)) |
3ff2b47de8f2
Update for calendar.el name changes.
Glenn Morris <rgm@gnu.org>
parents:
93785
diff
changeset
|
71 (year (calendar-extract-year date))) |
14914 | 72 (if (< year 0) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
73 (+ (calendar-persian-to-absolute |
14914 | 74 (list month day (1+ (mod year 2820)))) |
75 (* 1029983 (floor year 2820))) | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
76 (+ (1- calendar-persian-epoch) ; days before epoch |
92820 | 77 (* 365 (1- year)) ; days in prior years |
78 (* 683 ; leap days in prior 2820-year cycles | |
14914 | 79 (floor (+ year 2345) 2820)) |
92820 | 80 (* 186 ; leap days in prior 768 year cycles |
14914 | 81 (floor (mod (+ year 2345) 2820) 768)) |
92820 | 82 (floor ; leap years in current 768 or 516 year cycle |
14914 | 83 (* 683 (mod (mod (+ year 2345) 2820) 768)) |
84 2820) | |
92820 | 85 -568 ; leap years in Persian years -2345...-1 |
86 (calendar-sum ; days in prior months this year | |
14914 | 87 m 1 (< m month) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
88 (calendar-persian-last-day-of-month m year)) |
92820 | 89 day)))) ; days so far this month |
14914 | 90 |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
91 (define-obsolete-function-alias 'calendar-absolute-from-persian |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
92 'calendar-persian-to-absolute "23.1") |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
93 |
14914 | 94 (defun calendar-persian-year-from-absolute (date) |
95 "Persian year corresponding to the absolute DATE." | |
92820 | 96 (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
|
97 (- date (calendar-persian-to-absolute (list 1 1 -2345)))) |
92820 | 98 (n2820 ; completed 2820-year cycles |
14914 | 99 (floor d0 1029983)) |
92820 | 100 (d1 ; prior days not in n2820 |
14914 | 101 (mod d0 1029983)) |
92820 | 102 (n768 ; 768-year cycles not in n2820 |
14914 | 103 (floor d1 280506)) |
92820 | 104 (d2 ; prior days not in n2820 or n768 |
14914 | 105 (mod d1 280506)) |
92903 | 106 (n1 ; years not in n2820 or n768 |
92820 | 107 ;; Want: |
108 ;; (floor (+ (* 2820 d2) (* 2820 366)) 1029983)) | |
109 ;; but that causes overflow, so use the following. | |
110 ;; Use 366 as the divisor because (2820*366 mod 1029983) is small. | |
111 (let ((a (floor d2 366)) | |
14914 | 112 (b (mod d2 366))) |
113 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983)))) | |
92820 | 114 (year (+ (* 2820 n2820) ; complete 2820 year cycles |
115 (* 768 n768) ; complete 768 year cycles | |
116 ;; Remaining years. | |
117 (if (= d1 1029617) ; last day of 2820 year cycle | |
14914 | 118 (1- n1) |
119 n1) | |
92820 | 120 -2345))) ; years before year 1 |
14914 | 121 (if (< year 1) |
92820 | 122 (1- year) ; no year zero |
14914 | 123 year))) |
124 | |
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)) | |
92820 | 131 (month ; search forward from Farvardin |
14914 | 132 (1+ (calendar-sum m 1 |
133 (> date | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
134 (calendar-persian-to-absolute |
14914 | 135 (list |
136 m | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
137 (calendar-persian-last-day-of-month m year) |
14914 | 138 year))) |
139 1))) | |
92820 | 140 (day ; calculate the day by subtraction |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
141 (- date (1- (calendar-persian-to-absolute |
14914 | 142 (list month 1 year)))))) |
143 (list month day year))) | |
144 | |
92837
2bcff1e54131
(generated-autoload-file): Don't set, instead use different values of
Glenn Morris <rgm@gnu.org>
parents:
92820
diff
changeset
|
145 ;;;###cal-autoload |
14914 | 146 (defun calendar-persian-date-string (&optional date) |
92820 | 147 "String of Persian date of Gregorian DATE, default today." |
14914 | 148 (let* ((persian-date (calendar-persian-from-absolute |
92903 | 149 (calendar-absolute-from-gregorian |
150 (or date (calendar-current-date))))) | |
93809
3ff2b47de8f2
Update for calendar.el name changes.
Glenn Morris <rgm@gnu.org>
parents:
93785
diff
changeset
|
151 (y (calendar-extract-year persian-date)) |
3ff2b47de8f2
Update for calendar.el name changes.
Glenn Morris <rgm@gnu.org>
parents:
93785
diff
changeset
|
152 (m (calendar-extract-month persian-date)) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
153 (monthname (aref calendar-persian-month-name-array (1- m))) |
93844
bf9ef749c23e
Replace int-to-string with number-to-string.
Glenn Morris <rgm@gnu.org>
parents:
93809
diff
changeset
|
154 (day (number-to-string (calendar-extract-day persian-date))) |
bf9ef749c23e
Replace int-to-string with number-to-string.
Glenn Morris <rgm@gnu.org>
parents:
93809
diff
changeset
|
155 (year (number-to-string y)) |
bf9ef749c23e
Replace int-to-string with number-to-string.
Glenn Morris <rgm@gnu.org>
parents:
93809
diff
changeset
|
156 (month (number-to-string m)) |
93487
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
157 dayname) |
426872139a89
(Commentary): Point to calendar.el.
Glenn Morris <rgm@gnu.org>
parents:
93225
diff
changeset
|
158 (mapconcat 'eval calendar-date-display-form ""))) |
14914 | 159 |
92837
2bcff1e54131
(generated-autoload-file): Don't set, instead use different values of
Glenn Morris <rgm@gnu.org>
parents:
92820
diff
changeset
|
160 ;;;###cal-autoload |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
161 (defun calendar-persian-print-date () |
14914 | 162 "Show the Persian calendar equivalent of the selected date." |
163 (interactive) | |
164 (message "Persian date: %s" | |
165 (calendar-persian-date-string (calendar-cursor-to-date t)))) | |
166 | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
167 (define-obsolete-function-alias 'calendar-print-persian-date |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
168 'calendar-persian-print-date "23.1") |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
169 |
93186
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
170 (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
|
171 "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
|
172 Reads a year, month, and day." |
92820 | 173 (let* ((year (calendar-read |
14914 | 174 "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
|
175 (lambda (x) (not (zerop x))) |
93844
bf9ef749c23e
Replace int-to-string with number-to-string.
Glenn Morris <rgm@gnu.org>
parents:
93809
diff
changeset
|
176 (number-to-string |
93809
3ff2b47de8f2
Update for calendar.el name changes.
Glenn Morris <rgm@gnu.org>
parents:
93785
diff
changeset
|
177 (calendar-extract-year |
14914 | 178 (calendar-persian-from-absolute |
92820 | 179 (calendar-absolute-from-gregorian |
180 (calendar-current-date))))))) | |
14914 | 181 (completion-ignore-case t) |
182 (month (cdr (assoc | |
92903 | 183 (completing-read |
184 "Persian calendar month name: " | |
185 (mapcar 'list | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
186 (append calendar-persian-month-name-array nil)) |
92903 | 187 nil t) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
188 (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
|
189 1)))) |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
190 (last (calendar-persian-last-day-of-month month year)) |
14914 | 191 (day (calendar-read |
192 (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
|
193 (lambda (x) (and (< 0 x) (<= x last)))))) |
14914 | 194 (list (list month day year)))) |
195 | |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
196 (define-obsolete-function-alias 'persian-prompt-for-date |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
197 '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
|
198 |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
199 ;;;###cal-autoload |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
200 (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
|
201 "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
|
202 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
|
203 (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
|
204 (calendar-goto-date (calendar-gregorian-from-absolute |
93644
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
205 (calendar-persian-to-absolute date))) |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
206 (or noecho (calendar-persian-print-date))) |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
207 |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
208 (define-obsolete-function-alias 'calendar-goto-persian-date |
05a344ce267f
Update for cal-julian name changes.
Glenn Morris <rgm@gnu.org>
parents:
93487
diff
changeset
|
209 '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
|
210 |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
211 (defvar date) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
212 |
93785 | 213 ;; To be called from diary-list-sexp-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
|
214 ;;;###diary-autoload |
14914 | 215 (defun diary-persian-date () |
216 "Persian calendar equivalent of date diary entry." | |
17382
41db5b776fe4
(diary-persian-date): Use `date'.
Richard M. Stallman <rms@gnu.org>
parents:
15259
diff
changeset
|
217 (format "Persian date: %s" (calendar-persian-date-string date))) |
14914 | 218 |
15259
984ea4011d7e
Renamed from cal-persian.el to avoid 14-character limitation.
Karl Heuer <kwzh@gnu.org>
parents:
15070
diff
changeset
|
219 (provide 'cal-persia) |
14914 | 220 |
92591
dc0c296afd7e
Unquote lambda functions. Add autoload cookies to functions formerly
Glenn Morris <rgm@gnu.org>
parents:
79703
diff
changeset
|
221 ;; 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
|
222 ;;; cal-persia.el ends here |