Mercurial > emacs
annotate lisp/calendar/cal-persia.el @ 93358:6271c3c4590b
(Basic Isearch): Reference the Mark Ring node.
(Replace, Unconditional Replace, Other Repeating Search):
Describe Transient Mark mode as the default.
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Fri, 28 Mar 2008 19:04:33 +0000 |
parents | da8dabfafecc |
children | 426872139a89 |
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 | |
30 ;; This collection of functions implements the features of calendar.el and | |
31 ;; diary.el that deal with the Persian calendar. | |
32 | |
33 ;;; Code: | |
34 | |
93225
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
35 (require 'calendar) |
14914 | 36 |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
37 (defconst persian-calendar-month-name-array |
14914 | 38 ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban" |
92820 | 39 "Azar" "Dey" "Bahman" "Esfand"] |
40 "Names of the months in the Persian calendar.") | |
14914 | 41 |
93225
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
42 (eval-and-compile |
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
43 (autoload 'calendar-absolute-from-julian "cal-julian")) |
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
44 |
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
45 (defconst persian-calendar-epoch |
da8dabfafecc
Require calendar rather than cal-julian.
Glenn Morris <rgm@gnu.org>
parents:
93186
diff
changeset
|
46 (eval-when-compile (calendar-absolute-from-julian '(3 19 622))) |
92820 | 47 "Absolute date of start of Persian calendar = March 19, 622 AD (Julian).") |
14914 | 48 |
49 (defun persian-calendar-leap-year-p (year) | |
50 "True if YEAR is a leap year on the Persian calendar." | |
51 (< (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
|
52 (+ year 2346) ; no year zero |
14914 | 53 (+ year 2347)) |
54 2820) | |
55 768) | |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
56 683) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
57 2820) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
58 683)) |
14914 | 59 |
60 (defun persian-calendar-last-day-of-month (month year) | |
61 "Return last day of MONTH, YEAR on the Persian calendar." | |
62 (cond | |
63 ((< month 7) 31) | |
64 ((or (< month 12) (persian-calendar-leap-year-p year)) 30) | |
65 (t 29))) | |
66 | |
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))) | |
74 (if (< year 0) | |
75 (+ (calendar-absolute-from-persian | |
76 (list month day (1+ (mod year 2820)))) | |
77 (* 1029983 (floor year 2820))) | |
92820 | 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 | |
14914 | 81 (floor (+ year 2345) 2820)) |
92820 | 82 (* 186 ; leap days in prior 768 year cycles |
14914 | 83 (floor (mod (+ year 2345) 2820) 768)) |
92820 | 84 (floor ; leap years in current 768 or 516 year cycle |
14914 | 85 (* 683 (mod (mod (+ year 2345) 2820) 768)) |
86 2820) | |
92820 | 87 -568 ; leap years in Persian years -2345...-1 |
88 (calendar-sum ; days in prior months this year | |
14914 | 89 m 1 (< m month) |
90 (persian-calendar-last-day-of-month m year)) | |
92820 | 91 day)))) ; days so far this month |
14914 | 92 |
93 (defun calendar-persian-year-from-absolute (date) | |
94 "Persian year corresponding to the absolute DATE." | |
92820 | 95 (let* ((d0 ; prior days since start of 2820 cycles |
14914 | 96 (- date (calendar-absolute-from-persian (list 1 1 -2345)))) |
92820 | 97 (n2820 ; completed 2820-year cycles |
14914 | 98 (floor d0 1029983)) |
92820 | 99 (d1 ; prior days not in n2820 |
14914 | 100 (mod d0 1029983)) |
92820 | 101 (n768 ; 768-year cycles not in n2820 |
14914 | 102 (floor d1 280506)) |
92820 | 103 (d2 ; prior days not in n2820 or n768 |
14914 | 104 (mod d1 280506)) |
92903 | 105 (n1 ; years not in n2820 or n768 |
92820 | 106 ;; Want: |
107 ;; (floor (+ (* 2820 d2) (* 2820 366)) 1029983)) | |
108 ;; but that causes overflow, so use the following. | |
109 ;; Use 366 as the divisor because (2820*366 mod 1029983) is small. | |
110 (let ((a (floor d2 366)) | |
14914 | 111 (b (mod d2 366))) |
112 (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983)))) | |
92820 | 113 (year (+ (* 2820 n2820) ; complete 2820 year cycles |
114 (* 768 n768) ; complete 768 year cycles | |
115 ;; Remaining years. | |
116 (if (= d1 1029617) ; last day of 2820 year cycle | |
14914 | 117 (1- n1) |
118 n1) | |
92820 | 119 -2345))) ; years before year 1 |
14914 | 120 (if (< year 1) |
92820 | 121 (1- year) ; no year zero |
14914 | 122 year))) |
123 | |
124 (defun calendar-persian-from-absolute (date) | |
125 "Compute the Persian equivalent for absolute date DATE. | |
126 The result is a list of the form (MONTH DAY YEAR). | |
127 The absolute date is the number of days elapsed since the imaginary | |
128 Gregorian date Sunday, December 31, 1 BC." | |
129 (let* ((year (calendar-persian-year-from-absolute date)) | |
92820 | 130 (month ; search forward from Farvardin |
14914 | 131 (1+ (calendar-sum m 1 |
132 (> date | |
133 (calendar-absolute-from-persian | |
134 (list | |
135 m | |
136 (persian-calendar-last-day-of-month m year) | |
137 year))) | |
138 1))) | |
92820 | 139 (day ; calculate the day by subtraction |
14914 | 140 (- date (1- (calendar-absolute-from-persian |
141 (list month 1 year)))))) | |
142 (list month day year))) | |
143 | |
92837
2bcff1e54131
(generated-autoload-file): Don't set, instead use different values of
Glenn Morris <rgm@gnu.org>
parents:
92820
diff
changeset
|
144 ;;;###cal-autoload |
14914 | 145 (defun calendar-persian-date-string (&optional date) |
92820 | 146 "String of Persian date of Gregorian DATE, default today." |
14914 | 147 (let* ((persian-date (calendar-persian-from-absolute |
92903 | 148 (calendar-absolute-from-gregorian |
149 (or date (calendar-current-date))))) | |
14914 | 150 (y (extract-calendar-year persian-date)) |
151 (m (extract-calendar-month persian-date))) | |
152 (let ((monthname (aref persian-calendar-month-name-array (1- m))) | |
153 (day (int-to-string (extract-calendar-day persian-date))) | |
154 (dayname nil) | |
155 (month (int-to-string m)) | |
156 (year (int-to-string y))) | |
157 (mapconcat 'eval calendar-date-display-form "")))) | |
158 | |
92837
2bcff1e54131
(generated-autoload-file): Don't set, instead use different values of
Glenn Morris <rgm@gnu.org>
parents:
92820
diff
changeset
|
159 ;;;###cal-autoload |
14914 | 160 (defun calendar-print-persian-date () |
161 "Show the Persian calendar equivalent of the selected date." | |
162 (interactive) | |
163 (message "Persian date: %s" | |
164 (calendar-persian-date-string (calendar-cursor-to-date t)))) | |
165 | |
93186
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
166 (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
|
167 "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
|
168 Reads a year, month, and day." |
92820 | 169 (let* ((year (calendar-read |
14914 | 170 "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
|
171 (lambda (x) (not (zerop x))) |
14914 | 172 (int-to-string |
173 (extract-calendar-year | |
174 (calendar-persian-from-absolute | |
92820 | 175 (calendar-absolute-from-gregorian |
176 (calendar-current-date))))))) | |
14914 | 177 (completion-ignore-case t) |
178 (month (cdr (assoc | |
92903 | 179 (completing-read |
180 "Persian calendar month name: " | |
181 (mapcar 'list | |
182 (append persian-calendar-month-name-array nil)) | |
183 nil t) | |
14914 | 184 (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
|
185 1)))) |
14914 | 186 (last (persian-calendar-last-day-of-month month year)) |
187 (day (calendar-read | |
188 (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
|
189 (lambda (x) (and (< 0 x) (<= x last)))))) |
14914 | 190 (list (list month day year)))) |
191 | |
93186
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
192 (define-obsolete-function-alias |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
193 'persian-prompt-for-date 'calendar-persian-read-date "23.1") |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
194 |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
195 ;;;###cal-autoload |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
196 (defun calendar-goto-persian-date (date &optional noecho) |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
197 "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
|
198 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
|
199 (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
|
200 (calendar-goto-date (calendar-gregorian-from-absolute |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
201 (calendar-absolute-from-persian date))) |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
202 (or noecho (calendar-print-persian-date))) |
3c66b698cc43
(calendar-persian-read-date): New name for persian-prompt-for-date.
Glenn Morris <rgm@gnu.org>
parents:
92903
diff
changeset
|
203 |
92606
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
204 (defvar date) |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
205 |
f49e5129551f
(persian-calendar-month-name-array, persian-calendar-epoch): Make constants.
Glenn Morris <rgm@gnu.org>
parents:
92591
diff
changeset
|
206 ;; 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
|
207 ;;;###diary-autoload |
14914 | 208 (defun diary-persian-date () |
209 "Persian calendar equivalent of date diary entry." | |
17382
41db5b776fe4
(diary-persian-date): Use `date'.
Richard M. Stallman <rms@gnu.org>
parents:
15259
diff
changeset
|
210 (format "Persian date: %s" (calendar-persian-date-string date))) |
14914 | 211 |
15259
984ea4011d7e
Renamed from cal-persian.el to avoid 14-character limitation.
Karl Heuer <kwzh@gnu.org>
parents:
15070
diff
changeset
|
212 (provide 'cal-persia) |
14914 | 213 |
92591
dc0c296afd7e
Unquote lambda functions. Add autoload cookies to functions formerly
Glenn Morris <rgm@gnu.org>
parents:
79703
diff
changeset
|
214 ;; 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
|
215 ;;; cal-persia.el ends here |