root/trunk/lisp/calendar/cal-persia.el

Revision 4220, 8.1 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; cal-persia.el --- calendar functions for the Persian calendar
2
3 ;; Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
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 3, 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
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
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
35 (defvar date)
36
37 (require 'cal-julian)
38
39 (defvar persian-calendar-month-name-array
40   ["Farvardin" "Ordibehest" "Xordad" "Tir" "Mordad" "Sahrivar" "Mehr" "Aban"
41    "Azar" "Dey" "Bahman" "Esfand"])
42
43 (defvar persian-calendar-epoch (calendar-absolute-from-julian '(3 19 622))
44   "Absolute date of start of Persian calendar = March 19, 622 A.D. (Julian).")
45
46 (defun persian-calendar-leap-year-p (year)
47   "True if YEAR is a leap year on the Persian calendar."
48   (< (mod (* (mod (mod (if (<= 0 year)
49                            ; No year zero
50                            (+ year 2346)
51                          (+ year 2347))
52                        2820)
53                   768)
54               683)
55            2820)
56       683))
57
58 (defun persian-calendar-last-day-of-month (month year)
59   "Return last day of MONTH, YEAR on the Persian calendar."
60   (cond
61    ((< month 7) 31)
62    ((or (< month 12) (persian-calendar-leap-year-p year)) 30)
63    (t 29)))
64
65 (defun calendar-absolute-from-persian (date)
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."
69   (let ((month (extract-calendar-month date))
70         (day (extract-calendar-day date))
71         (year (extract-calendar-year date)))
72     (if (< year 0)
73         (+ (calendar-absolute-from-persian
74             (list month day (1+ (mod year 2820))))
75            (* 1029983 (floor year 2820)))
76       (+ (1- persian-calendar-epoch); Days before epoch
77          (* 365 (1- year)) ; Days in prior years.
78          (* 683        ; Leap days in prior 2820-year cycles
79             (floor (+ year 2345) 2820))
80          (* 186        ; Leap days in prior 768 year cycles
81             (floor (mod (+ year 2345) 2820) 768))
82          (floor; Leap years in current 768 or 516 year cycle
83           (* 683 (mod (mod (+ year 2345) 2820) 768))
84           2820)
85          -568          ; Leap years in Persian years -2345...-1
86          (calendar-sum ; Days in prior months this year.
87           m 1 (< m month)
88           (persian-calendar-last-day-of-month m year))
89          day))))        ; Days so far this month.
90
91 (defun calendar-persian-year-from-absolute (date)
92   "Persian year corresponding to the absolute DATE."
93   (let* ((d0        ; Prior days since start of 2820 cycles
94           (- date (calendar-absolute-from-persian (list 1 1 -2345))))
95          (n2820     ; Completed 2820-year cycles
96           (floor d0 1029983))
97          (d1        ; Prior days not in n2820
98           (mod d0 1029983))
99          (n768      ; 768-year cycles not in n2820
100           (floor d1 280506))
101          (d2        ; Prior days not in n2820 or n768
102           (mod d1 280506))
103          (n1        ; Years not in n2820 or n768
104           ; we want is
105           ; (floor (+ (* 2820 d2) (* 2820 366)) 1029983))
106           ; but that causes overflow, so we use
107           (let ((a (floor d2 366)); we use 366 as the divisor because
108                                   ; (2820*366 mod 1029983) is small
109                 (b (mod d2 366)))
110             (+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983))))
111          (year (+ (* 2820 n2820); Complete 2820 year cycles
112                   (* 768 n768)  ; Complete 768 year cycles
113                   (if           ; Remaining years
114                       ; Last day of 2820 year cycle
115                       (= d1 1029617)
116                       (1- n1)
117                     n1)
118                   -2345)))      ; Years before year 1
119     (if (< year 1)
120         (1- year); No year zero
121       year)))
122
123 (defun calendar-persian-from-absolute (date)
124   "Compute the Persian equivalent for absolute date DATE.
125 The result is a list of the form (MONTH DAY YEAR).
126 The absolute date is the number of days elapsed since the imaginary
127 Gregorian date Sunday, December 31, 1 BC."
128   (let* ((year (calendar-persian-year-from-absolute date))
129          (month         ; Search forward from Farvardin
130           (1+ (calendar-sum m 1
131                             (> date
132                                (calendar-absolute-from-persian
133                                 (list
134                                  m
135                                  (persian-calendar-last-day-of-month m year)
136                                  year)))
137                             1)))
138          (day           ; Calculate the day by subtraction
139           (- date (1- (calendar-absolute-from-persian
140                        (list month 1 year))))))
141     (list month day year)))
142
143 (defun calendar-persian-date-string (&optional date)
144   "String of Persian date of Gregorian DATE.
145 Defaults to today's date if DATE is not given."
146   (let* ((persian-date (calendar-persian-from-absolute
147                        (calendar-absolute-from-gregorian
148                         (or date (calendar-current-date)))))
149          (y (extract-calendar-year persian-date))
150          (m (extract-calendar-month persian-date)))
151     (let ((monthname (aref persian-calendar-month-name-array (1- m)))
152           (day (int-to-string (extract-calendar-day persian-date)))
153           (dayname nil)
154           (month (int-to-string m))
155           (year (int-to-string y)))
156       (mapconcat 'eval calendar-date-display-form ""))))
157
158 (defun calendar-print-persian-date ()
159   "Show the Persian calendar equivalent of the selected date."
160   (interactive)
161   (message "Persian date: %s"
162            (calendar-persian-date-string (calendar-cursor-to-date t))))
163
164 (defun calendar-goto-persian-date (date &optional noecho)
165   "Move cursor to Persian date DATE.
166 Echo Persian date unless NOECHO is t."
167   (interactive (persian-prompt-for-date))
168   (calendar-goto-date (calendar-gregorian-from-absolute
169                        (calendar-absolute-from-persian date)))
170   (or noecho (calendar-print-persian-date)))
171
172 (defun persian-prompt-for-date ()
173   "Ask for a Persian date."
174   (let* ((today (calendar-current-date))
175          (year (calendar-read
176                 "Persian calendar year (not 0): "
177                 '(lambda (x) (/= x 0))
178                 (int-to-string
179                  (extract-calendar-year
180                   (calendar-persian-from-absolute
181                    (calendar-absolute-from-gregorian today))))))
182          (completion-ignore-case t)
183          (month (cdr (assoc
184                        (completing-read
185                         "Persian calendar month name: "
186                         (mapcar 'list
187                                 (append persian-calendar-month-name-array nil))
188                         nil t)
189                       (calendar-make-alist persian-calendar-month-name-array
190                                            1))))
191          (last (persian-calendar-last-day-of-month month year))
192          (day (calendar-read
193                (format "Persian calendar day (1-%d): " last)
194                '(lambda (x) (and (< 0 x) (<= x last))))))
195     (list (list month day year))))
196
197 (defun diary-persian-date ()
198   "Persian calendar equivalent of date diary entry."
199   (format "Persian date: %s" (calendar-persian-date-string date)))
200
201 (provide 'cal-persia)
202
203 ;;; arch-tag: 2832383c-e4b4-4dc2-8ee9-cfbdd53e5e2d
204 ;;; cal-persia.el ends here
205
Note: See TracBrowser for help on using the browser.