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