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