Mercurial > emacs
annotate lisp/calendar/cal-persia.el @ 83507:81f2d90dee68
Merged from
Patches applied:
* emacs@sv.gnu.org/emacs--devo--0--patch-188
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-189
Merge from gnus--rel--5.10
* emacs@sv.gnu.org/emacs--devo--0--patch-190
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-191
Undo incorrect merge of etc/images/README from Gnus 5.10
* emacs@sv.gnu.org/emacs--devo--0--patch-192
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-193
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-194
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-195
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-196
Merge from gnus--rel--5.10
* emacs@sv.gnu.org/emacs--devo--0--patch-197
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-198
Merge from gnus--rel--5.10
* emacs@sv.gnu.org/emacs--devo--0--patch-199
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-200
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-201
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-202
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-203
Merge from gnus--rel--5.10
* emacs@sv.gnu.org/emacs--devo--0--patch-204
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-205
Update from CVS
* emacs@sv.gnu.org/emacs--devo--0--patch-206
Update from CVS
* emacs@sv.gnu.org/gnus--rel--5.10--patch-73
Update from CVS
* emacs@sv.gnu.org/gnus--rel--5.10--patch-74
Update from CVS
* emacs@sv.gnu.org/gnus--rel--5.10--patch-75
Update from CVS
* emacs@sv.gnu.org/gnus--rel--5.10--patch-76
Update from CVS: README: Addition from 5.10.6 tar ball.
* emacs@sv.gnu.org/gnus--rel--5.10--patch-77
Update from CVS
* emacs@sv.gnu.org/gnus--rel--5.10--patch-78
Update from CVS
* emacs@sv.gnu.org/gnus--rel--5.10--patch-79
Update from CVS
* emacs@sv.gnu.org/gnus--rel--5.10--patch-80
Update from CVS
git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-547
author | Karoly Lorentey <lorentey@elte.hu> |
---|---|
date | Mon, 10 Apr 2006 14:52:24 +0000 |
parents | 8daf7d9a0771 |
children | 7a3f13e2dd57 4b3d39451150 |
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 |
68721 | 3 ;; Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006 |
67465
a55ee709ec8d
Update copyright pending Emacs 22.
Glenn Morris <rgm@gnu.org>
parents:
65145
diff
changeset
|
4 ;; 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 | |
15 ;; the Free Software Foundation; either version 2, or (at your option) | |
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 | |
30 ;; This collection of functions implements the features of calendar.el and | |
31 ;; diary.el that deal with the Persian calendar. | |
32 | |
20462
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
17382
diff
changeset
|
33 ;; Technical details of all the calendrical calculations can be found in |
61148
7f7db25577d9
Update reference to "Calendrical Calculations" book; there's a new edition.
Paul Eggert <eggert@twinsun.com>
parents:
52401
diff
changeset
|
34 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold |
7f7db25577d9
Update reference to "Calendrical Calculations" book; there's a new edition.
Paul Eggert <eggert@twinsun.com>
parents:
52401
diff
changeset
|
35 ;; and Nachum Dershowitz, Cambridge University Press (2001). |
20462
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
17382
diff
changeset
|
36 |
14914 | 37 ;; Comments, corrections, and improvements should be sent to |
38 ;; Edward M. Reingold Department of Computer Science | |
39 ;; (217) 333-6733 University of Illinois at Urbana-Champaign | |
40 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue | |
41 ;; Urbana, Illinois 61801 | |
42 | |
43 ;;; Code: | |
44 | |
65145
ac895e21e622
(date): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents:
64085
diff
changeset
|
45 (defvar date) |
ac895e21e622
(date): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents:
64085
diff
changeset
|
46 |
14914 | 47 (require 'cal-julian) |
48 | |
49 (defvar persian-calendar-month-name-array | |
50 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban" | |
51 "Azar" "Dey" "Bahman" "Esfand"]) | |
52 | |
53 (defvar persian-calendar-epoch (calendar-absolute-from-julian '(3 19 622)) | |
54 "Absolute date of start of Persian calendar = March 19, 622 A.D. (Julian).") | |
55 | |
56 (defun persian-calendar-leap-year-p (year) | |
57 "True if YEAR is a leap year on the Persian calendar." | |
58 (< (mod (* (mod (mod (if (<= 0 year) | |
59 ; No year zero | |
60 (+ year 2346) | |
61 (+ year 2347)) | |
62 2820) | |
63 768) | |
64 683) | |
65 2820) | |
66 683)) | |
67 | |
68 (defun persian-calendar-last-day-of-month (month year) | |
69 "Return last day of MONTH, YEAR on the Persian calendar." | |
70 (cond | |
71 ((< month 7) 31) | |
72 ((or (< month 12) (persian-calendar-leap-year-p year)) 30) | |
73 (t 29))) | |
74 | |
75 (defun calendar-absolute-from-persian (date) | |
76 "Compute absolute date from Persian date DATE. | |
77 The absolute date is the number of days elapsed since the (imaginary) | |
78 Gregorian date Sunday, December 31, 1 BC." | |
79 (let ((month (extract-calendar-month date)) | |
80 (day (extract-calendar-day date)) | |
81 (year (extract-calendar-year date))) | |
82 (if (< year 0) | |
83 (+ (calendar-absolute-from-persian | |
84 (list month day (1+ (mod year 2820)))) | |
85 (* 1029983 (floor year 2820))) | |
86 (+ (1- persian-calendar-epoch); Days before epoch | |
87 (* 365 (1- year)) ; Days in prior years. | |
88 (* 683 ; Leap days in prior 2820-year cycles | |
89 (floor (+ year 2345) 2820)) | |
90 (* 186 ; Leap days in prior 768 year cycles | |
91 (floor (mod (+ year 2345) 2820) 768)) | |
92 (floor; Leap years in current 768 or 516 year cycle | |
93 (* 683 (mod (mod (+ year 2345) 2820) 768)) | |
94 2820) | |
95 -568 ; Leap years in Persian years -2345...-1 | |
96 (calendar-sum ; Days in prior months this year. | |
97 m 1 (< m month) | |
98 (persian-calendar-last-day-of-month m year)) | |
99 day)))) ; Days so far this month. | |
100 | |
101 (defun calendar-persian-year-from-absolute (date) | |
102 "Persian year corresponding to the absolute DATE." | |
103 (let* ((d0 ; Prior days since start of 2820 cycles | |
104 (- date (calendar-absolute-from-persian (list 1 1 -2345)))) | |
105 (n2820 ; Completed 2820-year cycles | |
106 (floor d0 1029983)) | |
107 (d1 ; Prior days not in n2820 | |
108 (mod d0 1029983)) | |
109 (n768 ; 768-year cycles not in n2820 | |
110 (floor d1 280506)) | |
111 (d2 ; Prior days not in n2820 or n768 | |
112 (mod d1 280506)) | |
113 (n1 ; Years not in n2820 or n768 | |
114 ; we want is | |
115 ; (floor (+ (* 2820 d2) (* 2820 366)) 1029983)) | |
116 ; but that causes overflow, so we use | |
117 (let ((a (floor d2 366)); we use 366 as the divisor because | |
118 ; (2820*366 mod 1029983) is small | |
119 (b (mod d2 366))) | |
120 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983)))) | |
121 (year (+ (* 2820 n2820); Complete 2820 year cycles | |
122 (* 768 n768) ; Complete 768 year cycles | |
123 (if ; Remaining years | |
124 ; Last day of 2820 year cycle | |
125 (= d1 1029617) | |
126 (1- n1) | |
127 n1) | |
128 -2345))) ; Years before year 1 | |
129 (if (< year 1) | |
130 (1- year); No year zero | |
131 year))) | |
132 | |
133 (defun calendar-persian-from-absolute (date) | |
134 "Compute the Persian equivalent for absolute date DATE. | |
135 The result is a list of the form (MONTH DAY YEAR). | |
136 The absolute date is the number of days elapsed since the imaginary | |
137 Gregorian date Sunday, December 31, 1 BC." | |
138 (let* ((year (calendar-persian-year-from-absolute date)) | |
139 (month ; Search forward from Farvardin | |
140 (1+ (calendar-sum m 1 | |
141 (> date | |
142 (calendar-absolute-from-persian | |
143 (list | |
144 m | |
145 (persian-calendar-last-day-of-month m year) | |
146 year))) | |
147 1))) | |
148 (day ; Calculate the day by subtraction | |
149 (- date (1- (calendar-absolute-from-persian | |
150 (list month 1 year)))))) | |
151 (list month day year))) | |
152 | |
153 (defun calendar-persian-date-string (&optional date) | |
154 "String of Persian date of Gregorian DATE. | |
155 Defaults to today's date if DATE is not given." | |
156 (let* ((persian-date (calendar-persian-from-absolute | |
157 (calendar-absolute-from-gregorian | |
158 (or date (calendar-current-date))))) | |
159 (y (extract-calendar-year persian-date)) | |
160 (m (extract-calendar-month persian-date))) | |
161 (let ((monthname (aref persian-calendar-month-name-array (1- m))) | |
162 (day (int-to-string (extract-calendar-day persian-date))) | |
163 (dayname nil) | |
164 (month (int-to-string m)) | |
165 (year (int-to-string y))) | |
166 (mapconcat 'eval calendar-date-display-form "")))) | |
167 | |
168 (defun calendar-print-persian-date () | |
169 "Show the Persian calendar equivalent of the selected date." | |
170 (interactive) | |
171 (message "Persian date: %s" | |
172 (calendar-persian-date-string (calendar-cursor-to-date t)))) | |
173 | |
174 (defun calendar-goto-persian-date (date &optional noecho) | |
175 "Move cursor to Persian date DATE. | |
176 Echo Persian date unless NOECHO is t." | |
177 (interactive (persian-prompt-for-date)) | |
178 (calendar-goto-date (calendar-gregorian-from-absolute | |
179 (calendar-absolute-from-persian date))) | |
180 (or noecho (calendar-print-persian-date))) | |
181 | |
182 (defun persian-prompt-for-date () | |
183 "Ask for a Persian date." | |
184 (let* ((today (calendar-current-date)) | |
185 (year (calendar-read | |
186 "Persian calendar year (not 0): " | |
187 '(lambda (x) (/= x 0)) | |
188 (int-to-string | |
189 (extract-calendar-year | |
190 (calendar-persian-from-absolute | |
191 (calendar-absolute-from-gregorian today)))))) | |
192 (completion-ignore-case t) | |
193 (month (cdr (assoc | |
194 (completing-read | |
195 "Persian calendar month name: " | |
196 (mapcar 'list | |
197 (append persian-calendar-month-name-array nil)) | |
24186
8aae7db1922c
(persian-prompt-for-date): Use assoc-ignore-case and do not capitalize
Richard M. Stallman <rms@gnu.org>
parents:
20462
diff
changeset
|
198 nil t) |
14914 | 199 (calendar-make-alist persian-calendar-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
|
200 1)))) |
14914 | 201 (last (persian-calendar-last-day-of-month month year)) |
202 (day (calendar-read | |
203 (format "Persian calendar day (1-%d): " last) | |
204 '(lambda (x) (and (< 0 x) (<= x last)))))) | |
205 (list (list month day year)))) | |
206 | |
207 (defun diary-persian-date () | |
208 "Persian calendar equivalent of date diary entry." | |
17382
41db5b776fe4
(diary-persian-date): Use `date'.
Richard M. Stallman <rms@gnu.org>
parents:
15259
diff
changeset
|
209 (format "Persian date: %s" (calendar-persian-date-string date))) |
14914 | 210 |
15259
984ea4011d7e
Renamed from cal-persian.el to avoid 14-character limitation.
Karl Heuer <kwzh@gnu.org>
parents:
15070
diff
changeset
|
211 (provide 'cal-persia) |
14914 | 212 |
52401 | 213 ;;; 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
|
214 ;;; cal-persia.el ends here |