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