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