Mercurial > emacs
annotate lisp/calc/calc-forms.el @ 89231:a4f536fc73d9
Fix mode line.
author | Dave Love <fx@gnu.org> |
---|---|
date | Wed, 16 Oct 2002 16:36:57 +0000 |
parents | 0f4506820432 |
children | f4d68f97221e |
rev | line source |
---|---|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1 ;;; calc-forms.el --- data format conversion functions for Calc |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
4 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
6 ;; Maintainer: Colin Walters <walters@debian.org> |
40785 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 ;; accepts responsibility to anyone for the consequences of using it | |
13 ;; or for whether it serves any particular purpose or works at all, | |
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 ;; License for full details. | |
16 | |
17 ;; Everyone is granted permission to copy, modify and redistribute | |
18 ;; GNU Emacs, but only under the conditions described in the | |
19 ;; GNU Emacs General Public License. A copy of this license is | |
20 ;; supposed to have been given to you along with GNU Emacs so you | |
21 ;; can know your rights and responsibilities. It should be in a | |
22 ;; file named COPYING. Among other things, the copyright notice | |
23 ;; and this notice must be preserved on all copies. | |
24 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
25 ;;; Commentary: |
40785 | 26 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
27 ;;; Code: |
40785 | 28 |
29 ;; This file is autoloaded from calc-ext.el. | |
30 (require 'calc-ext) | |
31 | |
32 (require 'calc-macs) | |
33 | |
34 (defun calc-Need-calc-forms () nil) | |
35 | |
36 | |
37 (defun calc-time () | |
38 (interactive) | |
39 (calc-wrapper | |
40 (let ((time (current-time-string))) | |
41 (calc-enter-result 0 "time" | |
42 (list 'mod | |
43 (list 'hms | |
44 (string-to-int (substring time 11 13)) | |
45 (string-to-int (substring time 14 16)) | |
46 (string-to-int (substring time 17 19))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
47 (list 'hms 24 0 0)))))) |
40785 | 48 |
49 (defun calc-to-hms (arg) | |
50 (interactive "P") | |
51 (calc-wrapper | |
52 (if (calc-is-inverse) | |
53 (if (eq calc-angle-mode 'rad) | |
54 (calc-unary-op ">rad" 'calcFunc-rad arg) | |
55 (calc-unary-op ">deg" 'calcFunc-deg arg)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
56 (calc-unary-op ">hms" 'calcFunc-hms arg)))) |
40785 | 57 |
58 (defun calc-from-hms (arg) | |
59 (interactive "P") | |
60 (calc-invert-func) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
61 (calc-to-hms arg)) |
40785 | 62 |
63 | |
64 (defun calc-hms-notation (fmt) | |
65 (interactive "sHours-minutes-seconds format (hms, @ ' \", etc.): ") | |
66 (calc-wrapper | |
67 (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt) | |
68 (progn | |
69 (calc-change-mode 'calc-hms-format | |
70 (concat "%s" (math-match-substring fmt 1) | |
71 (math-match-substring fmt 2) | |
72 "%s" (math-match-substring fmt 3) | |
73 (math-match-substring fmt 4) | |
74 "%s" (math-match-substring fmt 5)) | |
75 t) | |
76 (setq-default calc-hms-format calc-hms-format)) ; for minibuffer | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
77 (error "Bad hours-minutes-seconds format")))) |
40785 | 78 |
79 (defun calc-date-notation (fmt arg) | |
80 (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP") | |
81 (calc-wrapper | |
82 (if (equal fmt "") | |
83 (setq fmt "1")) | |
84 (if (string-match "\\` *[0-9] *\\'" fmt) | |
85 (setq fmt (nth (string-to-int fmt) calc-standard-date-formats))) | |
86 (or (string-match "[a-zA-Z]" fmt) | |
87 (error "Bad date format specifier")) | |
88 (and arg | |
89 (>= (setq arg (prefix-numeric-value arg)) 0) | |
90 (<= arg 9) | |
91 (setq calc-standard-date-formats | |
92 (copy-sequence calc-standard-date-formats)) | |
93 (setcar (nthcdr arg calc-standard-date-formats) fmt)) | |
94 (let ((case-fold-search nil)) | |
95 (and (not (string-match "<.*>" fmt)) | |
96 (string-match "\\`[^hHspP]*\\([^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*[bBhHmpPsS]+[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*\\)[^hHspP]*\\'" fmt) | |
97 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*" | |
98 (regexp-quote (math-match-substring fmt 1)) | |
99 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt) | |
100 (setq fmt (concat (substring fmt 0 (match-beginning 0)) | |
101 "<" | |
102 (substring fmt (match-beginning 0) (match-end 0)) | |
103 ">" | |
104 (substring fmt (match-end 0)))))) | |
105 (let ((lfmt nil) | |
106 (fullfmt nil) | |
107 (time nil) | |
108 pos pos2 sym temp) | |
109 (let ((case-fold-search nil)) | |
110 (and (setq temp (string-match ":[BS]S" fmt)) | |
111 (aset fmt temp ?C))) | |
112 (while (setq pos (string-match "[<>a-zA-Z]" fmt)) | |
113 (if (> pos 0) | |
114 (setq lfmt (cons (substring fmt 0 pos) lfmt))) | |
115 (setq pos2 (1+ pos)) | |
116 (cond ((= (aref fmt pos) ?\<) | |
117 (and time (error "Nested <'s not allowed")) | |
118 (and lfmt (setq fullfmt (nconc lfmt fullfmt) | |
119 lfmt nil)) | |
120 (setq time t)) | |
121 ((= (aref fmt pos) ?\>) | |
122 (or time (error "Misplaced > in format")) | |
123 (and lfmt (setq fullfmt (cons (nreverse lfmt) fullfmt) | |
124 lfmt nil)) | |
125 (setq time nil)) | |
126 (t | |
127 (if (string-match "\\`[^a-zA-Z]*[bB][a-zA-Z]" fmt) | |
128 (setq pos2 (1+ pos2))) | |
129 (while (and (< pos2 (length fmt)) | |
130 (= (upcase (aref fmt pos2)) | |
131 (upcase (aref fmt (1- pos2))))) | |
132 (setq pos2 (1+ pos2))) | |
133 (setq sym (intern (substring fmt pos pos2))) | |
134 (or (memq sym '(Y YY BY YYY YYYY | |
135 aa AA aaa AAA aaaa AAAA | |
136 bb BB bbb BBB bbbb BBBB | |
137 M MM BM mmm Mmm Mmmm MMM MMMM | |
138 D DD BD d ddd bdd | |
139 W www Www Wwww WWW WWWW | |
140 h hh bh H HH BH | |
141 p P pp PP pppp PPPP | |
142 m mm bm s ss bss SS BS C | |
143 N n J j U b)) | |
144 (and (eq sym 'X) (not lfmt) (not fullfmt)) | |
145 (error "Bad format code: %s" sym)) | |
146 (and (memq sym '(bb BB bbb BBB bbbb BBBB)) | |
147 (setq lfmt (cons 'b lfmt))) | |
148 (setq lfmt (cons sym lfmt)))) | |
149 (setq fmt (substring fmt pos2))) | |
150 (or (equal fmt "") | |
151 (setq lfmt (cons fmt lfmt))) | |
152 (and lfmt (if time | |
153 (setq fullfmt (cons (nreverse lfmt) fullfmt)) | |
154 (setq fullfmt (nconc lfmt fullfmt)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
155 (calc-change-mode 'calc-date-format (nreverse fullfmt) t)))) |
40785 | 156 |
157 | |
158 (defun calc-hms-mode () | |
159 (interactive) | |
160 (calc-wrapper | |
161 (calc-change-mode 'calc-angle-mode 'hms) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
162 (message "Angles measured in degrees-minutes-seconds"))) |
40785 | 163 |
164 | |
165 (defun calc-now (arg) | |
166 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
167 (calc-date-zero-args "now" 'calcFunc-now arg)) |
40785 | 168 |
169 (defun calc-date-part (arg) | |
170 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ") | |
171 (if (or (< arg 1) (> arg 9)) | |
172 (error "Part code out of range")) | |
173 (calc-wrapper | |
174 (calc-enter-result 1 | |
175 (nth arg '(nil "year" "mnth" "day" "hour" "minu" | |
176 "sec" "wday" "yday" "hmst")) | |
177 (list (nth arg '(nil calcFunc-year calcFunc-month | |
178 calcFunc-day calcFunc-hour | |
179 calcFunc-minute calcFunc-second | |
180 calcFunc-weekday calcFunc-yearday | |
181 calcFunc-time)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
182 (calc-top-n 1))))) |
40785 | 183 |
184 (defun calc-date (arg) | |
185 (interactive "p") | |
186 (if (or (< arg 1) (> arg 6)) | |
187 (error "Between one and six arguments are allowed")) | |
188 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
189 (calc-enter-result arg "date" (cons 'calcFunc-date (calc-top-list-n arg))))) |
40785 | 190 |
191 (defun calc-julian (arg) | |
192 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
193 (calc-date-one-arg "juln" 'calcFunc-julian arg)) |
40785 | 194 |
195 (defun calc-unix-time (arg) | |
196 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
197 (calc-date-one-arg "unix" 'calcFunc-unixtime arg)) |
40785 | 198 |
199 (defun calc-time-zone (arg) | |
200 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
201 (calc-date-zero-args "zone" 'calcFunc-tzone arg)) |
40785 | 202 |
203 (defun calc-convert-time-zones (old &optional new) | |
204 (interactive "sFrom time zone: ") | |
205 (calc-wrapper | |
206 (if (equal old "$") | |
207 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv (calc-top-list-n 3))) | |
208 (if (equal old "") (setq old "local")) | |
209 (or new | |
210 (setq new (read-string (concat "From time zone: " old | |
211 ", to zone: ")))) | |
212 (if (stringp old) (setq old (math-read-expr old))) | |
213 (if (eq (car-safe old) 'error) | |
214 (error "Error in expression: " (nth 1 old))) | |
215 (if (equal new "") (setq new "local")) | |
216 (if (stringp new) (setq new (math-read-expr new))) | |
217 (if (eq (car-safe new) 'error) | |
218 (error "Error in expression: " (nth 1 new))) | |
219 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
220 (calc-top-n 1) old new))))) |
40785 | 221 |
222 (defun calc-new-week (arg) | |
223 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
224 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg)) |
40785 | 225 |
226 (defun calc-new-month (arg) | |
227 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
228 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg)) |
40785 | 229 |
230 (defun calc-new-year (arg) | |
231 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
232 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg)) |
40785 | 233 |
234 (defun calc-inc-month (arg) | |
235 (interactive "p") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
236 (calc-date-one-arg "incm" 'calcFunc-incmonth arg)) |
40785 | 237 |
238 (defun calc-business-days-plus (arg) | |
239 (interactive "P") | |
240 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
241 (calc-binary-op "bus+" 'calcFunc-badd arg))) |
40785 | 242 |
243 (defun calc-business-days-minus (arg) | |
244 (interactive "P") | |
245 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
246 (calc-binary-op "bus-" 'calcFunc-bsub arg))) |
40785 | 247 |
248 (defun calc-date-zero-args (prefix func arg) | |
249 (calc-wrapper | |
250 (if (consp arg) | |
251 (calc-enter-result 1 prefix (list func (calc-top-n 1))) | |
252 (calc-enter-result 0 prefix (if arg | |
253 (list func (prefix-numeric-value arg)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
254 (list func)))))) |
40785 | 255 |
256 (defun calc-date-one-arg (prefix func arg) | |
257 (calc-wrapper | |
258 (if (consp arg) | |
259 (calc-enter-result 2 prefix (cons func (calc-top-list-n 2))) | |
260 (calc-enter-result 1 prefix (if arg | |
261 (list func (calc-top-n 1) | |
262 (prefix-numeric-value arg)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
263 (list func (calc-top-n 1))))))) |
40785 | 264 |
265 | |
266 ;;;; Hours-minutes-seconds forms. | |
267 | |
268 (defun math-normalize-hms (a) | |
269 (let ((h (math-normalize (nth 1 a))) | |
270 (m (math-normalize (nth 2 a))) | |
271 (s (let ((calc-internal-prec (max (- calc-internal-prec 4) 3))) | |
272 (math-normalize (nth 3 a))))) | |
273 (if (math-negp h) | |
274 (progn | |
275 (if (math-posp s) | |
276 (setq s (math-add s -60) | |
277 m (math-add m 1))) | |
278 (if (math-posp m) | |
279 (setq m (math-add m -60) | |
280 h (math-add h 1))) | |
281 (if (not (Math-lessp -60 s)) | |
282 (setq s (math-add s 60) | |
283 m (math-add m -1))) | |
284 (if (not (Math-lessp -60 m)) | |
285 (setq m (math-add m 60) | |
286 h (math-add h -1)))) | |
287 (if (math-negp s) | |
288 (setq s (math-add s 60) | |
289 m (math-add m -1))) | |
290 (if (math-negp m) | |
291 (setq m (math-add m 60) | |
292 h (math-add h -1))) | |
293 (if (not (Math-lessp s 60)) | |
294 (setq s (math-add s -60) | |
295 m (math-add m 1))) | |
296 (if (not (Math-lessp m 60)) | |
297 (setq m (math-add m -60) | |
298 h (math-add h 1)))) | |
299 (if (and (eq (car-safe s) 'float) | |
300 (<= (+ (math-numdigs (nth 1 s)) (nth 2 s)) | |
301 (- 2 calc-internal-prec))) | |
302 (setq s 0)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
303 (list 'hms h m s))) |
40785 | 304 |
305 ;;; Convert A from ANG or current angular mode to HMS format. | |
306 (defun math-to-hms (a &optional ang) ; [X R] [Public] | |
307 (cond ((eq (car-safe a) 'hms) a) | |
308 ((eq (car-safe a) 'sdev) | |
309 (math-make-sdev (math-to-hms (nth 1 a)) | |
310 (math-to-hms (nth 2 a)))) | |
311 ((not (Math-numberp a)) | |
312 (list 'calcFunc-hms a)) | |
313 ((math-negp a) | |
314 (math-neg (math-to-hms (math-neg a) ang))) | |
315 ((eq (or ang calc-angle-mode) 'rad) | |
316 (math-to-hms (math-div a (math-pi-over-180)) 'deg)) | |
317 ((memq (car-safe a) '(cplx polar)) a) | |
318 (t | |
319 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3))) | |
320 ; (math-normalize a))) | |
321 (math-normalize | |
322 (let* ((b (math-mul a 3600)) | |
323 (hm (math-trunc (math-div b 60))) | |
324 (hmd (math-idivmod hm 60))) | |
325 (list 'hms | |
326 (car hmd) | |
327 (cdr hmd) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
328 (math-sub b (math-mul hm 60)))))))) |
40785 | 329 (defun calcFunc-hms (h &optional m s) |
330 (or (Math-realp h) (math-reject-arg h 'realp)) | |
331 (or m (setq m 0)) | |
332 (or (Math-realp m) (math-reject-arg m 'realp)) | |
333 (or s (setq s 0)) | |
334 (or (Math-realp s) (math-reject-arg s 'realp)) | |
335 (if (and (not (Math-lessp m 0)) (Math-lessp m 60) | |
336 (not (Math-lessp s 0)) (Math-lessp s 60)) | |
337 (math-add (math-to-hms h) | |
338 (list 'hms 0 m s)) | |
339 (math-to-hms (math-add h | |
340 (math-add (math-div (or m 0) 60) | |
341 (math-div (or s 0) 3600))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
342 'deg))) |
40785 | 343 |
344 ;;; Convert A from HMS format to ANG or current angular mode. | |
345 (defun math-from-hms (a &optional ang) ; [R X] [Public] | |
346 (cond ((not (eq (car-safe a) 'hms)) | |
347 (if (Math-numberp a) | |
348 a | |
349 (if (eq (car-safe a) 'sdev) | |
350 (math-make-sdev (math-from-hms (nth 1 a) ang) | |
351 (math-from-hms (nth 2 a) ang)) | |
352 (if (eq (or ang calc-angle-mode) 'rad) | |
353 (list 'calcFunc-rad a) | |
354 (list 'calcFunc-deg a))))) | |
355 ((math-negp a) | |
356 (math-neg (math-from-hms (math-neg a) ang))) | |
357 ((eq (or ang calc-angle-mode) 'rad) | |
358 (math-mul (math-from-hms a 'deg) (math-pi-over-180))) | |
359 (t | |
360 (math-add (math-div (math-add (math-div (nth 3 a) | |
361 '(float 6 1)) | |
362 (nth 2 a)) | |
363 60) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
364 (nth 1 a))))) |
40785 | 365 |
366 ;;;; Date forms. | |
367 | |
368 | |
369 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el". | |
370 ;;; These versions are rewritten to use arbitrary-size integers. | |
371 ;;; The Julian calendar is used up to 9/2/1752, after which the Gregorian | |
372 ;;; calendar is used; the first day after 9/2/1752 is 9/14/1752. | |
373 | |
374 ;;; A numerical date is the number of days since midnight on | |
375 ;;; the morning of January 1, 1 A.D. If the date is a non-integer, | |
376 ;;; it represents a specific date and time. | |
377 ;;; A "dt" is a list of the form, (year month day), corresponding to | |
378 ;;; an integer code, or (year month day hour minute second), corresponding | |
379 ;;; to a non-integer code. | |
380 | |
381 (defun math-date-to-dt (value) | |
382 (if (eq (car-safe value) 'date) | |
383 (setq value (nth 1 value))) | |
384 (or (math-realp value) | |
385 (math-reject-arg value 'datep)) | |
386 (let* ((parts (math-date-parts value)) | |
387 (date (car parts)) | |
388 (time (nth 1 parts)) | |
389 (month 1) | |
390 day | |
391 (year (math-quotient (math-add date (if (Math-lessp date 711859) | |
392 365 ; for speed, we take | |
393 -108)) ; >1950 as a special case | |
394 (if (math-negp value) 366 365))) | |
395 ; this result may be an overestimate | |
396 temp) | |
397 (while (Math-lessp date (setq temp (math-absolute-from-date year 1 1))) | |
398 (setq year (math-add year -1))) | |
399 (if (eq year 0) (setq year -1)) | |
400 (setq date (1+ (math-sub date temp))) | |
401 (and (eq year 1752) (>= date 247) | |
402 (setq date (+ date 11))) | |
403 (setq temp (if (math-leap-year-p year) | |
404 [1 32 61 92 122 153 183 214 245 275 306 336 999] | |
405 [1 32 60 91 121 152 182 213 244 274 305 335 999])) | |
406 (while (>= date (aref temp month)) | |
407 (setq month (1+ month))) | |
408 (setq day (1+ (- date (aref temp (1- month))))) | |
409 (if (math-integerp value) | |
410 (list year month day) | |
411 (list year month day | |
412 (/ time 3600) | |
413 (% (/ time 60) 60) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
414 (math-add (% time 60) (nth 2 parts)))))) |
40785 | 415 |
416 (defun math-dt-to-date (dt) | |
417 (or (integerp (nth 1 dt)) | |
418 (math-reject-arg (nth 1 dt) 'fixnump)) | |
419 (if (or (< (nth 1 dt) 1) (> (nth 1 dt) 12)) | |
420 (math-reject-arg (nth 1 dt) "Month value is out of range")) | |
421 (or (integerp (nth 2 dt)) | |
422 (math-reject-arg (nth 2 dt) 'fixnump)) | |
423 (if (or (< (nth 2 dt) 1) (> (nth 2 dt) 31)) | |
424 (math-reject-arg (nth 2 dt) "Day value is out of range")) | |
425 (let ((date (math-absolute-from-date (car dt) (nth 1 dt) (nth 2 dt)))) | |
426 (if (nth 3 dt) | |
427 (math-add (math-float date) | |
428 (math-div (math-add (+ (* (nth 3 dt) 3600) | |
429 (* (nth 4 dt) 60)) | |
430 (nth 5 dt)) | |
431 '(float 864 2))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
432 date))) |
40785 | 433 |
434 (defun math-date-parts (value &optional offset) | |
435 (let* ((date (math-floor value)) | |
436 (time (math-round (math-mul (math-sub value (or offset date)) 86400) | |
437 (and (> calc-internal-prec 12) | |
438 (- calc-internal-prec 12)))) | |
439 (ftime (math-floor time))) | |
440 (list date | |
441 ftime | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
442 (math-sub time ftime)))) |
40785 | 443 |
444 | |
445 (defun math-this-year () | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
446 (string-to-int (substring (current-time-string) -4))) |
40785 | 447 |
448 (defun math-leap-year-p (year) | |
449 (if (Math-lessp year 1752) | |
450 (if (math-negp year) | |
451 (= (math-imod (math-neg year) 4) 1) | |
452 (= (math-imod year 4) 0)) | |
453 (setq year (math-imod year 400)) | |
454 (or (and (= (% year 4) 0) (/= (% year 100) 0)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
455 (= year 0)))) |
40785 | 456 |
457 (defun math-days-in-month (year month) | |
458 (if (and (= month 2) (math-leap-year-p year)) | |
459 29 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
460 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month)))) |
40785 | 461 |
462 (defun math-day-number (year month day) | |
463 (let ((day-of-year (+ day (* 31 (1- month))))) | |
464 (if (> month 2) | |
465 (progn | |
466 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10))) | |
467 (if (math-leap-year-p year) | |
468 (setq day-of-year (1+ day-of-year))))) | |
469 (and (eq year 1752) | |
470 (or (> month 9) | |
471 (and (= month 9) (>= day 14))) | |
472 (setq day-of-year (- day-of-year 11))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
473 day-of-year)) |
40785 | 474 |
475 (defun math-absolute-from-date (year month day) | |
476 (if (eq year 0) (setq year -1)) | |
477 (let ((yearm1 (math-sub year 1))) | |
478 (math-sub (math-add (math-day-number year month day) | |
479 (math-add (math-mul 365 yearm1) | |
480 (if (math-posp year) | |
481 (math-quotient yearm1 4) | |
482 (math-sub 365 | |
483 (math-quotient (math-sub 3 year) | |
484 4))))) | |
485 (if (or (Math-lessp year 1753) | |
486 (and (eq year 1752) (<= month 9))) | |
487 1 | |
488 (let ((correction (math-mul (math-quotient yearm1 100) 3))) | |
489 (let ((res (math-idivmod correction 4))) | |
490 (math-add (if (= (cdr res) 0) | |
491 -1 | |
492 0) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
493 (car res)))))))) |
40785 | 494 |
495 | |
496 ;;; It is safe to redefine these in your .emacs file to use a different | |
497 ;;; language. | |
498 | |
499 (defvar math-long-weekday-names '( "Sunday" "Monday" "Tuesday" "Wednesday" | |
500 "Thursday" "Friday" "Saturday" )) | |
501 (defvar math-short-weekday-names '( "Sun" "Mon" "Tue" "Wed" | |
502 "Thu" "Fri" "Sat" )) | |
503 | |
504 (defvar math-long-month-names '( "January" "February" "March" "April" | |
505 "May" "June" "July" "August" | |
506 "September" "October" "November" "December" )) | |
507 (defvar math-short-month-names '( "Jan" "Feb" "Mar" "Apr" "May" "Jun" | |
508 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" )) | |
509 | |
510 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
511 (defvar math-format-date-cache nil) |
40785 | 512 (defun math-format-date (date) |
513 (if (eq (car-safe date) 'date) | |
514 (setq date (nth 1 date))) | |
515 (let ((entry (list date calc-internal-prec calc-date-format))) | |
516 (or (cdr (assoc entry math-format-date-cache)) | |
517 (let* ((dt nil) | |
518 (calc-group-digits nil) | |
519 (calc-leading-zeros nil) | |
520 (calc-number-radix 10) | |
521 year month day weekday hour minute second | |
522 (bc-flag nil) | |
523 (fmt (apply 'concat (mapcar 'math-format-date-part | |
524 calc-date-format)))) | |
525 (setq math-format-date-cache (cons (cons entry fmt) | |
526 math-format-date-cache)) | |
527 (and (setq dt (nthcdr 10 math-format-date-cache)) | |
528 (setcdr dt nil)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
529 fmt)))) |
40785 | 530 |
531 (defun math-format-date-part (x) | |
532 (cond ((stringp x) | |
533 x) | |
534 ((listp x) | |
535 (if (math-integerp date) | |
536 "" | |
537 (apply 'concat (mapcar 'math-format-date-part x)))) | |
538 ((eq x 'X) | |
539 "") | |
540 ((eq x 'N) | |
541 (math-format-number date)) | |
542 ((eq x 'n) | |
543 (math-format-number (math-floor date))) | |
544 ((eq x 'J) | |
545 (math-format-number (math-add date '(float (bigpos 235 214 17) -1)))) | |
546 ((eq x 'j) | |
547 (math-format-number (math-add (math-floor date) '(bigpos 424 721 1)))) | |
548 ((eq x 'U) | |
549 (math-format-number (nth 1 (math-date-parts date 719164)))) | |
550 ((progn | |
551 (or dt | |
552 (progn | |
553 (setq dt (math-date-to-dt date) | |
554 year (car dt) | |
555 month (nth 1 dt) | |
556 day (nth 2 dt) | |
557 weekday (math-mod (math-add (math-floor date) 6) 7) | |
558 hour (nth 3 dt) | |
559 minute (nth 4 dt) | |
560 second (nth 5 dt)) | |
561 (and (memq 'b calc-date-format) | |
562 (math-negp year) | |
563 (setq year (math-neg year) | |
564 bc-flag t)))) | |
565 (memq x '(Y YY BY))) | |
566 (if (and (integerp year) (> year 1940) (< year 2040)) | |
567 (format (cond ((eq x 'YY) "%02d") | |
568 ((eq x 'BYY) "%2d") | |
569 (t "%d")) | |
570 (% year 100)) | |
571 (if (and (natnump year) (< year 100)) | |
572 (format "+%d" year) | |
573 (math-format-number year)))) | |
574 ((eq x 'YYY) | |
575 (math-format-number year)) | |
576 ((eq x 'YYYY) | |
577 (if (and (natnump year) (< year 100)) | |
578 (format "+%d" year) | |
579 (math-format-number year))) | |
580 ((eq x 'b) "") | |
581 ((eq x 'aa) | |
582 (and (not bc-flag) "ad")) | |
583 ((eq x 'AA) | |
584 (and (not bc-flag) "AD")) | |
585 ((eq x 'aaa) | |
586 (and (not bc-flag) "ad ")) | |
587 ((eq x 'AAA) | |
588 (and (not bc-flag) "AD ")) | |
589 ((eq x 'aaaa) | |
590 (and (not bc-flag) "a.d.")) | |
591 ((eq x 'AAAA) | |
592 (and (not bc-flag) "A.D.")) | |
593 ((eq x 'bb) | |
594 (and bc-flag "bc")) | |
595 ((eq x 'BB) | |
596 (and bc-flag "BC")) | |
597 ((eq x 'bbb) | |
598 (and bc-flag " bc")) | |
599 ((eq x 'BBB) | |
600 (and bc-flag " BC")) | |
601 ((eq x 'bbbb) | |
602 (and bc-flag "b.c.")) | |
603 ((eq x 'BBBB) | |
604 (and bc-flag "B.C.")) | |
605 ((eq x 'M) | |
606 (format "%d" month)) | |
607 ((eq x 'MM) | |
608 (format "%02d" month)) | |
609 ((eq x 'BM) | |
610 (format "%2d" month)) | |
611 ((eq x 'mmm) | |
612 (downcase (nth (1- month) math-short-month-names))) | |
613 ((eq x 'Mmm) | |
614 (nth (1- month) math-short-month-names)) | |
615 ((eq x 'MMM) | |
616 (upcase (nth (1- month) math-short-month-names))) | |
617 ((eq x 'Mmmm) | |
618 (nth (1- month) math-long-month-names)) | |
619 ((eq x 'MMMM) | |
620 (upcase (nth (1- month) math-long-month-names))) | |
621 ((eq x 'D) | |
622 (format "%d" day)) | |
623 ((eq x 'DD) | |
624 (format "%02d" day)) | |
625 ((eq x 'BD) | |
626 (format "%2d" day)) | |
627 ((eq x 'W) | |
628 (format "%d" weekday)) | |
629 ((eq x 'www) | |
630 (downcase (nth weekday math-short-weekday-names))) | |
631 ((eq x 'Www) | |
632 (nth weekday math-short-weekday-names)) | |
633 ((eq x 'WWW) | |
634 (upcase (nth weekday math-short-weekday-names))) | |
635 ((eq x 'Wwww) | |
636 (nth weekday math-long-weekday-names)) | |
637 ((eq x 'WWWW) | |
638 (upcase (nth weekday math-long-weekday-names))) | |
639 ((eq x 'd) | |
640 (format "%d" (math-day-number year month day))) | |
641 ((eq x 'ddd) | |
642 (format "%03d" (math-day-number year month day))) | |
643 ((eq x 'bdd) | |
644 (format "%3d" (math-day-number year month day))) | |
645 ((eq x 'h) | |
646 (and hour (format "%d" hour))) | |
647 ((eq x 'hh) | |
648 (and hour (format "%02d" hour))) | |
649 ((eq x 'bh) | |
650 (and hour (format "%2d" hour))) | |
651 ((eq x 'H) | |
652 (and hour (format "%d" (1+ (% (+ hour 11) 12))))) | |
653 ((eq x 'HH) | |
654 (and hour (format "%02d" (1+ (% (+ hour 11) 12))))) | |
655 ((eq x 'BH) | |
656 (and hour (format "%2d" (1+ (% (+ hour 11) 12))))) | |
657 ((eq x 'p) | |
658 (and hour (if (< hour 12) "a" "p"))) | |
659 ((eq x 'P) | |
660 (and hour (if (< hour 12) "A" "P"))) | |
661 ((eq x 'pp) | |
662 (and hour (if (< hour 12) "am" "pm"))) | |
663 ((eq x 'PP) | |
664 (and hour (if (< hour 12) "AM" "PM"))) | |
665 ((eq x 'pppp) | |
666 (and hour (if (< hour 12) "a.m." "p.m."))) | |
667 ((eq x 'PPPP) | |
668 (and hour (if (< hour 12) "A.M." "P.M."))) | |
669 ((eq x 'm) | |
670 (and minute (format "%d" minute))) | |
671 ((eq x 'mm) | |
672 (and minute (format "%02d" minute))) | |
673 ((eq x 'bm) | |
674 (and minute (format "%2d" minute))) | |
675 ((eq x 'C) | |
676 (and second (not (math-zerop second)) | |
677 ":")) | |
678 ((memq x '(s ss bs SS BS)) | |
679 (and second | |
680 (not (and (memq x '(SS BS)) (math-zerop second))) | |
681 (if (integerp second) | |
682 (format (cond ((memq x '(ss SS)) "%02d") | |
683 ((memq x '(bs BS)) "%2d") | |
684 (t "%d")) | |
685 second) | |
686 (concat (if (Math-lessp second 10) | |
687 (cond ((memq x '(ss SS)) "0") | |
688 ((memq x '(bs BS)) " ") | |
689 (t "")) | |
690 "") | |
691 (let ((calc-float-format | |
692 (list 'fix (min (- 12 calc-internal-prec) | |
693 0)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
694 (math-format-number second)))))))) |
40785 | 695 |
696 | |
697 (defun math-parse-date (str) | |
698 (catch 'syntax | |
699 (or (math-parse-standard-date str t) | |
700 (math-parse-standard-date str nil) | |
701 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" str) | |
702 (list 'date (math-read-number (math-match-substring str 1)))) | |
703 (let ((case-fold-search t) | |
704 (year nil) (month nil) (day nil) (weekday nil) | |
705 (hour nil) (minute nil) (second nil) (bc-flag nil) | |
706 (a nil) (b nil) (c nil) (bigyear nil) temp) | |
707 | |
708 ;; Extract the time, if any. | |
709 (if (or (string-match "\\([0-9][0-9]?\\):\\([0-9][0-9]?\\)\\(:\\([0-9][0-9]?\\(\\.[0-9]+\\)?\\)\\)? *\\([ap]m?\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)?" str) | |
710 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]m?\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" str)) | |
711 (let ((ampm (math-match-substring str 6))) | |
712 (setq hour (string-to-int (math-match-substring str 1)) | |
713 minute (math-match-substring str 2) | |
714 second (math-match-substring str 4) | |
715 str (concat (substring str 0 (match-beginning 0)) | |
716 (substring str (match-end 0)))) | |
717 (if (equal minute "") | |
718 (setq minute 0) | |
719 (setq minute (string-to-int minute))) | |
720 (if (equal second "") | |
721 (setq second 0) | |
722 (setq second (math-read-number second))) | |
723 (if (equal ampm "") | |
724 (if (> hour 23) | |
725 (throw 'syntax "Hour value out of range")) | |
726 (setq ampm (upcase (aref ampm 0))) | |
727 (if (memq ampm '(?N ?M)) | |
728 (if (and (= hour 12) (= minute 0) (eq second 0)) | |
729 (if (eq ampm ?M) (setq hour 0)) | |
730 (throw 'syntax | |
731 "Time must be 12:00:00 in this context")) | |
732 (if (or (= hour 0) (> hour 12)) | |
733 (throw 'syntax "Hour value out of range")) | |
734 (if (eq (= ampm ?A) (= hour 12)) | |
735 (setq hour (% (+ hour 12) 24))))))) | |
736 | |
737 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign. | |
738 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" str) | |
739 (progn | |
740 (setq str (copy-sequence str)) | |
741 (aset str (match-beginning 1) ?\/))) | |
742 | |
743 ;; Extract obvious month or weekday names. | |
744 (if (string-match "[a-zA-Z]" str) | |
745 (progn | |
746 (setq month (math-parse-date-word math-long-month-names)) | |
747 (setq weekday (math-parse-date-word math-long-weekday-names)) | |
748 (or month (setq month | |
749 (math-parse-date-word math-short-month-names))) | |
750 (or weekday (math-parse-date-word math-short-weekday-names)) | |
751 (or hour | |
752 (if (setq temp (math-parse-date-word | |
753 '( "noon" "midnight" "mid" ))) | |
754 (setq hour (if (= temp 1) 12 0) minute 0 second 0))) | |
755 (or (math-parse-date-word '( "ad" "a.d." )) | |
756 (if (math-parse-date-word '( "bc" "b.c." )) | |
757 (setq bc-flag t))) | |
758 (if (string-match "[a-zA-Z]+" str) | |
759 (throw 'syntax (format "Bad word in date: \"%s\"" | |
760 (math-match-substring str 0)))))) | |
761 | |
762 ;; If there is a huge number other than the year, ignore it. | |
763 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" str) | |
764 (setq temp (concat (substring str 0 (match-beginning 0)) | |
765 (substring str (match-end 0)))) | |
766 (string-match "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp)) | |
767 (setq str temp)) | |
768 | |
769 ;; If there is a number with a sign or a large number, it is a year. | |
770 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" str) | |
771 (string-match "\\(0*[1-9][0-9][0-9]+\\)" str)) | |
772 (setq year (math-match-substring str 1) | |
773 str (concat (substring str 0 (match-beginning 1)) | |
774 (substring str (match-end 1))) | |
775 year (math-read-number year) | |
776 bigyear t)) | |
777 | |
778 ;; Collect remaining numbers. | |
779 (setq temp 0) | |
780 (while (string-match "[0-9]+" str temp) | |
781 (and c (throw 'syntax "Too many numbers in date")) | |
782 (setq c (string-to-int (math-match-substring str 0))) | |
783 (or b (setq b c c nil)) | |
784 (or a (setq a b b nil)) | |
785 (setq temp (match-end 0))) | |
786 | |
787 ;; Check that we have the right amount of information. | |
788 (setq temp (+ (if year 1 0) (if month 1 0) (if day 1 0) | |
789 (if a 1 0) (if b 1 0) (if c 1 0))) | |
790 (if (> temp 3) | |
791 (throw 'syntax "Too many numbers in date") | |
792 (if (or (< temp 2) (and year (= temp 2))) | |
793 (throw 'syntax "Not enough numbers in date") | |
794 (if (= temp 2) ; if year omitted, assume current year | |
795 (setq year (math-this-year))))) | |
796 | |
797 ;; A large number must be a year. | |
798 (or year | |
799 (if (and a (or (> a 31) (< a 1))) | |
800 (setq year a a b b c c nil) | |
801 (if (and b (or (> b 31) (< b 1))) | |
802 (setq year b b c c nil) | |
803 (if (and c (or (> c 31) (< c 1))) | |
804 (setq year c c nil))))) | |
805 | |
806 ;; A medium-large number must be a day. | |
807 (if year | |
808 (if (and a (> a 12)) | |
809 (setq day a a b b c c nil) | |
810 (if (and b (> b 12)) | |
811 (setq day b b c c nil) | |
812 (if (and c (> c 12)) | |
813 (setq day c c nil))))) | |
814 | |
815 ;; We may know enough to sort it out now. | |
816 (if (and year day) | |
817 (or month (setq month a)) | |
818 (if (and year month) | |
819 (setq day a) | |
820 | |
821 ;; Interpret order of numbers as same as for display format. | |
822 (setq temp calc-date-format) | |
823 (while temp | |
824 (cond ((not (symbolp (car temp)))) | |
825 ((memq (car temp) '(Y YY BY YYY YYYY)) | |
826 (or year (setq year a a b b c))) | |
827 ((memq (car temp) '(M MM BM mmm Mmm Mmmm MMM MMMM)) | |
828 (or month (setq month a a b b c))) | |
829 ((memq (car temp) '(D DD BD)) | |
830 (or day (setq day a a b b c)))) | |
831 (setq temp (cdr temp))) | |
832 | |
833 ;; If display format was not complete, assume American style. | |
834 (or month (setq month a a b b c)) | |
835 (or day (setq day a a b b c)) | |
836 (or year (setq year a a b b c)))) | |
837 | |
838 (if bc-flag | |
839 (setq year (math-neg (math-abs year)))) | |
840 | |
841 (math-parse-date-validate year bigyear month day | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
842 hour minute second))))) |
40785 | 843 |
844 (defun math-parse-date-validate (year bigyear month day hour minute second) | |
845 (and (not bigyear) (natnump year) (< year 100) | |
846 (setq year (+ year (if (< year 40) 2000 1900)))) | |
847 (if (eq year 0) | |
848 (throw 'syntax "Year value is out of range")) | |
849 (if (or (< month 1) (> month 12)) | |
850 (throw 'syntax "Month value is out of range")) | |
851 (if (or (< day 1) (> day (math-days-in-month year month))) | |
852 (throw 'syntax "Day value is out of range")) | |
853 (and hour | |
854 (progn | |
855 (if (or (< hour 0) (> hour 23)) | |
856 (throw 'syntax "Hour value is out of range")) | |
857 (if (or (< minute 0) (> minute 59)) | |
858 (throw 'syntax "Minute value is out of range")) | |
859 (if (or (math-negp second) (not (Math-lessp second 60))) | |
860 (throw 'syntax "Seconds value is out of range")))) | |
861 (list 'date (math-dt-to-date (append (list year month day) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
862 (and hour (list hour minute second)))))) |
40785 | 863 |
864 (defun math-parse-date-word (names &optional front) | |
865 (let ((n 1)) | |
866 (while (and names (not (string-match (if (equal (car names) "Sep") | |
867 "Sept?" | |
868 (regexp-quote (car names))) | |
869 str))) | |
870 (setq names (cdr names) | |
871 n (1+ n))) | |
872 (and names | |
873 (or (not front) (= (match-beginning 0) 0)) | |
874 (progn | |
875 (setq str (concat (substring str 0 (match-beginning 0)) | |
876 (if front "" " ") | |
877 (substring str (match-end 0)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
878 n)))) |
40785 | 879 |
880 (defun math-parse-standard-date (str with-time) | |
881 (let ((case-fold-search t) | |
882 (okay t) num | |
883 (fmt calc-date-format) this next (gnext nil) | |
884 (year nil) (month nil) (day nil) (bigyear nil) (yearday nil) | |
885 (hour nil) (minute nil) (second nil) (bc-flag nil)) | |
886 (while (and fmt okay) | |
887 (setq this (car fmt) | |
888 fmt (setq fmt (or (cdr fmt) | |
889 (prog1 | |
890 gnext | |
891 (setq gnext nil)))) | |
892 next (car fmt)) | |
893 (if (consp next) (setq next (car next))) | |
894 (or (cond ((listp this) | |
895 (or (not with-time) | |
896 (not this) | |
897 (setq gnext fmt | |
898 fmt this))) | |
899 ((stringp this) | |
900 (if (and (<= (length this) (length str)) | |
901 (equal this | |
902 (substring str 0 (length this)))) | |
903 (setq str (substring str (length this))))) | |
904 ((eq this 'X) | |
905 t) | |
906 ((memq this '(n N j J)) | |
907 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" str) | |
908 (setq num (math-match-substring str 0) | |
909 str (substring str (match-end 0)) | |
910 num (math-date-to-dt (math-read-number num)) | |
911 num (math-sub num | |
912 (if (memq this '(n N)) | |
913 0 | |
914 (if (or (eq this 'j) | |
915 (math-integerp num)) | |
916 '(bigpos 424 721 1) | |
917 '(float (bigpos 235 214 17) | |
918 -1)))) | |
919 hour (or (nth 3 num) hour) | |
920 minute (or (nth 4 num) minute) | |
921 second (or (nth 5 num) second) | |
922 year (car num) | |
923 month (nth 1 num) | |
924 day (nth 2 num)))) | |
925 ((eq this 'U) | |
926 (and (string-match "\\`[-+]?[0-9]+" str) | |
927 (setq num (math-match-substring str 0) | |
928 str (substring str (match-end 0)) | |
929 num (math-date-to-dt | |
930 (math-add 719164 | |
931 (math-div (math-read-number num) | |
932 '(float 864 2)))) | |
933 hour (nth 3 num) | |
934 minute (nth 4 num) | |
935 second (nth 5 num) | |
936 year (car num) | |
937 month (nth 1 num) | |
938 day (nth 2 num)))) | |
939 ((memq this '(mmm Mmm MMM)) | |
940 (setq month (math-parse-date-word math-short-month-names t))) | |
941 ((memq this '(Mmmm MMMM)) | |
942 (setq month (math-parse-date-word math-long-month-names t))) | |
943 ((memq this '(www Www WWW)) | |
944 (math-parse-date-word math-short-weekday-names t)) | |
945 ((memq this '(Wwww WWWW)) | |
946 (math-parse-date-word math-long-weekday-names t)) | |
947 ((memq this '(p P)) | |
948 (if (string-match "\\`a" str) | |
949 (setq hour (if (= hour 12) 0 hour) | |
950 str (substring str 1)) | |
951 (if (string-match "\\`p" str) | |
952 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24)) | |
953 str (substring str 1))))) | |
954 ((memq this '(pp PP pppp PPPP)) | |
955 (if (string-match "\\`am\\|a\\.m\\." str) | |
956 (setq hour (if (= hour 12) 0 hour) | |
957 str (substring str (match-end 0))) | |
958 (if (string-match "\\`pm\\|p\\.m\\." str) | |
959 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24)) | |
960 str (substring str (match-end 0)))))) | |
961 ((memq this '(Y YY BY YYY YYYY)) | |
962 (and (if (memq next '(MM DD ddd hh HH mm ss SS)) | |
963 (if (memq this '(Y YY BYY)) | |
964 (string-match "\\` *[0-9][0-9]" str) | |
965 (string-match "\\`[0-9][0-9][0-9][0-9]" str)) | |
966 (string-match "\\`[-+]?[0-9]+" str)) | |
967 (setq year (math-match-substring str 0) | |
968 bigyear (or (eq this 'YYY) | |
969 (memq (aref str 0) '(?\+ ?\-))) | |
970 str (substring str (match-end 0)) | |
971 year (math-read-number year)))) | |
972 ((eq this 'b) | |
973 t) | |
974 ((memq this '(aa AA aaaa AAAA)) | |
975 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" str) | |
976 (setq str (substring str (match-end 0))))) | |
977 ((memq this '(aaa AAA)) | |
978 (if (string-match "\\` *ad *" str) | |
979 (setq str (substring str (match-end 0))))) | |
980 ((memq this '(bb BB bbb BBB bbbb BBBB)) | |
981 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" str) | |
982 (setq str (substring str (match-end 0)) | |
983 bc-flag t))) | |
984 ((memq this '(s ss bs SS BS)) | |
985 (and (if (memq next '(YY YYYY MM DD hh HH mm)) | |
986 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" str) | |
987 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" str)) | |
988 (setq second (math-match-substring str 0) | |
989 str (substring str (match-end 0)) | |
990 second (math-read-number second)))) | |
991 ((eq this 'C) | |
992 (if (string-match "\\`:[0-9][0-9]" str) | |
993 (setq str (substring str 1)) | |
994 t)) | |
995 ((or (not (if (and (memq this '(ddd MM DD hh HH mm)) | |
996 (memq next '(YY YYYY MM DD ddd | |
997 hh HH mm ss SS))) | |
998 (if (eq this 'ddd) | |
999 (string-match "\\` *[0-9][0-9][0-9]" str) | |
1000 (string-match "\\` *[0-9][0-9]" str)) | |
1001 (string-match "\\` *[0-9]+" str))) | |
1002 (and (setq num (string-to-int | |
1003 (math-match-substring str 0)) | |
1004 str (substring str (match-end 0))) | |
1005 nil)) | |
1006 nil) | |
1007 ((eq this 'W) | |
1008 (and (>= num 0) (< num 7))) | |
1009 ((memq this '(d ddd bdd)) | |
1010 (setq yearday num)) | |
1011 ((memq this '(M MM BM)) | |
1012 (setq month num)) | |
1013 ((memq this '(D DD BD)) | |
1014 (setq day num)) | |
1015 ((memq this '(h hh bh H HH BH)) | |
1016 (setq hour num)) | |
1017 ((memq this '(m mm bm)) | |
1018 (setq minute num))) | |
1019 (setq okay nil))) | |
1020 (if yearday | |
1021 (if (and month day) | |
1022 (setq yearday nil) | |
1023 (setq month 1 day 1))) | |
1024 (if (and okay (equal str "")) | |
1025 (and month day (or (not (or hour minute second)) | |
1026 (and hour minute)) | |
1027 (progn | |
1028 (or year (setq year (math-this-year))) | |
1029 (or second (setq second 0)) | |
1030 (if bc-flag | |
1031 (setq year (math-neg (math-abs year)))) | |
1032 (setq day (math-parse-date-validate year bigyear month day | |
1033 hour minute second)) | |
1034 (if yearday | |
1035 (setq day (math-add day (1- yearday)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1036 day))))) |
40785 | 1037 |
1038 | |
1039 (defun calcFunc-now (&optional zone) | |
1040 (let ((date (let ((calc-date-format nil)) | |
1041 (math-parse-date (current-time-string))))) | |
1042 (if (consp date) | |
1043 (if zone | |
1044 (math-add date (math-div (math-sub (calcFunc-tzone nil date) | |
1045 (calcFunc-tzone zone date)) | |
1046 '(float 864 2))) | |
1047 date) | |
1048 (calc-record-why "*Unable to interpret current date from system") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1049 (append (list 'calcFunc-now) (and zone (list zone)))))) |
40785 | 1050 |
1051 (defun calcFunc-year (date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1052 (car (math-date-to-dt date))) |
40785 | 1053 |
1054 (defun calcFunc-month (date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1055 (nth 1 (math-date-to-dt date))) |
40785 | 1056 |
1057 (defun calcFunc-day (date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1058 (nth 2 (math-date-to-dt date))) |
40785 | 1059 |
1060 (defun calcFunc-weekday (date) | |
1061 (if (eq (car-safe date) 'date) | |
1062 (setq date (nth 1 date))) | |
1063 (or (math-realp date) | |
1064 (math-reject-arg date 'datep)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1065 (math-mod (math-add (math-floor date) 6) 7)) |
40785 | 1066 |
1067 (defun calcFunc-yearday (date) | |
1068 (let ((dt (math-date-to-dt date))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1069 (math-day-number (car dt) (nth 1 dt) (nth 2 dt)))) |
40785 | 1070 |
1071 (defun calcFunc-hour (date) | |
1072 (if (eq (car-safe date) 'hms) | |
1073 (nth 1 date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1074 (or (nth 3 (math-date-to-dt date)) 0))) |
40785 | 1075 |
1076 (defun calcFunc-minute (date) | |
1077 (if (eq (car-safe date) 'hms) | |
1078 (nth 2 date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1079 (or (nth 4 (math-date-to-dt date)) 0))) |
40785 | 1080 |
1081 (defun calcFunc-second (date) | |
1082 (if (eq (car-safe date) 'hms) | |
1083 (nth 3 date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1084 (or (nth 5 (math-date-to-dt date)) 0))) |
40785 | 1085 |
1086 (defun calcFunc-time (date) | |
1087 (let ((dt (math-date-to-dt date))) | |
1088 (if (nth 3 dt) | |
1089 (cons 'hms (nthcdr 3 dt)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1090 (list 'hms 0 0 0)))) |
40785 | 1091 |
1092 (defun calcFunc-date (date &optional month day hour minute second) | |
1093 (and (math-messy-integerp month) (setq month (math-trunc month))) | |
1094 (and month (not (integerp month)) (math-reject-arg month 'fixnump)) | |
1095 (and (math-messy-integerp day) (setq day (math-trunc day))) | |
1096 (and day (not (integerp day)) (math-reject-arg day 'fixnump)) | |
1097 (if (and (eq (car-safe hour) 'hms) (not minute)) | |
1098 (setq second (nth 3 hour) | |
1099 minute (nth 2 hour) | |
1100 hour (nth 1 hour))) | |
1101 (and (math-messy-integerp hour) (setq hour (math-trunc hour))) | |
1102 (and hour (not (integerp hour)) (math-reject-arg hour 'fixnump)) | |
1103 (and (math-messy-integerp minute) (setq minute (math-trunc minute))) | |
1104 (and minute (not (integerp minute)) (math-reject-arg minute 'fixnump)) | |
1105 (and (math-messy-integerp second) (setq second (math-trunc second))) | |
1106 (and second (not (math-realp second)) (math-reject-arg second 'realp)) | |
1107 (if month | |
1108 (progn | |
1109 (and (math-messy-integerp date) (setq date (math-trunc date))) | |
1110 (and date (not (math-integerp date)) (math-reject-arg date 'integerp)) | |
1111 (if day | |
1112 (if hour | |
1113 (list 'date (math-dt-to-date (list date month day hour | |
1114 (or minute 0) | |
1115 (or second 0)))) | |
1116 (list 'date (math-dt-to-date (list date month day)))) | |
1117 (list 'date (math-dt-to-date (list (math-this-year) date month))))) | |
1118 (if (math-realp date) | |
1119 (list 'date date) | |
1120 (if (eq (car date) 'date) | |
1121 (nth 1 date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1122 (math-reject-arg date 'datep))))) |
40785 | 1123 |
1124 (defun calcFunc-julian (date &optional zone) | |
1125 (if (math-realp date) | |
1126 (list 'date (if (math-integerp date) | |
1127 (math-sub date '(bigpos 424 721 1)) | |
1128 (setq date (math-sub date '(float (bigpos 235 214 17) -1))) | |
1129 (math-sub date (math-div (calcFunc-tzone zone date) | |
1130 '(float 864 2))))) | |
1131 (if (eq (car date) 'date) | |
1132 (math-add (nth 1 date) (if (math-integerp (nth 1 date)) | |
1133 '(bigpos 424 721 1) | |
1134 (math-add '(float (bigpos 235 214 17) -1) | |
1135 (math-div (calcFunc-tzone zone date) | |
1136 '(float 864 2))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1137 (math-reject-arg date 'datep)))) |
40785 | 1138 |
1139 (defun calcFunc-unixtime (date &optional zone) | |
1140 (if (math-realp date) | |
1141 (progn | |
1142 (setq date (math-add 719164 (math-div date '(float 864 2)))) | |
1143 (list 'date (math-sub date (math-div (calcFunc-tzone zone date) | |
1144 '(float 864 2))))) | |
1145 (if (eq (car date) 'date) | |
1146 (math-add (nth 1 (math-date-parts (nth 1 date) 719164)) | |
1147 (calcFunc-tzone zone date)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1148 (math-reject-arg date 'datep)))) |
40785 | 1149 |
1150 (defun calcFunc-tzone (&optional zone date) | |
1151 (if zone | |
1152 (cond ((math-realp zone) | |
1153 (math-round (math-mul zone 3600))) | |
1154 ((eq (car zone) 'hms) | |
1155 (math-round (math-mul (math-from-hms zone 'deg) 3600))) | |
1156 ((eq (car zone) '+) | |
1157 (math-add (calcFunc-tzone (nth 1 zone) date) | |
1158 (calcFunc-tzone (nth 2 zone) date))) | |
1159 ((eq (car zone) '-) | |
1160 (math-sub (calcFunc-tzone (nth 1 zone) date) | |
1161 (calcFunc-tzone (nth 2 zone) date))) | |
1162 ((eq (car zone) 'var) | |
1163 (let ((name (upcase (symbol-name (nth 1 zone)))) | |
1164 found) | |
1165 (if (setq found (assoc name math-tzone-names)) | |
1166 (calcFunc-tzone (math-add (nth 1 found) | |
1167 (if (integerp (nth 2 found)) | |
1168 (nth 2 found) | |
1169 (or | |
1170 (math-daylight-savings-adjust | |
1171 date (car found)) | |
1172 0))) | |
1173 date) | |
1174 (if (equal name "LOCAL") | |
1175 (calcFunc-tzone nil date) | |
1176 (math-reject-arg zone "*Unrecognized time zone name"))))) | |
1177 (t (math-reject-arg zone "*Expected a time zone"))) | |
1178 (if (calc-var-value 'var-TimeZone) | |
1179 (calcFunc-tzone (calc-var-value 'var-TimeZone) date) | |
1180 (let ((p math-tzone-names) | |
1181 (offset 0) | |
1182 (tz '(var error var-error))) | |
1183 (save-excursion | |
1184 (set-buffer (get-buffer-create " *Calc Temporary*")) | |
1185 (erase-buffer) | |
1186 (call-process "date" nil t) | |
1187 (goto-char 1) | |
1188 (let ((case-fold-search t)) | |
1189 (while (and p (not (search-forward (car (car p)) nil t))) | |
1190 (setq p (cdr p)))) | |
1191 (if (looking-at "\\([-+][0-9]?[0-9]\\)\\([0-9][0-9]\\)?\\(\\'\\|[^0-9]\\)") | |
1192 (setq offset (math-add | |
1193 (string-to-int (buffer-substring | |
1194 (match-beginning 1) | |
1195 (match-end 1))) | |
1196 (if (match-beginning 2) | |
1197 (math-div (string-to-int (buffer-substring | |
1198 (match-beginning 2) | |
1199 (match-end 2))) | |
1200 60) | |
1201 0))))) | |
1202 (if p | |
1203 (progn | |
1204 (setq p (car p)) | |
1205 ;; Try to convert to a generalized time zone. | |
1206 (if (integerp (nth 2 p)) | |
1207 (let ((gen math-tzone-names)) | |
1208 (while (and gen | |
1209 (not (equal (nth 2 (car gen)) (car p))) | |
1210 (not (equal (nth 3 (car gen)) (car p))) | |
1211 (not (equal (nth 4 (car gen)) (car p))) | |
1212 (not (equal (nth 5 (car gen)) (car p)))) | |
1213 (setq gen (cdr gen))) | |
1214 (and gen | |
1215 (setq gen (car gen)) | |
1216 (equal (math-daylight-savings-adjust nil (car gen)) | |
1217 (nth 2 p)) | |
1218 (setq p gen)))) | |
1219 (setq tz (math-add (list 'var | |
1220 (intern (car p)) | |
1221 (intern (concat "var-" (car p)))) | |
1222 offset)))) | |
1223 (kill-buffer " *Calc Temporary*") | |
1224 (setq var-TimeZone tz) | |
1225 (calc-refresh-evaltos 'var-TimeZone) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1226 (calcFunc-tzone tz date))))) |
40785 | 1227 |
1228 ;;; Note: Longer names must appear before shorter names which are | |
1229 ;;; substrings of them. | |
1230 (defvar math-tzone-names | |
1231 '( ( "MEGT" -1 "MET" "METDST" ) ; Middle Europe | |
1232 ( "METDST" -1 -1 ) ( "MET" -1 0 ) | |
1233 ( "MEGZ" -1 "MEZ" "MESZ" ) ( "MEZ" -1 0 ) ( "MESZ" -1 -1 ) | |
1234 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe | |
1235 ( "WETDST" 0 -1 ) ( "WET" 0 0 ) | |
1236 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -1 ) ; Britain | |
1237 ( "NGT" (float 35 -1) "NST" "NDT" ) ; Newfoundland | |
1238 ( "NST" (float 35 -1) 0 ) ( "NDT" (float 35 -1) -1 ) | |
1239 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -1 ) ; Atlantic | |
1240 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -1 ) ; Eastern | |
1241 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -1 ) ; Central | |
1242 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -1 ) ; Mountain | |
1243 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -1 ) ; Pacific | |
1244 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -1 ) ; Yukon | |
1245 )) | |
1246 | |
1247 | |
1248 (defun math-daylight-savings-adjust (date zone &optional dt) | |
1249 (or date (setq date (nth 1 (calcFunc-now)))) | |
1250 (let (bump) | |
1251 (if (eq (car-safe date) 'date) | |
1252 (setq bump 0 | |
1253 date (nth 1 date)) | |
1254 (if (and date (math-realp date)) | |
1255 (let ((zadj (assoc zone math-tzone-names))) | |
1256 (if zadj (setq bump -1 | |
1257 date (math-sub date (math-div (nth 1 zadj) | |
1258 '(float 24 0)))))) | |
1259 (math-reject-arg date 'datep))) | |
1260 (setq date (math-float date)) | |
1261 (or dt (setq dt (math-date-to-dt date))) | |
1262 (and math-daylight-savings-hook | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1263 (funcall math-daylight-savings-hook date dt zone bump)))) |
40785 | 1264 |
1265 (defun calcFunc-dsadj (date &optional zone) | |
1266 (if zone | |
1267 (or (eq (car-safe zone) 'var) | |
1268 (math-reject-arg zone "*Time zone variable expected")) | |
1269 (setq zone (or (calc-var-value 'var-TimeZone) | |
1270 (progn | |
1271 (calcFunc-tzone) | |
1272 (calc-var-value 'var-TimeZone))))) | |
1273 (setq zone (and (eq (car-safe zone) 'var) | |
1274 (upcase (symbol-name (nth 1 zone))))) | |
1275 (let ((zadj (assoc zone math-tzone-names))) | |
1276 (or zadj (math-reject-arg zone "*Unrecognized time zone name")) | |
1277 (if (integerp (nth 2 zadj)) | |
1278 (nth 2 zadj) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1279 (math-daylight-savings-adjust date zone)))) |
40785 | 1280 |
1281 (defun calcFunc-tzconv (date z1 z2) | |
1282 (if (math-realp date) | |
1283 (nth 1 (calcFunc-tzconv (list 'date date) z1 z2)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1284 (calcFunc-unixtime (calcFunc-unixtime date z1) z2))) |
40785 | 1285 |
1286 (defvar math-daylight-savings-hook 'math-std-daylight-savings) | |
1287 | |
1288 (defun math-std-daylight-savings (date dt zone bump) | |
1289 "Standard North American daylight savings algorithm. | |
1290 This implements the rules for the U.S. and Canada as of 1987. | |
1291 Daylight savings begins on the first Sunday of April at 2 a.m., | |
1292 and ends on the last Sunday of October at 2 a.m." | |
1293 (cond ((< (nth 1 dt) 4) 0) | |
1294 ((= (nth 1 dt) 4) | |
1295 (let ((sunday (math-prev-weekday-in-month date dt 7 0))) | |
1296 (cond ((< (nth 2 dt) sunday) 0) | |
1297 ((= (nth 2 dt) sunday) | |
1298 (if (>= (nth 3 dt) (+ 3 bump)) -1 0)) | |
1299 (t -1)))) | |
1300 ((< (nth 1 dt) 10) -1) | |
1301 ((= (nth 1 dt) 10) | |
1302 (let ((sunday (math-prev-weekday-in-month date dt 31 0))) | |
1303 (cond ((< (nth 2 dt) sunday) -1) | |
1304 ((= (nth 2 dt) sunday) | |
1305 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1)) | |
1306 (t 0)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1307 (t 0))) |
40785 | 1308 |
1309 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given | |
1310 ;;; day of the given month. | |
1311 (defun math-prev-weekday-in-month (date dt day wday) | |
1312 (or day (setq day (nth 2 dt))) | |
1313 (if (> day (math-days-in-month (car dt) (nth 1 dt))) | |
1314 (setq day (math-days-in-month (car dt) (nth 1 dt)))) | |
1315 (let ((zeroth (math-sub (math-floor date) (nth 2 dt)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1316 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day))) zeroth))) |
40785 | 1317 |
1318 (defun calcFunc-pwday (date &optional day weekday) | |
1319 (if (eq (car-safe date) 'date) | |
1320 (setq date (nth 1 date))) | |
1321 (or (math-realp date) | |
1322 (math-reject-arg date 'datep)) | |
1323 (if (math-messy-integerp day) (setq day (math-trunc day))) | |
1324 (or (integerp day) (math-reject-arg day 'fixnump)) | |
1325 (if (= day 0) (setq day 31)) | |
1326 (and (or (< day 7) (> day 31)) (math-reject-arg day 'range)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1327 (math-prev-weekday-in-month date (math-date-to-dt date) day (or weekday 0))) |
40785 | 1328 |
1329 | |
1330 (defun calcFunc-newweek (date &optional weekday) | |
1331 (if (eq (car-safe date) 'date) | |
1332 (setq date (nth 1 date))) | |
1333 (or (math-realp date) | |
1334 (math-reject-arg date 'datep)) | |
1335 (or weekday (setq weekday 0)) | |
1336 (and (math-messy-integerp weekday) (setq weekday (math-trunc weekday))) | |
1337 (or (integerp weekday) (math-reject-arg weekday 'fixnump)) | |
1338 (and (or (< weekday 0) (> weekday 6)) (math-reject-arg weekday 'range)) | |
1339 (setq date (math-floor date)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1340 (list 'date (math-sub date (calcFunc-weekday (math-sub date weekday))))) |
40785 | 1341 |
1342 (defun calcFunc-newmonth (date &optional day) | |
1343 (or day (setq day 1)) | |
1344 (and (math-messy-integerp day) (setq day (math-trunc day))) | |
1345 (or (integerp day) (math-reject-arg day 'fixnump)) | |
1346 (and (or (< day 0) (> day 31)) (math-reject-arg day 'range)) | |
1347 (let ((dt (math-date-to-dt date))) | |
1348 (if (or (= day 0) (> day (math-days-in-month (car dt) (nth 1 dt)))) | |
1349 (setq day (math-days-in-month (car dt) (nth 1 dt)))) | |
1350 (and (eq (car dt) 1752) (= (nth 1 dt) 9) | |
1351 (if (>= day 14) (setq day (- day 11)))) | |
1352 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1353 (1- day))))) |
40785 | 1354 |
1355 (defun calcFunc-newyear (date &optional day) | |
1356 (or day (setq day 1)) | |
1357 (and (math-messy-integerp day) (setq day (math-trunc day))) | |
1358 (or (integerp day) (math-reject-arg day 'fixnump)) | |
1359 (let ((dt (math-date-to-dt date))) | |
1360 (if (and (>= day 0) (<= day 366)) | |
1361 (let ((max (if (eq (car dt) 1752) 355 | |
1362 (if (math-leap-year-p (car dt)) 366 365)))) | |
1363 (if (or (= day 0) (> day max)) (setq day max)) | |
1364 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1)) | |
1365 (1- day)))) | |
1366 (if (and (>= day -12) (<= day -1)) | |
1367 (list 'date (math-dt-to-date (list (car dt) (- day) 1))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1368 (math-reject-arg day 'range))))) |
40785 | 1369 |
1370 (defun calcFunc-incmonth (date &optional step) | |
1371 (or step (setq step 1)) | |
1372 (and (math-messy-integerp step) (setq step (math-trunc step))) | |
1373 (or (math-integerp step) (math-reject-arg step 'integerp)) | |
1374 (let* ((dt (math-date-to-dt date)) | |
1375 (year (car dt)) | |
1376 (month (math-add (1- (nth 1 dt)) step)) | |
1377 (extra (calcFunc-idiv month 12)) | |
1378 (day (nth 2 dt))) | |
1379 (setq month (1+ (math-sub month (math-mul extra 12))) | |
1380 year (math-add year extra) | |
1381 day (min day (math-days-in-month year month))) | |
1382 (and (math-posp (car dt)) (not (math-posp year)) | |
1383 (setq year (math-sub year 1))) ; did we go past the year zero? | |
1384 (and (math-negp (car dt)) (not (math-negp year)) | |
1385 (setq year (math-add year 1))) | |
1386 (list 'date (math-dt-to-date | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1387 (cons year (cons month (cons day (cdr (cdr (cdr dt)))))))))) |
40785 | 1388 |
1389 (defun calcFunc-incyear (date &optional step) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1390 (calcFunc-incmonth date (math-mul (or step 1) 12))) |
40785 | 1391 |
1392 | |
1393 | |
1394 (defun calcFunc-bsub (a b) | |
1395 (or (eq (car-safe a) 'date) | |
1396 (math-reject-arg a 'datep)) | |
1397 (if (eq (car-safe b) 'date) | |
1398 (if (math-lessp (nth 1 a) (nth 1 b)) | |
1399 (math-neg (calcFunc-bsub b a)) | |
1400 (math-setup-holidays b) | |
1401 (let* ((da (math-to-business-day a)) | |
1402 (db (math-to-business-day b))) | |
1403 (math-add (math-sub (car da) (car db)) | |
1404 (if (and (cdr db) (not (cdr da))) 1 0)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1405 (calcFunc-badd a (math-neg b)))) |
40785 | 1406 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1407 (defvar math-holidays-cache nil) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1408 (defvar math-holidays-cache-tag t) |
40785 | 1409 (defun calcFunc-badd (a b) |
1410 (if (eq (car-safe b) 'date) | |
1411 (if (eq (car-safe a) 'date) | |
1412 (math-reject-arg nil "*Illegal combination in date arithmetic") | |
1413 (calcFunc-badd b a)) | |
1414 (if (eq (car-safe a) 'date) | |
1415 (if (Math-realp b) | |
1416 (if (Math-zerop b) | |
1417 a | |
1418 (let* ((d (math-to-business-day a)) | |
1419 (bb (math-add (car d) | |
1420 (if (and (cdr d) (Math-posp b)) | |
1421 (math-sub b 1) b)))) | |
1422 (or (math-from-business-day bb) | |
1423 (calcFunc-badd a b)))) | |
1424 (if (eq (car-safe b) 'hms) | |
1425 (let ((hours (nth 7 math-holidays-cache))) | |
1426 (setq b (math-div (math-from-hms b 'deg) 24)) | |
1427 (if hours | |
1428 (setq b (math-div b (cdr hours)))) | |
1429 (calcFunc-badd a b)) | |
1430 (math-reject-arg nil "*Illegal combination in date arithmetic"))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1431 (math-reject-arg a 'datep)))) |
40785 | 1432 |
1433 (defun calcFunc-holiday (a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1434 (if (cdr (math-to-business-day a)) 1 0)) |
40785 | 1435 |
1436 ;;; Compute the number of business days since Jan 1, 1 AD. | |
1437 | |
1438 (defun math-to-business-day (date &optional need-year) | |
1439 (if (eq (car-safe date) 'date) | |
1440 (setq date (nth 1 date))) | |
1441 (or (Math-realp date) | |
1442 (math-reject-arg date 'datep)) | |
1443 (let* ((day (math-floor date)) | |
1444 (time (math-sub date day)) | |
1445 (dt (math-date-to-dt day)) | |
1446 (delta 0) | |
1447 (holiday nil)) | |
1448 (or (not need-year) (eq (car dt) need-year) | |
1449 (math-reject-arg (list 'date day) "*Generated holiday has wrong year")) | |
1450 (math-setup-holidays date) | |
1451 (let ((days (car math-holidays-cache))) | |
1452 (while (and (setq days (cdr days)) (< (car days) day)) | |
1453 (setq delta (1+ delta))) | |
1454 (and days (= day (car days)) | |
1455 (setq holiday t))) | |
1456 (let* ((weekdays (nth 3 math-holidays-cache)) | |
1457 (weeks (1- (/ (+ day 6) 7))) | |
1458 (wkday (- day 1 (* weeks 7)))) | |
1459 (setq delta (+ delta (* weeks (length weekdays)))) | |
1460 (while (and weekdays (< (car weekdays) wkday)) | |
1461 (setq weekdays (cdr weekdays) | |
1462 delta (1+ delta))) | |
1463 (and weekdays (eq wkday (car weekdays)) | |
1464 (setq holiday t))) | |
1465 (let ((hours (nth 7 math-holidays-cache))) | |
1466 (if hours | |
1467 (progn | |
1468 (setq time (math-div (math-sub time (car hours)) (cdr hours))) | |
1469 (if (Math-lessp time 0) (setq time 0)) | |
1470 (or (Math-lessp time 1) | |
1471 (setq time | |
1472 (math-sub 1 | |
1473 (math-div 1 (math-mul 86400 (cdr hours))))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1474 (cons (math-add (math-sub day delta) time) holiday))) |
40785 | 1475 |
1476 | |
1477 ;;; Compute the date a certain number of business days since Jan 1, 1 AD. | |
42206 | 1478 ;;; If this returns nil, holiday table was adjusted; redo calculation. |
40785 | 1479 |
1480 (defun math-from-business-day (num) | |
1481 (let* ((day (math-floor num)) | |
1482 (time (math-sub num day))) | |
1483 (or (integerp day) | |
1484 (math-reject-arg nil "*Date is outside valid range")) | |
1485 (math-setup-holidays) | |
1486 (let ((days (nth 1 math-holidays-cache)) | |
1487 (delta 0)) | |
1488 (while (and (setq days (cdr days)) (< (car days) day)) | |
1489 (setq delta (1+ delta))) | |
1490 (setq day (+ day delta))) | |
1491 (let* ((weekdays (nth 3 math-holidays-cache)) | |
1492 (bweek (- 7 (length weekdays))) | |
1493 (weeks (1- (/ (+ day (1- bweek)) bweek))) | |
1494 (wkday (- day 1 (* weeks bweek))) | |
1495 (w 0)) | |
1496 (setq day (+ day (* weeks (length weekdays)))) | |
1497 (while (if (memq w weekdays) | |
1498 (setq day (1+ day)) | |
1499 (> (setq wkday (1- wkday)) 0)) | |
1500 (setq w (1+ w))) | |
1501 (let ((hours (nth 7 math-holidays-cache))) | |
1502 (if hours | |
1503 (setq time (math-add (math-mul time (cdr hours)) (car hours))))) | |
1504 (and (not (math-setup-holidays day)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1505 (list 'date (math-add day time)))))) |
40785 | 1506 |
1507 | |
1508 (defun math-setup-holidays (&optional date) | |
1509 (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag) | |
1510 (let ((h (calc-var-value 'var-Holidays)) | |
1511 (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3) | |
1512 (thu . 4) (fri . 5) (sat . 6) )) | |
1513 (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil)) | |
1514 (or (math-vectorp h) | |
1515 (math-reject-arg h "*Holidays variable must be a vector")) | |
1516 (while (setq h (cdr h)) | |
1517 (cond ((or (and (eq (car-safe (car h)) 'date) | |
1518 (integerp (nth 1 (car h)))) | |
1519 (and (eq (car-safe (car h)) 'intv) | |
1520 (eq (car-safe (nth 2 (car h))) 'date)) | |
1521 (eq (car-safe (car h)) 'vec)) | |
1522 (setq days (cons (car h) days))) | |
1523 ((and (eq (car-safe (car h)) 'var) | |
1524 (assq (nth 1 (car h)) wdnames)) | |
1525 (setq weekdays (cons (cdr (assq (nth 1 (car h)) wdnames)) | |
1526 weekdays))) | |
1527 ((and (eq (car-safe (car h)) 'intv) | |
1528 (eq (car-safe (nth 2 (car h))) 'hms) | |
1529 (eq (car-safe (nth 3 (car h))) 'hms)) | |
1530 (if hours | |
1531 (math-reject-arg | |
1532 (car h) "*Only one hours interval allowed in Holidays")) | |
1533 (setq hours (math-div (car h) '(hms 24 0 0))) | |
1534 (if (or (Math-lessp (nth 2 hours) 0) | |
1535 (Math-lessp 1 (nth 3 hours))) | |
1536 (math-reject-arg | |
1537 (car h) "*Hours interval out of range")) | |
1538 (setq hours (cons (nth 2 hours) | |
1539 (math-sub (nth 3 hours) (nth 2 hours)))) | |
1540 (if (Math-zerop (cdr hours)) | |
1541 (math-reject-arg | |
1542 (car h) "*Degenerate hours interval"))) | |
1543 ((or (and (eq (car-safe (car h)) 'intv) | |
1544 (Math-integerp (nth 2 (car h))) | |
1545 (Math-integerp (nth 3 (car h)))) | |
1546 (and (integerp (car h)) | |
1547 (> (car h) 1900) (< (car h) 2100))) | |
1548 (if limit | |
1549 (math-reject-arg | |
1550 (car h) "*Only one limit allowed in Holidays")) | |
1551 (setq limit (calcFunc-vint (car h) '(intv 3 1 2737))) | |
1552 (if (equal limit '(vec)) | |
1553 (math-reject-arg (car h) "*Limit is out of range"))) | |
1554 ((or (math-expr-contains (car h) '(var y var-y)) | |
1555 (math-expr-contains (car h) '(var m var-m))) | |
1556 (setq exprs (cons (car h) exprs))) | |
1557 (t (math-reject-arg | |
1558 (car h) "*Holidays must contain a vector of holidays")))) | |
1559 (if (= (length weekdays) 7) | |
1560 (math-reject-arg nil "*Too many weekend days")) | |
1561 (setq math-holidays-cache (list (list -1) ; 0: days list | |
1562 (list -1) ; 1: inverse-days list | |
1563 nil ; 2: exprs | |
1564 (sort weekdays '<) | |
1565 (or limit '(intv 3 1 2737)) | |
1566 nil ; 5: (lo.hi) expanded years | |
1567 (cons exprs days) | |
1568 hours) ; 7: business hours | |
1569 math-holidays-cache-tag (calc-var-value 'var-Holidays)))) | |
1570 (if date | |
1571 (let ((year (calcFunc-year date)) | |
1572 (limits (nth 5 math-holidays-cache)) | |
1573 (done nil)) | |
1574 (or (eq (calcFunc-in year (nth 4 math-holidays-cache)) 1) | |
1575 (progn | |
1576 (or (eq (car-safe date) 'date) (setq date (list 'date date))) | |
1577 (math-reject-arg date "*Date is outside valid range"))) | |
1578 (unwind-protect | |
1579 (let ((days (nth 6 math-holidays-cache))) | |
1580 (if days | |
1581 (let ((year nil)) ; see below | |
1582 (setcar (nthcdr 6 math-holidays-cache) nil) | |
1583 (math-setup-add-holidays (cons 'vec (cdr days))) | |
1584 (setcar (nthcdr 2 math-holidays-cache) (car days)))) | |
1585 (cond ((not (nth 2 math-holidays-cache)) | |
1586 (setq done t) | |
1587 nil) | |
1588 ((not limits) | |
1589 (setcar (nthcdr 5 math-holidays-cache) (cons year year)) | |
1590 (math-setup-year-holidays year) | |
1591 (setq done t)) | |
1592 ((< year (car limits)) | |
1593 (message "Computing holidays, %d .. %d" | |
1594 year (1- (car limits))) | |
1595 (calc-set-command-flag 'clear-message) | |
1596 (while (< year (car limits)) | |
1597 (setcar limits (1- (car limits))) | |
1598 (math-setup-year-holidays (car limits))) | |
1599 (setq done t)) | |
1600 ((> year (cdr limits)) | |
1601 (message "Computing holidays, %d .. %d" | |
1602 (1+ (cdr limits)) year) | |
1603 (calc-set-command-flag 'clear-message) | |
1604 (while (> year (cdr limits)) | |
1605 (setcdr limits (1+ (cdr limits))) | |
1606 (math-setup-year-holidays (cdr limits))) | |
1607 (setq done t)) | |
1608 (t | |
1609 (setq done t) | |
1610 nil))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1611 (or done (setq math-holidays-cache-tag t)))))) |
40785 | 1612 |
1613 (defun math-setup-year-holidays (year) | |
1614 (let ((exprs (nth 2 math-holidays-cache))) | |
1615 (while exprs | |
1616 (let* ((var-y year) | |
1617 (var-m nil) | |
1618 (expr (math-evaluate-expr (car exprs)))) | |
1619 (if (math-expr-contains expr '(var m var-m)) | |
1620 (let ((var-m 0)) | |
1621 (while (<= (setq var-m (1+ var-m)) 12) | |
1622 (math-setup-add-holidays (math-evaluate-expr expr)))) | |
1623 (math-setup-add-holidays expr))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1624 (setq exprs (cdr exprs))))) |
40785 | 1625 |
1626 (defun math-setup-add-holidays (days) ; uses "year" | |
1627 (cond ((eq (car-safe days) 'vec) | |
1628 (while (setq days (cdr days)) | |
1629 (math-setup-add-holidays (car days)))) | |
1630 ((eq (car-safe days) 'intv) | |
1631 (let ((day (math-ceiling (nth 2 days)))) | |
1632 (or (eq (calcFunc-in day days) 1) | |
1633 (setq day (math-add day 1))) | |
1634 (while (eq (calcFunc-in day days) 1) | |
1635 (math-setup-add-holidays day) | |
1636 (setq day (math-add day 1))))) | |
1637 ((eq (car-safe days) 'date) | |
1638 (math-setup-add-holidays (nth 1 days))) | |
1639 ((eq days 0)) | |
1640 ((integerp days) | |
1641 (let ((b (math-to-business-day days year))) | |
1642 (or (cdr b) ; don't register holidays twice! | |
1643 (let ((prev (car math-holidays-cache)) | |
1644 (iprev (nth 1 math-holidays-cache))) | |
1645 (while (and (cdr prev) (< (nth 1 prev) days)) | |
1646 (setq prev (cdr prev) iprev (cdr iprev))) | |
1647 (setcdr prev (cons days (cdr prev))) | |
1648 (setcdr iprev (cons (car b) (cdr iprev))) | |
1649 (while (setq iprev (cdr iprev)) | |
1650 (setcar iprev (1- (car iprev)))))))) | |
1651 ((Math-realp days) | |
1652 (math-reject-arg (list 'date days) "*Invalid holiday value")) | |
1653 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1654 (math-reject-arg days "*Holiday formula failed to evaluate")))) |
40785 | 1655 |
1656 | |
1657 | |
1658 | |
1659 ;;;; Error forms. | |
1660 | |
1661 ;;; Build a standard deviation form. [X X X] | |
1662 (defun math-make-sdev (x sigma) | |
1663 (if (memq (car-safe x) '(date mod sdev intv vec)) | |
1664 (math-reject-arg x 'realp)) | |
1665 (if (memq (car-safe sigma) '(date mod sdev intv vec)) | |
1666 (math-reject-arg sigma 'realp)) | |
1667 (if (or (Math-negp sigma) (memq (car-safe sigma) '(cplx polar))) | |
1668 (setq sigma (math-abs sigma))) | |
1669 (if (and (Math-zerop sigma) (Math-scalarp x)) | |
1670 x | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1671 (list 'sdev x sigma))) |
40785 | 1672 (defun calcFunc-sdev (x sigma) |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1673 (math-make-sdev x sigma)) |
40785 | 1674 |
1675 | |
1676 | |
1677 ;;;; Modulo forms. | |
1678 | |
1679 (defun math-normalize-mod (a) | |
1680 (let ((n (math-normalize (nth 1 a))) | |
1681 (m (math-normalize (nth 2 a)))) | |
1682 (if (and (math-anglep n) (math-anglep m) (math-posp m)) | |
1683 (math-make-mod n m) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1684 (math-normalize (list 'calcFunc-makemod n m))))) |
40785 | 1685 |
1686 ;;; Build a modulo form. [N R R] | |
1687 (defun math-make-mod (n m) | |
1688 (setq calc-previous-modulo m) | |
1689 (and n | |
1690 (cond ((not (Math-anglep m)) | |
1691 (math-reject-arg m 'anglep)) | |
1692 ((not (math-posp m)) | |
1693 (math-reject-arg m 'posp)) | |
1694 ((Math-anglep n) | |
1695 (if (or (Math-negp n) | |
1696 (not (Math-lessp n m))) | |
1697 (list 'mod (math-mod n m) m) | |
1698 (list 'mod n m))) | |
1699 ((memq (car n) '(+ - / vec neg)) | |
1700 (math-normalize | |
1701 (cons (car n) | |
1702 (mapcar (function (lambda (x) (math-make-mod x m))) | |
1703 (cdr n))))) | |
1704 ((and (eq (car n) '*) (Math-anglep (nth 1 n))) | |
1705 (math-mul (math-make-mod (nth 1 n) m) (nth 2 n))) | |
1706 ((memq (car n) '(* ^ var calcFunc-subscr)) | |
1707 (math-mul (math-make-mod 1 m) n)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1708 (t (math-reject-arg n 'anglep))))) |
40785 | 1709 (defun calcFunc-makemod (n m) |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1710 (math-make-mod n m)) |
40785 | 1711 |
1712 | |
1713 | |
1714 ;;;; Interval forms. | |
1715 | |
1716 ;;; Build an interval form. [X S X X] | |
1717 (defun math-make-intv (mask lo hi) | |
1718 (if (memq (car-safe lo) '(cplx polar mod sdev intv vec)) | |
1719 (math-reject-arg lo 'realp)) | |
1720 (if (memq (car-safe hi) '(cplx polar mod sdev intv vec)) | |
1721 (math-reject-arg hi 'realp)) | |
1722 (or (eq (eq (car-safe lo) 'date) (eq (car-safe hi) 'date)) | |
1723 (math-reject-arg (if (eq (car-safe lo) 'date) hi lo) 'datep)) | |
1724 (if (and (or (Math-realp lo) (eq (car lo) 'date)) | |
1725 (or (Math-realp hi) (eq (car hi) 'date))) | |
1726 (let ((cmp (math-compare lo hi))) | |
1727 (if (= cmp 0) | |
1728 (if (= mask 3) | |
1729 lo | |
1730 (list 'intv mask lo hi)) | |
1731 (if (> cmp 0) | |
1732 (if (= mask 3) | |
1733 (list 'intv 2 lo lo) | |
1734 (list 'intv mask lo lo)) | |
1735 (list 'intv mask lo hi)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1736 (list 'intv mask lo hi))) |
40785 | 1737 (defun calcFunc-intv (mask lo hi) |
1738 (if (math-messy-integerp mask) (setq mask (math-trunc mask))) | |
1739 (or (natnump mask) (math-reject-arg mask 'fixnatnump)) | |
1740 (or (<= mask 3) (math-reject-arg mask 'range)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1741 (math-make-intv mask lo hi)) |
40785 | 1742 |
1743 (defun math-sort-intv (mask lo hi) | |
1744 (if (Math-lessp hi lo) | |
1745 (math-make-intv (aref [0 2 1 3] mask) hi lo) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1746 (math-make-intv mask lo hi))) |
40785 | 1747 |
1748 | |
1749 | |
1750 | |
1751 (defun math-combine-intervals (a am b bm c cm d dm) | |
1752 (let (res) | |
1753 (if (= (setq res (math-compare a c)) 1) | |
1754 (setq a c am cm) | |
1755 (if (= res 0) | |
1756 (setq am (or am cm)))) | |
1757 (if (= (setq res (math-compare b d)) -1) | |
1758 (setq b d bm dm) | |
1759 (if (= res 0) | |
1760 (setq bm (or bm dm)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1761 (math-make-intv (+ (if am 2 0) (if bm 1 0)) a b))) |
40785 | 1762 |
1763 | |
1764 (defun math-div-mod (a b m) ; [R R R R] (Returns nil if no solution) | |
1765 (and (Math-integerp a) (Math-integerp b) (Math-integerp m) | |
1766 (let ((u1 1) (u3 b) (v1 0) (v3 m)) | |
1767 (while (not (eq v3 0)) ; See Knuth sec 4.5.2, exercise 15 | |
1768 (let* ((q (math-idivmod u3 v3)) | |
1769 (t1 (math-sub u1 (math-mul v1 (car q))))) | |
1770 (setq u1 v1 u3 v3 v1 t1 v3 (cdr q)))) | |
1771 (let ((q (math-idivmod a u3))) | |
1772 (and (eq (cdr q) 0) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1773 (math-mod (math-mul (car q) u1) m)))))) |
40785 | 1774 |
1775 (defun math-mod-intv (a b) | |
1776 (let* ((q1 (math-floor (math-div (nth 2 a) b))) | |
1777 (q2 (math-floor (math-div (nth 3 a) b))) | |
1778 (m1 (math-sub (nth 2 a) (math-mul q1 b))) | |
1779 (m2 (math-sub (nth 3 a) (math-mul q2 b)))) | |
1780 (cond ((equal q1 q2) | |
1781 (math-sort-intv (nth 1 a) m1 m2)) | |
1782 ((and (math-equal-int (math-sub q2 q1) 1) | |
1783 (math-zerop m2) | |
1784 (memq (nth 1 a) '(0 2))) | |
1785 (math-make-intv (nth 1 a) m1 b)) | |
1786 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1787 (math-make-intv 2 0 b))))) |
40785 | 1788 |
1789 | |
1790 (defun math-read-angle-brackets () | |
1791 (let* ((last (or (math-check-for-commas t) (length exp-str))) | |
1792 (str (substring exp-str exp-pos last)) | |
1793 (res | |
1794 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str) | |
1795 (let ((str1 (substring str 0 (1- (match-end 0)))) | |
1796 (str2 (substring str (match-end 0))) | |
1797 (calc-hashes-used 0)) | |
1798 (setq str1 (math-read-expr (concat "[" str1 "]"))) | |
1799 (if (eq (car-safe str1) 'error) | |
1800 str1 | |
1801 (setq str2 (math-read-expr str2)) | |
1802 (if (eq (car-safe str2) 'error) | |
1803 str2 | |
1804 (append '(calcFunc-lambda) (cdr str1) (list str2))))) | |
1805 (if (string-match "#" str) | |
1806 (let ((calc-hashes-used 0)) | |
1807 (and (setq str (math-read-expr str)) | |
1808 (if (eq (car-safe str) 'error) | |
1809 str | |
1810 (append '(calcFunc-lambda) | |
1811 (calc-invent-args calc-hashes-used) | |
1812 (list str))))) | |
1813 (math-parse-date str))))) | |
1814 (if (stringp res) | |
1815 (throw 'syntax res)) | |
1816 (if (eq (car-safe res) 'error) | |
1817 (throw 'syntax (nth 2 res))) | |
1818 (setq exp-pos (1+ last)) | |
1819 (math-read-token) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1820 res)) |
40785 | 1821 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1822 ;;; calc-forms.el ends here |