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