| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 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 |
|
|---|
| 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) |
|---|
| 77 |
(* 365 (1- year)) |
|---|
| 78 |
(* 683 |
|---|
| 79 |
(floor (+ year 2345) 2820)) |
|---|
| 80 |
(* 186 |
|---|
| 81 |
(floor (mod (+ year 2345) 2820) 768)) |
|---|
| 82 |
(floor |
|---|
| 83 |
(* 683 (mod (mod (+ year 2345) 2820) 768)) |
|---|
| 84 |
2820) |
|---|
| 85 |
-568 |
|---|
| 86 |
(calendar-sum |
|---|
| 87 |
m 1 (< m month) |
|---|
| 88 |
(persian-calendar-last-day-of-month m year)) |
|---|
| 89 |
day)))) |
|---|
| 90 |
|
|---|
| 91 |
(defun calendar-persian-year-from-absolute (date) |
|---|
| 92 |
"Persian year corresponding to the absolute DATE." |
|---|
| 93 |
(let* ((d0 |
|---|
| 94 |
(- date (calendar-absolute-from-persian (list 1 1 -2345)))) |
|---|
| 95 |
(n2820 |
|---|
| 96 |
(floor d0 1029983)) |
|---|
| 97 |
(d1 |
|---|
| 98 |
(mod d0 1029983)) |
|---|
| 99 |
(n768 |
|---|
| 100 |
(floor d1 280506)) |
|---|
| 101 |
(d2 |
|---|
| 102 |
(mod d1 280506)) |
|---|
| 103 |
(n1 |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
(let ((a (floor d2 366)) |
|---|
| 108 |
|
|---|
| 109 |
(b (mod d2 366))) |
|---|
| 110 |
(+ 1 a (floor (+ (* 2137 a) (* 2820 b) 2137) 1029983)))) |
|---|
| 111 |
(year (+ (* 2820 n2820) |
|---|
| 112 |
(* 768 n768) |
|---|
| 113 |
(if |
|---|
| 114 |
|
|---|
| 115 |
(= d1 1029617) |
|---|
| 116 |
(1- n1) |
|---|
| 117 |
n1) |
|---|
| 118 |
-2345))) |
|---|
| 119 |
(if (< year 1) |
|---|
| 120 |
(1- year) |
|---|
| 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 |
|---|
| 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 |
|---|
| 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 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|