Mercurial > emacs
annotate lisp/calc/calc-forms.el @ 49755:fb628fd7f726
(fortran-fill): Fill lines that do not have comments.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Wed, 12 Feb 2003 21:51:45 +0000 |
parents | 0d8b17d428b5 |
children | d493b5b74454 d7ddb3e565de |
rev | line source |
---|---|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1 ;;; calc-forms.el --- data format conversion functions for Calc |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
4 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
6 ;; Maintainers: D. Goel <deego@gnufans.org> |
49263
f4d68f97221e
Add new maintainer (deego).
Deepak Goel <deego@gnufans.org>
parents:
42206
diff
changeset
|
7 ;; Colin Walters <walters@debian.org> |
40785 | 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 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
26 ;;; Commentary: |
40785 | 27 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
28 ;;; Code: |
40785 | 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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
48 (list 'hms 24 0 0)))))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
57 (calc-unary-op ">hms" 'calcFunc-hms arg)))) |
40785 | 58 |
59 (defun calc-from-hms (arg) | |
60 (interactive "P") | |
61 (calc-invert-func) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
62 (calc-to-hms arg)) |
40785 | 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 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
78 (error "Bad hours-minutes-seconds format")))) |
40785 | 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)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
156 (calc-change-mode 'calc-date-format (nreverse fullfmt) t)))) |
40785 | 157 |
158 | |
159 (defun calc-hms-mode () | |
160 (interactive) | |
161 (calc-wrapper | |
162 (calc-change-mode 'calc-angle-mode 'hms) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
163 (message "Angles measured in degrees-minutes-seconds"))) |
40785 | 164 |
165 | |
166 (defun calc-now (arg) | |
167 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
168 (calc-date-zero-args "now" 'calcFunc-now arg)) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
183 (calc-top-n 1))))) |
40785 | 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 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
190 (calc-enter-result arg "date" (cons 'calcFunc-date (calc-top-list-n arg))))) |
40785 | 191 |
192 (defun calc-julian (arg) | |
193 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
194 (calc-date-one-arg "juln" 'calcFunc-julian arg)) |
40785 | 195 |
196 (defun calc-unix-time (arg) | |
197 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
198 (calc-date-one-arg "unix" 'calcFunc-unixtime arg)) |
40785 | 199 |
200 (defun calc-time-zone (arg) | |
201 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
202 (calc-date-zero-args "zone" 'calcFunc-tzone arg)) |
40785 | 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 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
221 (calc-top-n 1) old new))))) |
40785 | 222 |
223 (defun calc-new-week (arg) | |
224 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
225 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg)) |
40785 | 226 |
227 (defun calc-new-month (arg) | |
228 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
229 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg)) |
40785 | 230 |
231 (defun calc-new-year (arg) | |
232 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
233 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg)) |
40785 | 234 |
235 (defun calc-inc-month (arg) | |
236 (interactive "p") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
237 (calc-date-one-arg "incm" 'calcFunc-incmonth arg)) |
40785 | 238 |
239 (defun calc-business-days-plus (arg) | |
240 (interactive "P") | |
241 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
242 (calc-binary-op "bus+" 'calcFunc-badd arg))) |
40785 | 243 |
244 (defun calc-business-days-minus (arg) | |
245 (interactive "P") | |
246 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
247 (calc-binary-op "bus-" 'calcFunc-bsub arg))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
255 (list func)))))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
264 (list func (calc-top-n 1))))))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
304 (list 'hms h m s))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
329 (math-sub b (math-mul hm 60)))))))) |
40785 | 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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
343 'deg))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
365 (nth 1 a))))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
415 (math-add (% time 60) (nth 2 parts)))))) |
40785 | 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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
433 date))) |
40785 | 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 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
443 (math-sub time ftime)))) |
40785 | 444 |
445 | |
446 (defun math-this-year () | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
447 (string-to-int (substring (current-time-string) -4))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
456 (= year 0)))) |
40785 | 457 |
458 (defun math-days-in-month (year month) | |
459 (if (and (= month 2) (math-leap-year-p year)) | |
460 29 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
461 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month)))) |
40785 | 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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
474 day-of-year)) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
494 (car res)))))))) |
40785 | 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 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
512 (defvar math-format-date-cache nil) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
530 fmt)))) |
40785 | 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)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
695 (math-format-number second)))))))) |
40785 | 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 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
843 hour minute second))))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
863 (and hour (list hour minute second)))))) |
40785 | 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)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
879 n)))) |
40785 | 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)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1037 day))))) |
40785 | 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") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1050 (append (list 'calcFunc-now) (and zone (list zone)))))) |
40785 | 1051 |
1052 (defun calcFunc-year (date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1053 (car (math-date-to-dt date))) |
40785 | 1054 |
1055 (defun calcFunc-month (date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1056 (nth 1 (math-date-to-dt date))) |
40785 | 1057 |
1058 (defun calcFunc-day (date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1059 (nth 2 (math-date-to-dt date))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1066 (math-mod (math-add (math-floor date) 6) 7)) |
40785 | 1067 |
1068 (defun calcFunc-yearday (date) | |
1069 (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
|
1070 (math-day-number (car dt) (nth 1 dt) (nth 2 dt)))) |
40785 | 1071 |
1072 (defun calcFunc-hour (date) | |
1073 (if (eq (car-safe date) 'hms) | |
1074 (nth 1 date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1075 (or (nth 3 (math-date-to-dt date)) 0))) |
40785 | 1076 |
1077 (defun calcFunc-minute (date) | |
1078 (if (eq (car-safe date) 'hms) | |
1079 (nth 2 date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1080 (or (nth 4 (math-date-to-dt date)) 0))) |
40785 | 1081 |
1082 (defun calcFunc-second (date) | |
1083 (if (eq (car-safe date) 'hms) | |
1084 (nth 3 date) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1085 (or (nth 5 (math-date-to-dt date)) 0))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1091 (list 'hms 0 0 0)))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1123 (math-reject-arg date 'datep))))) |
40785 | 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))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1138 (math-reject-arg date 'datep)))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1149 (math-reject-arg date 'datep)))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1227 (calcFunc-tzone tz date))))) |
40785 | 1228 |
1229 ;;; Note: Longer names must appear before shorter names which are | |
1230 ;;; substrings of them. | |
1231 (defvar math-tzone-names | |
1232 '( ( "MEGT" -1 "MET" "METDST" ) ; Middle Europe | |
1233 ( "METDST" -1 -1 ) ( "MET" -1 0 ) | |
1234 ( "MEGZ" -1 "MEZ" "MESZ" ) ( "MEZ" -1 0 ) ( "MESZ" -1 -1 ) | |
1235 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe | |
1236 ( "WETDST" 0 -1 ) ( "WET" 0 0 ) | |
1237 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -1 ) ; Britain | |
1238 ( "NGT" (float 35 -1) "NST" "NDT" ) ; Newfoundland | |
1239 ( "NST" (float 35 -1) 0 ) ( "NDT" (float 35 -1) -1 ) | |
1240 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -1 ) ; Atlantic | |
1241 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -1 ) ; Eastern | |
1242 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -1 ) ; Central | |
1243 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -1 ) ; Mountain | |
1244 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -1 ) ; Pacific | |
1245 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -1 ) ; Yukon | |
1246 )) | |
1247 | |
1248 | |
1249 (defun math-daylight-savings-adjust (date zone &optional dt) | |
1250 (or date (setq date (nth 1 (calcFunc-now)))) | |
1251 (let (bump) | |
1252 (if (eq (car-safe date) 'date) | |
1253 (setq bump 0 | |
1254 date (nth 1 date)) | |
1255 (if (and date (math-realp date)) | |
1256 (let ((zadj (assoc zone math-tzone-names))) | |
1257 (if zadj (setq bump -1 | |
1258 date (math-sub date (math-div (nth 1 zadj) | |
1259 '(float 24 0)))))) | |
1260 (math-reject-arg date 'datep))) | |
1261 (setq date (math-float date)) | |
1262 (or dt (setq dt (math-date-to-dt date))) | |
1263 (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
|
1264 (funcall math-daylight-savings-hook date dt zone bump)))) |
40785 | 1265 |
1266 (defun calcFunc-dsadj (date &optional zone) | |
1267 (if zone | |
1268 (or (eq (car-safe zone) 'var) | |
1269 (math-reject-arg zone "*Time zone variable expected")) | |
1270 (setq zone (or (calc-var-value 'var-TimeZone) | |
1271 (progn | |
1272 (calcFunc-tzone) | |
1273 (calc-var-value 'var-TimeZone))))) | |
1274 (setq zone (and (eq (car-safe zone) 'var) | |
1275 (upcase (symbol-name (nth 1 zone))))) | |
1276 (let ((zadj (assoc zone math-tzone-names))) | |
1277 (or zadj (math-reject-arg zone "*Unrecognized time zone name")) | |
1278 (if (integerp (nth 2 zadj)) | |
1279 (nth 2 zadj) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1280 (math-daylight-savings-adjust date zone)))) |
40785 | 1281 |
1282 (defun calcFunc-tzconv (date z1 z2) | |
1283 (if (math-realp date) | |
1284 (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
|
1285 (calcFunc-unixtime (calcFunc-unixtime date z1) z2))) |
40785 | 1286 |
1287 (defvar math-daylight-savings-hook 'math-std-daylight-savings) | |
1288 | |
1289 (defun math-std-daylight-savings (date dt zone bump) | |
1290 "Standard North American daylight savings algorithm. | |
1291 This implements the rules for the U.S. and Canada as of 1987. | |
1292 Daylight savings begins on the first Sunday of April at 2 a.m., | |
1293 and ends on the last Sunday of October at 2 a.m." | |
1294 (cond ((< (nth 1 dt) 4) 0) | |
1295 ((= (nth 1 dt) 4) | |
1296 (let ((sunday (math-prev-weekday-in-month date dt 7 0))) | |
1297 (cond ((< (nth 2 dt) sunday) 0) | |
1298 ((= (nth 2 dt) sunday) | |
1299 (if (>= (nth 3 dt) (+ 3 bump)) -1 0)) | |
1300 (t -1)))) | |
1301 ((< (nth 1 dt) 10) -1) | |
1302 ((= (nth 1 dt) 10) | |
1303 (let ((sunday (math-prev-weekday-in-month date dt 31 0))) | |
1304 (cond ((< (nth 2 dt) sunday) -1) | |
1305 ((= (nth 2 dt) sunday) | |
1306 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1)) | |
1307 (t 0)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1308 (t 0))) |
40785 | 1309 |
1310 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given | |
1311 ;;; day of the given month. | |
1312 (defun math-prev-weekday-in-month (date dt day wday) | |
1313 (or day (setq day (nth 2 dt))) | |
1314 (if (> day (math-days-in-month (car dt) (nth 1 dt))) | |
1315 (setq day (math-days-in-month (car dt) (nth 1 dt)))) | |
1316 (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
|
1317 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day))) zeroth))) |
40785 | 1318 |
1319 (defun calcFunc-pwday (date &optional day weekday) | |
1320 (if (eq (car-safe date) 'date) | |
1321 (setq date (nth 1 date))) | |
1322 (or (math-realp date) | |
1323 (math-reject-arg date 'datep)) | |
1324 (if (math-messy-integerp day) (setq day (math-trunc day))) | |
1325 (or (integerp day) (math-reject-arg day 'fixnump)) | |
1326 (if (= day 0) (setq day 31)) | |
1327 (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
|
1328 (math-prev-weekday-in-month date (math-date-to-dt date) day (or weekday 0))) |
40785 | 1329 |
1330 | |
1331 (defun calcFunc-newweek (date &optional weekday) | |
1332 (if (eq (car-safe date) 'date) | |
1333 (setq date (nth 1 date))) | |
1334 (or (math-realp date) | |
1335 (math-reject-arg date 'datep)) | |
1336 (or weekday (setq weekday 0)) | |
1337 (and (math-messy-integerp weekday) (setq weekday (math-trunc weekday))) | |
1338 (or (integerp weekday) (math-reject-arg weekday 'fixnump)) | |
1339 (and (or (< weekday 0) (> weekday 6)) (math-reject-arg weekday 'range)) | |
1340 (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
|
1341 (list 'date (math-sub date (calcFunc-weekday (math-sub date weekday))))) |
40785 | 1342 |
1343 (defun calcFunc-newmonth (date &optional day) | |
1344 (or day (setq day 1)) | |
1345 (and (math-messy-integerp day) (setq day (math-trunc day))) | |
1346 (or (integerp day) (math-reject-arg day 'fixnump)) | |
1347 (and (or (< day 0) (> day 31)) (math-reject-arg day 'range)) | |
1348 (let ((dt (math-date-to-dt date))) | |
1349 (if (or (= day 0) (> day (math-days-in-month (car dt) (nth 1 dt)))) | |
1350 (setq day (math-days-in-month (car dt) (nth 1 dt)))) | |
1351 (and (eq (car dt) 1752) (= (nth 1 dt) 9) | |
1352 (if (>= day 14) (setq day (- day 11)))) | |
1353 (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
|
1354 (1- day))))) |
40785 | 1355 |
1356 (defun calcFunc-newyear (date &optional day) | |
1357 (or day (setq day 1)) | |
1358 (and (math-messy-integerp day) (setq day (math-trunc day))) | |
1359 (or (integerp day) (math-reject-arg day 'fixnump)) | |
1360 (let ((dt (math-date-to-dt date))) | |
1361 (if (and (>= day 0) (<= day 366)) | |
1362 (let ((max (if (eq (car dt) 1752) 355 | |
1363 (if (math-leap-year-p (car dt)) 366 365)))) | |
1364 (if (or (= day 0) (> day max)) (setq day max)) | |
1365 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1)) | |
1366 (1- day)))) | |
1367 (if (and (>= day -12) (<= day -1)) | |
1368 (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
|
1369 (math-reject-arg day 'range))))) |
40785 | 1370 |
1371 (defun calcFunc-incmonth (date &optional step) | |
1372 (or step (setq step 1)) | |
1373 (and (math-messy-integerp step) (setq step (math-trunc step))) | |
1374 (or (math-integerp step) (math-reject-arg step 'integerp)) | |
1375 (let* ((dt (math-date-to-dt date)) | |
1376 (year (car dt)) | |
1377 (month (math-add (1- (nth 1 dt)) step)) | |
1378 (extra (calcFunc-idiv month 12)) | |
1379 (day (nth 2 dt))) | |
1380 (setq month (1+ (math-sub month (math-mul extra 12))) | |
1381 year (math-add year extra) | |
1382 day (min day (math-days-in-month year month))) | |
1383 (and (math-posp (car dt)) (not (math-posp year)) | |
1384 (setq year (math-sub year 1))) ; did we go past the year zero? | |
1385 (and (math-negp (car dt)) (not (math-negp year)) | |
1386 (setq year (math-add year 1))) | |
1387 (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
|
1388 (cons year (cons month (cons day (cdr (cdr (cdr dt)))))))))) |
40785 | 1389 |
1390 (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
|
1391 (calcFunc-incmonth date (math-mul (or step 1) 12))) |
40785 | 1392 |
1393 | |
1394 | |
1395 (defun calcFunc-bsub (a b) | |
1396 (or (eq (car-safe a) 'date) | |
1397 (math-reject-arg a 'datep)) | |
1398 (if (eq (car-safe b) 'date) | |
1399 (if (math-lessp (nth 1 a) (nth 1 b)) | |
1400 (math-neg (calcFunc-bsub b a)) | |
1401 (math-setup-holidays b) | |
1402 (let* ((da (math-to-business-day a)) | |
1403 (db (math-to-business-day b))) | |
1404 (math-add (math-sub (car da) (car db)) | |
1405 (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
|
1406 (calcFunc-badd a (math-neg b)))) |
40785 | 1407 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1408 (defvar math-holidays-cache nil) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1409 (defvar math-holidays-cache-tag t) |
40785 | 1410 (defun calcFunc-badd (a b) |
1411 (if (eq (car-safe b) 'date) | |
1412 (if (eq (car-safe a) 'date) | |
1413 (math-reject-arg nil "*Illegal combination in date arithmetic") | |
1414 (calcFunc-badd b a)) | |
1415 (if (eq (car-safe a) 'date) | |
1416 (if (Math-realp b) | |
1417 (if (Math-zerop b) | |
1418 a | |
1419 (let* ((d (math-to-business-day a)) | |
1420 (bb (math-add (car d) | |
1421 (if (and (cdr d) (Math-posp b)) | |
1422 (math-sub b 1) b)))) | |
1423 (or (math-from-business-day bb) | |
1424 (calcFunc-badd a b)))) | |
1425 (if (eq (car-safe b) 'hms) | |
1426 (let ((hours (nth 7 math-holidays-cache))) | |
1427 (setq b (math-div (math-from-hms b 'deg) 24)) | |
1428 (if hours | |
1429 (setq b (math-div b (cdr hours)))) | |
1430 (calcFunc-badd a b)) | |
1431 (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
|
1432 (math-reject-arg a 'datep)))) |
40785 | 1433 |
1434 (defun calcFunc-holiday (a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1435 (if (cdr (math-to-business-day a)) 1 0)) |
40785 | 1436 |
1437 ;;; Compute the number of business days since Jan 1, 1 AD. | |
1438 | |
1439 (defun math-to-business-day (date &optional need-year) | |
1440 (if (eq (car-safe date) 'date) | |
1441 (setq date (nth 1 date))) | |
1442 (or (Math-realp date) | |
1443 (math-reject-arg date 'datep)) | |
1444 (let* ((day (math-floor date)) | |
1445 (time (math-sub date day)) | |
1446 (dt (math-date-to-dt day)) | |
1447 (delta 0) | |
1448 (holiday nil)) | |
1449 (or (not need-year) (eq (car dt) need-year) | |
1450 (math-reject-arg (list 'date day) "*Generated holiday has wrong year")) | |
1451 (math-setup-holidays date) | |
1452 (let ((days (car math-holidays-cache))) | |
1453 (while (and (setq days (cdr days)) (< (car days) day)) | |
1454 (setq delta (1+ delta))) | |
1455 (and days (= day (car days)) | |
1456 (setq holiday t))) | |
1457 (let* ((weekdays (nth 3 math-holidays-cache)) | |
1458 (weeks (1- (/ (+ day 6) 7))) | |
1459 (wkday (- day 1 (* weeks 7)))) | |
1460 (setq delta (+ delta (* weeks (length weekdays)))) | |
1461 (while (and weekdays (< (car weekdays) wkday)) | |
1462 (setq weekdays (cdr weekdays) | |
1463 delta (1+ delta))) | |
1464 (and weekdays (eq wkday (car weekdays)) | |
1465 (setq holiday t))) | |
1466 (let ((hours (nth 7 math-holidays-cache))) | |
1467 (if hours | |
1468 (progn | |
1469 (setq time (math-div (math-sub time (car hours)) (cdr hours))) | |
1470 (if (Math-lessp time 0) (setq time 0)) | |
1471 (or (Math-lessp time 1) | |
1472 (setq time | |
1473 (math-sub 1 | |
1474 (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
|
1475 (cons (math-add (math-sub day delta) time) holiday))) |
40785 | 1476 |
1477 | |
1478 ;;; Compute the date a certain number of business days since Jan 1, 1 AD. | |
42206 | 1479 ;;; If this returns nil, holiday table was adjusted; redo calculation. |
40785 | 1480 |
1481 (defun math-from-business-day (num) | |
1482 (let* ((day (math-floor num)) | |
1483 (time (math-sub num day))) | |
1484 (or (integerp day) | |
1485 (math-reject-arg nil "*Date is outside valid range")) | |
1486 (math-setup-holidays) | |
1487 (let ((days (nth 1 math-holidays-cache)) | |
1488 (delta 0)) | |
1489 (while (and (setq days (cdr days)) (< (car days) day)) | |
1490 (setq delta (1+ delta))) | |
1491 (setq day (+ day delta))) | |
1492 (let* ((weekdays (nth 3 math-holidays-cache)) | |
1493 (bweek (- 7 (length weekdays))) | |
1494 (weeks (1- (/ (+ day (1- bweek)) bweek))) | |
1495 (wkday (- day 1 (* weeks bweek))) | |
1496 (w 0)) | |
1497 (setq day (+ day (* weeks (length weekdays)))) | |
1498 (while (if (memq w weekdays) | |
1499 (setq day (1+ day)) | |
1500 (> (setq wkday (1- wkday)) 0)) | |
1501 (setq w (1+ w))) | |
1502 (let ((hours (nth 7 math-holidays-cache))) | |
1503 (if hours | |
1504 (setq time (math-add (math-mul time (cdr hours)) (car hours))))) | |
1505 (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
|
1506 (list 'date (math-add day time)))))) |
40785 | 1507 |
1508 | |
1509 (defun math-setup-holidays (&optional date) | |
1510 (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag) | |
1511 (let ((h (calc-var-value 'var-Holidays)) | |
1512 (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3) | |
1513 (thu . 4) (fri . 5) (sat . 6) )) | |
1514 (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil)) | |
1515 (or (math-vectorp h) | |
1516 (math-reject-arg h "*Holidays variable must be a vector")) | |
1517 (while (setq h (cdr h)) | |
1518 (cond ((or (and (eq (car-safe (car h)) 'date) | |
1519 (integerp (nth 1 (car h)))) | |
1520 (and (eq (car-safe (car h)) 'intv) | |
1521 (eq (car-safe (nth 2 (car h))) 'date)) | |
1522 (eq (car-safe (car h)) 'vec)) | |
1523 (setq days (cons (car h) days))) | |
1524 ((and (eq (car-safe (car h)) 'var) | |
1525 (assq (nth 1 (car h)) wdnames)) | |
1526 (setq weekdays (cons (cdr (assq (nth 1 (car h)) wdnames)) | |
1527 weekdays))) | |
1528 ((and (eq (car-safe (car h)) 'intv) | |
1529 (eq (car-safe (nth 2 (car h))) 'hms) | |
1530 (eq (car-safe (nth 3 (car h))) 'hms)) | |
1531 (if hours | |
1532 (math-reject-arg | |
1533 (car h) "*Only one hours interval allowed in Holidays")) | |
1534 (setq hours (math-div (car h) '(hms 24 0 0))) | |
1535 (if (or (Math-lessp (nth 2 hours) 0) | |
1536 (Math-lessp 1 (nth 3 hours))) | |
1537 (math-reject-arg | |
1538 (car h) "*Hours interval out of range")) | |
1539 (setq hours (cons (nth 2 hours) | |
1540 (math-sub (nth 3 hours) (nth 2 hours)))) | |
1541 (if (Math-zerop (cdr hours)) | |
1542 (math-reject-arg | |
1543 (car h) "*Degenerate hours interval"))) | |
1544 ((or (and (eq (car-safe (car h)) 'intv) | |
1545 (Math-integerp (nth 2 (car h))) | |
1546 (Math-integerp (nth 3 (car h)))) | |
1547 (and (integerp (car h)) | |
1548 (> (car h) 1900) (< (car h) 2100))) | |
1549 (if limit | |
1550 (math-reject-arg | |
1551 (car h) "*Only one limit allowed in Holidays")) | |
1552 (setq limit (calcFunc-vint (car h) '(intv 3 1 2737))) | |
1553 (if (equal limit '(vec)) | |
1554 (math-reject-arg (car h) "*Limit is out of range"))) | |
1555 ((or (math-expr-contains (car h) '(var y var-y)) | |
1556 (math-expr-contains (car h) '(var m var-m))) | |
1557 (setq exprs (cons (car h) exprs))) | |
1558 (t (math-reject-arg | |
1559 (car h) "*Holidays must contain a vector of holidays")))) | |
1560 (if (= (length weekdays) 7) | |
1561 (math-reject-arg nil "*Too many weekend days")) | |
1562 (setq math-holidays-cache (list (list -1) ; 0: days list | |
1563 (list -1) ; 1: inverse-days list | |
1564 nil ; 2: exprs | |
1565 (sort weekdays '<) | |
1566 (or limit '(intv 3 1 2737)) | |
1567 nil ; 5: (lo.hi) expanded years | |
1568 (cons exprs days) | |
1569 hours) ; 7: business hours | |
1570 math-holidays-cache-tag (calc-var-value 'var-Holidays)))) | |
1571 (if date | |
1572 (let ((year (calcFunc-year date)) | |
1573 (limits (nth 5 math-holidays-cache)) | |
1574 (done nil)) | |
1575 (or (eq (calcFunc-in year (nth 4 math-holidays-cache)) 1) | |
1576 (progn | |
1577 (or (eq (car-safe date) 'date) (setq date (list 'date date))) | |
1578 (math-reject-arg date "*Date is outside valid range"))) | |
1579 (unwind-protect | |
1580 (let ((days (nth 6 math-holidays-cache))) | |
1581 (if days | |
1582 (let ((year nil)) ; see below | |
1583 (setcar (nthcdr 6 math-holidays-cache) nil) | |
1584 (math-setup-add-holidays (cons 'vec (cdr days))) | |
1585 (setcar (nthcdr 2 math-holidays-cache) (car days)))) | |
1586 (cond ((not (nth 2 math-holidays-cache)) | |
1587 (setq done t) | |
1588 nil) | |
1589 ((not limits) | |
1590 (setcar (nthcdr 5 math-holidays-cache) (cons year year)) | |
1591 (math-setup-year-holidays year) | |
1592 (setq done t)) | |
1593 ((< year (car limits)) | |
1594 (message "Computing holidays, %d .. %d" | |
1595 year (1- (car limits))) | |
1596 (calc-set-command-flag 'clear-message) | |
1597 (while (< year (car limits)) | |
1598 (setcar limits (1- (car limits))) | |
1599 (math-setup-year-holidays (car limits))) | |
1600 (setq done t)) | |
1601 ((> year (cdr limits)) | |
1602 (message "Computing holidays, %d .. %d" | |
1603 (1+ (cdr limits)) year) | |
1604 (calc-set-command-flag 'clear-message) | |
1605 (while (> year (cdr limits)) | |
1606 (setcdr limits (1+ (cdr limits))) | |
1607 (math-setup-year-holidays (cdr limits))) | |
1608 (setq done t)) | |
1609 (t | |
1610 (setq done t) | |
1611 nil))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1612 (or done (setq math-holidays-cache-tag t)))))) |
40785 | 1613 |
1614 (defun math-setup-year-holidays (year) | |
1615 (let ((exprs (nth 2 math-holidays-cache))) | |
1616 (while exprs | |
1617 (let* ((var-y year) | |
1618 (var-m nil) | |
1619 (expr (math-evaluate-expr (car exprs)))) | |
1620 (if (math-expr-contains expr '(var m var-m)) | |
1621 (let ((var-m 0)) | |
1622 (while (<= (setq var-m (1+ var-m)) 12) | |
1623 (math-setup-add-holidays (math-evaluate-expr expr)))) | |
1624 (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
|
1625 (setq exprs (cdr exprs))))) |
40785 | 1626 |
1627 (defun math-setup-add-holidays (days) ; uses "year" | |
1628 (cond ((eq (car-safe days) 'vec) | |
1629 (while (setq days (cdr days)) | |
1630 (math-setup-add-holidays (car days)))) | |
1631 ((eq (car-safe days) 'intv) | |
1632 (let ((day (math-ceiling (nth 2 days)))) | |
1633 (or (eq (calcFunc-in day days) 1) | |
1634 (setq day (math-add day 1))) | |
1635 (while (eq (calcFunc-in day days) 1) | |
1636 (math-setup-add-holidays day) | |
1637 (setq day (math-add day 1))))) | |
1638 ((eq (car-safe days) 'date) | |
1639 (math-setup-add-holidays (nth 1 days))) | |
1640 ((eq days 0)) | |
1641 ((integerp days) | |
1642 (let ((b (math-to-business-day days year))) | |
1643 (or (cdr b) ; don't register holidays twice! | |
1644 (let ((prev (car math-holidays-cache)) | |
1645 (iprev (nth 1 math-holidays-cache))) | |
1646 (while (and (cdr prev) (< (nth 1 prev) days)) | |
1647 (setq prev (cdr prev) iprev (cdr iprev))) | |
1648 (setcdr prev (cons days (cdr prev))) | |
1649 (setcdr iprev (cons (car b) (cdr iprev))) | |
1650 (while (setq iprev (cdr iprev)) | |
1651 (setcar iprev (1- (car iprev)))))))) | |
1652 ((Math-realp days) | |
1653 (math-reject-arg (list 'date days) "*Invalid holiday value")) | |
1654 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1655 (math-reject-arg days "*Holiday formula failed to evaluate")))) |
40785 | 1656 |
1657 | |
1658 | |
1659 | |
1660 ;;;; Error forms. | |
1661 | |
1662 ;;; Build a standard deviation form. [X X X] | |
1663 (defun math-make-sdev (x sigma) | |
1664 (if (memq (car-safe x) '(date mod sdev intv vec)) | |
1665 (math-reject-arg x 'realp)) | |
1666 (if (memq (car-safe sigma) '(date mod sdev intv vec)) | |
1667 (math-reject-arg sigma 'realp)) | |
1668 (if (or (Math-negp sigma) (memq (car-safe sigma) '(cplx polar))) | |
1669 (setq sigma (math-abs sigma))) | |
1670 (if (and (Math-zerop sigma) (Math-scalarp x)) | |
1671 x | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1672 (list 'sdev x sigma))) |
40785 | 1673 (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
|
1674 (math-make-sdev x sigma)) |
40785 | 1675 |
1676 | |
1677 | |
1678 ;;;; Modulo forms. | |
1679 | |
1680 (defun math-normalize-mod (a) | |
1681 (let ((n (math-normalize (nth 1 a))) | |
1682 (m (math-normalize (nth 2 a)))) | |
1683 (if (and (math-anglep n) (math-anglep m) (math-posp m)) | |
1684 (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
|
1685 (math-normalize (list 'calcFunc-makemod n m))))) |
40785 | 1686 |
1687 ;;; Build a modulo form. [N R R] | |
1688 (defun math-make-mod (n m) | |
1689 (setq calc-previous-modulo m) | |
1690 (and n | |
1691 (cond ((not (Math-anglep m)) | |
1692 (math-reject-arg m 'anglep)) | |
1693 ((not (math-posp m)) | |
1694 (math-reject-arg m 'posp)) | |
1695 ((Math-anglep n) | |
1696 (if (or (Math-negp n) | |
1697 (not (Math-lessp n m))) | |
1698 (list 'mod (math-mod n m) m) | |
1699 (list 'mod n m))) | |
1700 ((memq (car n) '(+ - / vec neg)) | |
1701 (math-normalize | |
1702 (cons (car n) | |
1703 (mapcar (function (lambda (x) (math-make-mod x m))) | |
1704 (cdr n))))) | |
1705 ((and (eq (car n) '*) (Math-anglep (nth 1 n))) | |
1706 (math-mul (math-make-mod (nth 1 n) m) (nth 2 n))) | |
1707 ((memq (car n) '(* ^ var calcFunc-subscr)) | |
1708 (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
|
1709 (t (math-reject-arg n 'anglep))))) |
40785 | 1710 (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
|
1711 (math-make-mod n m)) |
40785 | 1712 |
1713 | |
1714 | |
1715 ;;;; Interval forms. | |
1716 | |
1717 ;;; Build an interval form. [X S X X] | |
1718 (defun math-make-intv (mask lo hi) | |
1719 (if (memq (car-safe lo) '(cplx polar mod sdev intv vec)) | |
1720 (math-reject-arg lo 'realp)) | |
1721 (if (memq (car-safe hi) '(cplx polar mod sdev intv vec)) | |
1722 (math-reject-arg hi 'realp)) | |
1723 (or (eq (eq (car-safe lo) 'date) (eq (car-safe hi) 'date)) | |
1724 (math-reject-arg (if (eq (car-safe lo) 'date) hi lo) 'datep)) | |
1725 (if (and (or (Math-realp lo) (eq (car lo) 'date)) | |
1726 (or (Math-realp hi) (eq (car hi) 'date))) | |
1727 (let ((cmp (math-compare lo hi))) | |
1728 (if (= cmp 0) | |
1729 (if (= mask 3) | |
1730 lo | |
1731 (list 'intv mask lo hi)) | |
1732 (if (> cmp 0) | |
1733 (if (= mask 3) | |
1734 (list 'intv 2 lo lo) | |
1735 (list 'intv mask lo lo)) | |
1736 (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
|
1737 (list 'intv mask lo hi))) |
40785 | 1738 (defun calcFunc-intv (mask lo hi) |
1739 (if (math-messy-integerp mask) (setq mask (math-trunc mask))) | |
1740 (or (natnump mask) (math-reject-arg mask 'fixnatnump)) | |
1741 (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
|
1742 (math-make-intv mask lo hi)) |
40785 | 1743 |
1744 (defun math-sort-intv (mask lo hi) | |
1745 (if (Math-lessp hi lo) | |
1746 (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
|
1747 (math-make-intv mask lo hi))) |
40785 | 1748 |
1749 | |
1750 | |
1751 | |
1752 (defun math-combine-intervals (a am b bm c cm d dm) | |
1753 (let (res) | |
1754 (if (= (setq res (math-compare a c)) 1) | |
1755 (setq a c am cm) | |
1756 (if (= res 0) | |
1757 (setq am (or am cm)))) | |
1758 (if (= (setq res (math-compare b d)) -1) | |
1759 (setq b d bm dm) | |
1760 (if (= res 0) | |
1761 (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
|
1762 (math-make-intv (+ (if am 2 0) (if bm 1 0)) a b))) |
40785 | 1763 |
1764 | |
1765 (defun math-div-mod (a b m) ; [R R R R] (Returns nil if no solution) | |
1766 (and (Math-integerp a) (Math-integerp b) (Math-integerp m) | |
1767 (let ((u1 1) (u3 b) (v1 0) (v3 m)) | |
1768 (while (not (eq v3 0)) ; See Knuth sec 4.5.2, exercise 15 | |
1769 (let* ((q (math-idivmod u3 v3)) | |
1770 (t1 (math-sub u1 (math-mul v1 (car q))))) | |
1771 (setq u1 v1 u3 v3 v1 t1 v3 (cdr q)))) | |
1772 (let ((q (math-idivmod a u3))) | |
1773 (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
|
1774 (math-mod (math-mul (car q) u1) m)))))) |
40785 | 1775 |
1776 (defun math-mod-intv (a b) | |
1777 (let* ((q1 (math-floor (math-div (nth 2 a) b))) | |
1778 (q2 (math-floor (math-div (nth 3 a) b))) | |
1779 (m1 (math-sub (nth 2 a) (math-mul q1 b))) | |
1780 (m2 (math-sub (nth 3 a) (math-mul q2 b)))) | |
1781 (cond ((equal q1 q2) | |
1782 (math-sort-intv (nth 1 a) m1 m2)) | |
1783 ((and (math-equal-int (math-sub q2 q1) 1) | |
1784 (math-zerop m2) | |
1785 (memq (nth 1 a) '(0 2))) | |
1786 (math-make-intv (nth 1 a) m1 b)) | |
1787 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1788 (math-make-intv 2 0 b))))) |
40785 | 1789 |
1790 | |
1791 (defun math-read-angle-brackets () | |
1792 (let* ((last (or (math-check-for-commas t) (length exp-str))) | |
1793 (str (substring exp-str exp-pos last)) | |
1794 (res | |
1795 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str) | |
1796 (let ((str1 (substring str 0 (1- (match-end 0)))) | |
1797 (str2 (substring str (match-end 0))) | |
1798 (calc-hashes-used 0)) | |
1799 (setq str1 (math-read-expr (concat "[" str1 "]"))) | |
1800 (if (eq (car-safe str1) 'error) | |
1801 str1 | |
1802 (setq str2 (math-read-expr str2)) | |
1803 (if (eq (car-safe str2) 'error) | |
1804 str2 | |
1805 (append '(calcFunc-lambda) (cdr str1) (list str2))))) | |
1806 (if (string-match "#" str) | |
1807 (let ((calc-hashes-used 0)) | |
1808 (and (setq str (math-read-expr str)) | |
1809 (if (eq (car-safe str) 'error) | |
1810 str | |
1811 (append '(calcFunc-lambda) | |
1812 (calc-invent-args calc-hashes-used) | |
1813 (list str))))) | |
1814 (math-parse-date str))))) | |
1815 (if (stringp res) | |
1816 (throw 'syntax res)) | |
1817 (if (eq (car-safe res) 'error) | |
1818 (throw 'syntax (nth 2 res))) | |
1819 (setq exp-pos (1+ last)) | |
1820 (math-read-token) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1821 res)) |
40785 | 1822 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1823 ;;; calc-forms.el ends here |