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