]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-forms.el
* lisp/calc/calc-forms.el (calc-date-notation): Fix typo.
[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]\\|10\\|11\\) *\\'" 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 11)
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 (or (memq 'IYYY calc-date-format) (memq 'Iww calc-date-format))
922 (math-parse-iso-date math-pd-str))
923 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str)
924 (list 'date (math-read-number (math-match-substring math-pd-str 1))))
925 (let ((case-fold-search t)
926 (year nil) (month nil) (day nil) (weekday nil)
927 (hour nil) (minute nil) (second nil) (bc-flag nil)
928 (a nil) (b nil) (c nil) (bigyear nil) temp)
929
930 ;; Extract the time, if any.
931 (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)
932 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str))
933 (let ((ampm (math-match-substring math-pd-str 6)))
934 (setq hour (string-to-number (math-match-substring math-pd-str 1))
935 minute (math-match-substring math-pd-str 2)
936 second (math-match-substring math-pd-str 4)
937 math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
938 (substring math-pd-str (match-end 0))))
939 (if (equal minute "")
940 (setq minute 0)
941 (setq minute (string-to-number minute)))
942 (if (equal second "")
943 (setq second 0)
944 (setq second (math-read-number second)))
945 (if (equal ampm "")
946 (if (> hour 23)
947 (throw 'syntax "Hour value out of range"))
948 (setq ampm (upcase (aref ampm 0)))
949 (if (memq ampm '(?N ?M))
950 (if (and (= hour 12) (= minute 0) (eq second 0))
951 (if (eq ampm ?M) (setq hour 0))
952 (throw 'syntax
953 "Time must be 12:00:00 in this context"))
954 (if (or (= hour 0) (> hour 12))
955 (throw 'syntax "Hour value out of range"))
956 (if (eq (= ampm ?A) (= hour 12))
957 (setq hour (% (+ hour 12) 24)))))))
958
959 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
960 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str)
961 (progn
962 (setq math-pd-str (copy-sequence math-pd-str))
963 (aset math-pd-str (match-beginning 1) ?\/)))
964
965 ;; Extract obvious month or weekday names.
966 (if (string-match "[a-zA-Z]" math-pd-str)
967 (progn
968 (setq month (math-parse-date-word math-long-month-names))
969 (setq weekday (math-parse-date-word math-long-weekday-names))
970 (or month (setq month
971 (math-parse-date-word math-short-month-names)))
972 (or weekday (math-parse-date-word math-short-weekday-names))
973 (or hour
974 (if (setq temp (math-parse-date-word
975 '( "noon" "midnight" "mid" )))
976 (setq hour (if (= temp 1) 12 0) minute 0 second 0)))
977 (or (math-parse-date-word '( "ad" "a.d." ))
978 (if (math-parse-date-word '( "bc" "b.c." ))
979 (setq bc-flag t)))
980 (if (string-match "[a-zA-Z]+" math-pd-str)
981 (throw 'syntax (format "Bad word in date: \"%s\""
982 (math-match-substring math-pd-str 0))))))
983
984 ;; If there is a huge number other than the year, ignore it.
985 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str)
986 (setq temp (concat (substring math-pd-str 0 (match-beginning 0))
987 (substring math-pd-str (match-end 0))))
988 (string-match
989 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp))
990 (setq math-pd-str temp))
991
992 ;; If there is a number with a sign or a large number, it is a year.
993 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str)
994 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str))
995 (setq year (math-match-substring math-pd-str 1)
996 math-pd-str (concat (substring math-pd-str 0 (match-beginning 1))
997 (substring math-pd-str (match-end 1)))
998 year (math-read-number year)
999 bigyear t))
1000
1001 ;; Collect remaining numbers.
1002 (setq temp 0)
1003 (while (string-match "[0-9]+" math-pd-str temp)
1004 (and c (throw 'syntax "Too many numbers in date"))
1005 (setq c (string-to-number (math-match-substring math-pd-str 0)))
1006 (or b (setq b c c nil))
1007 (or a (setq a b b nil))
1008 (setq temp (match-end 0)))
1009
1010 ;; Check that we have the right amount of information.
1011 (setq temp (+ (if year 1 0) (if month 1 0) (if day 1 0)
1012 (if a 1 0) (if b 1 0) (if c 1 0)))
1013 (if (> temp 3)
1014 (throw 'syntax "Too many numbers in date")
1015 (if (or (< temp 2) (and year (= temp 2)))
1016 (throw 'syntax "Not enough numbers in date")
1017 (if (= temp 2) ; if year omitted, assume current year
1018 (setq year (math-this-year)))))
1019
1020 ;; A large number must be a year.
1021 (or year
1022 (if (and a (or (> a 31) (< a 1)))
1023 (setq year a a b b c c nil)
1024 (if (and b (or (> b 31) (< b 1)))
1025 (setq year b b c c nil)
1026 (if (and c (or (> c 31) (< c 1)))
1027 (setq year c c nil)))))
1028
1029 ;; A medium-large number must be a day.
1030 (if year
1031 (if (and a (> a 12))
1032 (setq day a a b b c c nil)
1033 (if (and b (> b 12))
1034 (setq day b b c c nil)
1035 (if (and c (> c 12))
1036 (setq day c c nil)))))
1037
1038 ;; We may know enough to sort it out now.
1039 (if (and year day)
1040 (or month (setq month a))
1041 (if (and year month)
1042 (setq day a)
1043
1044 ;; Interpret order of numbers as same as for display format.
1045 (setq temp calc-date-format)
1046 (while temp
1047 (cond ((not (symbolp (car temp))))
1048 ((memq (car temp) '(Y YY BY YYY YYYY))
1049 (or year (setq year a a b b c)))
1050 ((memq (car temp) '(M MM BM mmm Mmm Mmmm MMM MMMM))
1051 (or month (setq month a a b b c)))
1052 ((memq (car temp) '(D DD BD))
1053 (or day (setq day a a b b c))))
1054 (setq temp (cdr temp)))
1055
1056 ;; If display format was not complete, assume American style.
1057 (or month (setq month a a b b c))
1058 (or day (setq day a a b b c))
1059 (or year (setq year a a b b c))))
1060
1061 (if bc-flag
1062 (setq year (math-neg (math-abs year))))
1063
1064 (math-parse-date-validate year bigyear month day
1065 hour minute second)))))
1066
1067 (defun math-parse-date-validate (year bigyear month day hour minute second)
1068 (and (not bigyear) (natnump year) (< year 100)
1069 (setq year (+ year (if (< year 40) 2000 1900))))
1070 (if (eq year 0)
1071 (throw 'syntax "Year value is out of range"))
1072 (if (or (< month 1) (> month 12))
1073 (throw 'syntax "Month value is out of range"))
1074 (if (or (< day 1) (> day (math-days-in-month year month)))
1075 (throw 'syntax "Day value is out of range"))
1076 (and hour
1077 (progn
1078 (if (or (< hour 0) (> hour 23))
1079 (throw 'syntax "Hour value is out of range"))
1080 (if (or (< minute 0) (> minute 59))
1081 (throw 'syntax "Minute value is out of range"))
1082 (if (or (math-negp second) (not (Math-lessp second 60)))
1083 (throw 'syntax "Seconds value is out of range"))))
1084 (list 'date (math-dt-to-date (append (list year month day)
1085 (and hour (list hour minute second))))))
1086
1087 (defun math-parse-iso-date-validate (isoyear isoweek isoweekday hour minute second)
1088 (if (or (< isoweek 1) (> isoweek 53))
1089 (throw 'syntax "Week value is out of range"))
1090 (if (or (< isoweekday 1) (> isoweekday 7))
1091 (throw 'syntax "Weekday value is out of range"))
1092 (and hour
1093 (progn
1094 (if (or (< hour 0) (> hour 23))
1095 (throw 'syntax "Hour value is out of range"))
1096 (if (or (< minute 0) (> minute 59))
1097 (throw 'syntax "Minute value is out of range"))
1098 (if (or (math-negp second) (not (Math-lessp second 60)))
1099 (throw 'syntax "Seconds value is out of range"))))
1100 (list 'date (math-iso-dt-to-date (append (list isoyear isoweek isoweekday)
1101 (and hour (list hour minute second))))))
1102
1103 (defun math-parse-date-word (names &optional front)
1104 (let ((n 1))
1105 (while (and names (not (string-match (if (equal (car names) "Sep")
1106 "Sept?"
1107 (regexp-quote (car names)))
1108 math-pd-str)))
1109 (setq names (cdr names)
1110 n (1+ n)))
1111 (and names
1112 (or (not front) (= (match-beginning 0) 0))
1113 (progn
1114 (setq math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
1115 (if front "" " ")
1116 (substring math-pd-str (match-end 0))))
1117 n))))
1118
1119 (defun math-parse-standard-date (math-pd-str with-time)
1120 (let ((case-fold-search t)
1121 (okay t) num
1122 (fmt calc-date-format) this next (gnext nil)
1123 (isoyear nil) (isoweek nil) (isoweekday nil)
1124 (year nil) (month nil) (day nil) (bigyear nil) (yearday nil)
1125 (hour nil) (minute nil) (second nil) (bc-flag nil))
1126 (while (and fmt okay)
1127 (setq this (car fmt)
1128 fmt (setq fmt (or (cdr fmt)
1129 (prog1
1130 gnext
1131 (setq gnext nil))))
1132 next (car fmt))
1133 (if (consp next) (setq next (car next)))
1134 (or (cond ((listp this)
1135 (or (not with-time)
1136 (not this)
1137 (setq gnext fmt
1138 fmt this)))
1139 ((stringp this)
1140 (if (and (<= (length this) (length math-pd-str))
1141 (equal this
1142 (substring math-pd-str 0 (length this))))
1143 (setq math-pd-str (substring math-pd-str (length this)))))
1144 ((eq this 'X)
1145 t)
1146 ((memq this '(n N j J))
1147 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str)
1148 (setq num (math-match-substring math-pd-str 0)
1149 math-pd-str (substring math-pd-str (match-end 0))
1150 num (math-date-to-dt (math-read-number num))
1151 num (math-sub num
1152 (if (memq this '(n N))
1153 0
1154 (if (or (eq this 'j)
1155 (math-integerp num))
1156 math-julian-date-beginning-int
1157 math-julian-date-beginning)))
1158 hour (or (nth 3 num) hour)
1159 minute (or (nth 4 num) minute)
1160 second (or (nth 5 num) second)
1161 year (car num)
1162 month (nth 1 num)
1163 day (nth 2 num))))
1164 ((eq this 'U)
1165 (and (string-match "\\`[-+]?[0-9]+" math-pd-str)
1166 (setq num (math-match-substring math-pd-str 0)
1167 math-pd-str (substring math-pd-str (match-end 0))
1168 num (math-date-to-dt
1169 (math-add 719164
1170 (math-div (math-read-number num)
1171 '(float 864 2))))
1172 hour (nth 3 num)
1173 minute (nth 4 num)
1174 second (nth 5 num)
1175 year (car num)
1176 month (nth 1 num)
1177 day (nth 2 num))))
1178 ((memq this '(mmm Mmm MMM))
1179 (setq month (math-parse-date-word math-short-month-names t)))
1180 ((memq this '(Mmmm MMMM))
1181 (setq month (math-parse-date-word math-long-month-names t)))
1182 ((memq this '(www Www WWW))
1183 (math-parse-date-word math-short-weekday-names t))
1184 ((memq this '(Wwww WWWW))
1185 (math-parse-date-word math-long-weekday-names t))
1186 ((memq this '(p P))
1187 (if (string-match "\\`a" math-pd-str)
1188 (setq hour (if (= hour 12) 0 hour)
1189 math-pd-str (substring math-pd-str 1))
1190 (if (string-match "\\`p" math-pd-str)
1191 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
1192 math-pd-str (substring math-pd-str 1)))))
1193 ((memq this '(pp PP pppp PPPP))
1194 (if (string-match "\\`am\\|a\\.m\\." math-pd-str)
1195 (setq hour (if (= hour 12) 0 hour)
1196 math-pd-str (substring math-pd-str (match-end 0)))
1197 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str)
1198 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
1199 math-pd-str (substring math-pd-str (match-end 0))))))
1200 ((memq this '(Y YY BY YYY YYYY ZYYY))
1201 (and (if (memq next '(MM DD ddd hh HH mm ss SS))
1202 (if (memq this '(Y YY BYY))
1203 (string-match "\\` *[0-9][0-9]" math-pd-str)
1204 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str))
1205 (string-match "\\`[-+]?[0-9]+" math-pd-str))
1206 (setq year (math-match-substring math-pd-str 0)
1207 bigyear (or (eq this 'YYY)
1208 (memq (aref math-pd-str 0) '(?\+ ?\-)))
1209 math-pd-str (substring math-pd-str (match-end 0))
1210 year (math-read-number year))
1211 (if (and (eq this 'ZYYY) (eq year 0))
1212 (setq year (math-sub year 1)
1213 bigyear t)
1214 t)))
1215 ((eq this 'IYYY)
1216 (if (string-match "\\`[-+]?[0-9]+" math-pd-str)
1217 (setq isoyear (string-to-number (math-match-substring math-pd-str 0))
1218 math-pd-str (substring math-pd-str (match-end 0)))))
1219 ((eq this 'Iww)
1220 (if (string-match "W\\([0-9][0-9]\\)" math-pd-str)
1221 (setq isoweek (string-to-number (math-match-substring math-pd-str 1))
1222 math-pd-str (substring math-pd-str 3))))
1223 ((eq this 'b)
1224 t)
1225 ((eq this 'T)
1226 (if (eq (aref math-pd-str 0) ?T)
1227 (setq math-pd-str (substring math-pd-str 1))
1228 t))
1229 ((memq this '(aa AA aaaa AAAA))
1230 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str)
1231 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1232 ((memq this '(aaa AAA))
1233 (if (string-match "\\` *ad *" math-pd-str)
1234 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1235 ((memq this '(bb BB bbb BBB bbbb BBBB))
1236 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str)
1237 (setq math-pd-str (substring math-pd-str (match-end 0))
1238 bc-flag t)))
1239 ((memq this '(s ss bs SS BS))
1240 (and (if (memq next '(YY YYYY MM DD hh HH mm))
1241 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str)
1242 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str))
1243 (setq second (math-match-substring math-pd-str 0)
1244 math-pd-str (substring math-pd-str (match-end 0))
1245 second (math-read-number second))))
1246 ((eq this 'C)
1247 (if (string-match "\\`:[0-9][0-9]" math-pd-str)
1248 (setq math-pd-str (substring math-pd-str 1))
1249 t))
1250 ((or (not (if (and (memq this '(ddd MM DD hh HH mm))
1251 (memq next '(YY YYYY MM DD ddd
1252 hh HH mm ss SS)))
1253 (if (eq this 'ddd)
1254 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str)
1255 (string-match "\\` *[0-9][0-9]" math-pd-str))
1256 (string-match "\\` *[0-9]+" math-pd-str)))
1257 (and (setq num (string-to-number
1258 (math-match-substring math-pd-str 0))
1259 math-pd-str (substring math-pd-str (match-end 0)))
1260 nil))
1261 nil)
1262 ((eq this 'W)
1263 (and (>= num 0) (< num 7)))
1264 ((eq this 'w)
1265 (setq isoweekday num))
1266 ((memq this '(d ddd bdd))
1267 (setq yearday num))
1268 ((memq this '(M MM BM))
1269 (setq month num))
1270 ((memq this '(D DD BD))
1271 (setq day num))
1272 ((memq this '(h hh bh H HH BH))
1273 (setq hour num))
1274 ((memq this '(m mm bm))
1275 (setq minute num)))
1276 (setq okay nil)))
1277 (if yearday
1278 (if (and month day)
1279 (setq yearday nil)
1280 (setq month 1 day 1)))
1281 (if (and okay (equal math-pd-str ""))
1282 (if isoyear
1283 (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second)
1284 (and month day (or (not (or hour minute second))
1285 (and hour minute))
1286 (progn
1287 (or year (setq year (math-this-year)))
1288 (or second (setq second 0))
1289 (if bc-flag
1290 (setq year (math-neg (math-abs year))))
1291 (setq day (math-parse-date-validate year bigyear month day
1292 hour minute second))
1293 (if yearday
1294 (setq day (math-add day (1- yearday))))
1295 day))))))
1296
1297 (defun math-parse-iso-date (math-pd-str)
1298 "Parse MATH-PD-STR as an ISO week date, or return nil."
1299 (let ((case-fold-search t)
1300 (isoyear nil) (isoweek nil) (isoweekday nil)
1301 (hour nil) (minute nil) (second nil))
1302 ;; Extract the time, if any.
1303 (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)
1304 (progn
1305 (setq hour (string-to-number (math-match-substring math-pd-str 1))
1306 minute (math-match-substring math-pd-str 2)
1307 second (math-match-substring math-pd-str 3)
1308 math-pd-str (substring math-pd-str 0 (match-beginning 0)))
1309 (if (equal minute "")
1310 (setq minute 0)
1311 (setq minute (string-to-number minute)))
1312 (if (equal second "")
1313 (setq second 0)
1314 (setq second (math-read-number second)))))
1315 ;; Next, the year, week and weekday
1316 (if (string-match "\\(-?[0-9]*\\)[^0-9]*W\\([0-9][0-9]\\)[^0-9]*\\([0-9]\\)[^0-9]*\\'" math-pd-str)
1317 (progn
1318 (setq isoyear (string-to-number (math-match-substring math-pd-str 1))
1319 isoweek (string-to-number (math-match-substring math-pd-str 2))
1320 isoweekday (string-to-number (math-match-substring math-pd-str 3)))
1321 (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second)))))
1322
1323 (defun calcFunc-now (&optional zone)
1324 (let ((date (let ((calc-date-format nil))
1325 (math-parse-date (current-time-string)))))
1326 (if (consp date)
1327 (if zone
1328 (math-add date (math-div (math-sub (calcFunc-tzone nil date)
1329 (calcFunc-tzone zone date))
1330 '(float 864 2)))
1331 date)
1332 (calc-record-why "*Unable to interpret current date from system")
1333 (append (list 'calcFunc-now) (and zone (list zone))))))
1334
1335 (defun calcFunc-year (date)
1336 (car (math-date-to-dt date)))
1337
1338 (defun calcFunc-month (date)
1339 (nth 1 (math-date-to-dt date)))
1340
1341 (defun calcFunc-day (date)
1342 (nth 2 (math-date-to-dt date)))
1343
1344 (defun calcFunc-weekday (date)
1345 (if (eq (car-safe date) 'date)
1346 (setq date (nth 1 date)))
1347 (or (math-realp date)
1348 (math-reject-arg date 'datep))
1349 (math-mod (math-floor date) 7))
1350
1351 (defun calcFunc-yearday (date)
1352 (let ((dt (math-date-to-dt date)))
1353 (math-day-number (car dt) (nth 1 dt) (nth 2 dt))))
1354
1355 (defun calcFunc-hour (date)
1356 (if (eq (car-safe date) 'hms)
1357 (nth 1 date)
1358 (or (nth 3 (math-date-to-dt date)) 0)))
1359
1360 (defun calcFunc-minute (date)
1361 (if (eq (car-safe date) 'hms)
1362 (nth 2 date)
1363 (or (nth 4 (math-date-to-dt date)) 0)))
1364
1365 (defun calcFunc-second (date)
1366 (if (eq (car-safe date) 'hms)
1367 (nth 3 date)
1368 (or (nth 5 (math-date-to-dt date)) 0)))
1369
1370 (defun calcFunc-time (date)
1371 (let ((dt (math-date-to-dt date)))
1372 (if (nth 3 dt)
1373 (cons 'hms (nthcdr 3 dt))
1374 (list 'hms 0 0 0))))
1375
1376 (defun calcFunc-date (date &optional month day hour minute second)
1377 (and (math-messy-integerp month) (setq month (math-trunc month)))
1378 (and month (not (integerp month)) (math-reject-arg month 'fixnump))
1379 (and (math-messy-integerp day) (setq day (math-trunc day)))
1380 (and day (not (integerp day)) (math-reject-arg day 'fixnump))
1381 (if (and (eq (car-safe hour) 'hms) (not minute))
1382 (setq second (nth 3 hour)
1383 minute (nth 2 hour)
1384 hour (nth 1 hour)))
1385 (and (math-messy-integerp hour) (setq hour (math-trunc hour)))
1386 (and hour (not (integerp hour)) (math-reject-arg hour 'fixnump))
1387 (and (math-messy-integerp minute) (setq minute (math-trunc minute)))
1388 (and minute (not (integerp minute)) (math-reject-arg minute 'fixnump))
1389 (and (math-messy-integerp second) (setq second (math-trunc second)))
1390 (and second (not (math-realp second)) (math-reject-arg second 'realp))
1391 (if month
1392 (progn
1393 (and (math-messy-integerp date) (setq date (math-trunc date)))
1394 (and date (not (math-integerp date)) (math-reject-arg date 'integerp))
1395 (if day
1396 (if hour
1397 (list 'date (math-dt-to-date (list date month day hour
1398 (or minute 0)
1399 (or second 0))))
1400 (list 'date (math-dt-to-date (list date month day))))
1401 (list 'date (math-dt-to-date (list (math-this-year) date month)))))
1402 (if (math-realp date)
1403 (list 'date date)
1404 (if (eq (car date) 'date)
1405 (nth 1 date)
1406 (math-reject-arg date 'datep)))))
1407
1408 (defun calcFunc-julian (date &optional zone)
1409 (if (math-realp date)
1410 (list 'date (if (math-integerp date)
1411 (math-sub date math-julian-date-beginning-int)
1412 (setq date (math-sub date math-julian-date-beginning))
1413 (math-sub date (math-div (calcFunc-tzone zone date)
1414 '(float 864 2)))))
1415 (if (eq (car date) 'date)
1416 (math-add (nth 1 date) (if (math-integerp (nth 1 date))
1417 math-julian-date-beginning-int
1418 (math-add math-julian-date-beginning
1419 (math-div (calcFunc-tzone zone date)
1420 '(float 864 2)))))
1421 (math-reject-arg date 'datep))))
1422
1423 (defun calcFunc-unixtime (date &optional zone)
1424 (if (math-realp date)
1425 (progn
1426 (setq date (math-add 719164 (math-div date '(float 864 2))))
1427 (list 'date (math-sub date (math-div (calcFunc-tzone zone date)
1428 '(float 864 2)))))
1429 (if (eq (car date) 'date)
1430 (math-add (nth 1 (math-date-parts (nth 1 date) 719164))
1431 (calcFunc-tzone zone date))
1432 (math-reject-arg date 'datep))))
1433
1434
1435 ;;; Note: Longer names must appear before shorter names which are
1436 ;;; substrings of them.
1437 (defvar math-tzone-names
1438 '(( "UTC" 0 0)
1439 ( "MEGT" -1 "MET" "METDST" ) ; Middle Europe
1440 ( "METDST" -1 -1 ) ( "MET" -1 0 )
1441 ( "MEGZ" -1 "MEZ" "MESZ" ) ( "MEZ" -1 0 ) ( "MESZ" -1 -1 )
1442 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1443 ( "WETDST" 0 -1 ) ( "WET" 0 0 )
1444 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -1 ) ; Britain
1445 ( "NGT" (float 35 -1) "NST" "NDT" ) ; Newfoundland
1446 ( "NST" (float 35 -1) 0 ) ( "NDT" (float 35 -1) -1 )
1447 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -1 ) ; Atlantic
1448 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -1 ) ; Eastern
1449 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -1 ) ; Central
1450 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -1 ) ; Mountain
1451 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -1 ) ; Pacific
1452 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -1 ) ; Yukon
1453 )
1454 "No doc yet. See calc manual for now. ")
1455
1456 (defvar var-TimeZone nil)
1457
1458 ;; From cal-dst
1459 (defvar calendar-current-time-zone-cache)
1460
1461 (defvar math-calendar-tzinfo
1462 nil
1463 "Information about the timezone, retrieved from the calendar.")
1464
1465 (defun math-get-calendar-tzinfo ()
1466 "Get information about the timezone from the calendar.
1467 The result should be a list of two items about the current time zone:
1468 first, the number of seconds difference from GMT
1469 second, the number of seconds offset for daylight savings."
1470 (if math-calendar-tzinfo
1471 math-calendar-tzinfo
1472 (require 'cal-dst)
1473 (let ((tzinfo (progn
1474 (calendar-current-time-zone)
1475 calendar-current-time-zone-cache)))
1476 (setq math-calendar-tzinfo
1477 (list (* 60 (abs (nth 0 tzinfo)))
1478 (* 60 (nth 1 tzinfo)))))))
1479
1480 (defun calcFunc-tzone (&optional zone date)
1481 (if zone
1482 (cond ((math-realp zone)
1483 (math-round (math-mul zone 3600)))
1484 ((eq (car zone) 'hms)
1485 (math-round (math-mul (math-from-hms zone 'deg) 3600)))
1486 ((eq (car zone) '+)
1487 (math-add (calcFunc-tzone (nth 1 zone) date)
1488 (calcFunc-tzone (nth 2 zone) date)))
1489 ((eq (car zone) '-)
1490 (math-sub (calcFunc-tzone (nth 1 zone) date)
1491 (calcFunc-tzone (nth 2 zone) date)))
1492 ((eq (car zone) 'var)
1493 (let ((name (upcase (symbol-name (nth 1 zone))))
1494 found)
1495 (if (setq found (assoc name math-tzone-names))
1496 (calcFunc-tzone (math-add (nth 1 found)
1497 (if (integerp (nth 2 found))
1498 (nth 2 found)
1499 (or
1500 (math-daylight-savings-adjust
1501 date (car found))
1502 0)))
1503 date)
1504 (if (equal name "LOCAL")
1505 (calcFunc-tzone nil date)
1506 (math-reject-arg zone "*Unrecognized time zone name")))))
1507 (t (math-reject-arg zone "*Expected a time zone")))
1508 (if (calc-var-value 'var-TimeZone)
1509 (calcFunc-tzone (calc-var-value 'var-TimeZone) date)
1510 (let ((tzinfo (math-get-calendar-tzinfo)))
1511 (+ (nth 0 tzinfo)
1512 (* (math-cal-daylight-savings-adjust date) (nth 1 tzinfo)))))))
1513
1514 (defvar math-daylight-savings-hook 'math-std-daylight-savings)
1515
1516 (defun math-daylight-savings-adjust (date zone &optional dt)
1517 (or date (setq date (nth 1 (calcFunc-now))))
1518 (let (bump)
1519 (if (eq (car-safe date) 'date)
1520 (setq bump 0
1521 date (nth 1 date))
1522 (if (and date (math-realp date))
1523 (let ((zadj (assoc zone math-tzone-names)))
1524 (if zadj (setq bump -1
1525 date (math-sub date (math-div (nth 1 zadj)
1526 '(float 24 0))))))
1527 (math-reject-arg date 'datep)))
1528 (setq date (math-float date))
1529 (or dt (setq dt (math-date-to-dt date)))
1530 (and math-daylight-savings-hook
1531 (funcall math-daylight-savings-hook date dt zone bump))))
1532
1533 ;;; Based on part of dst-adjust-time in cal-dst.el
1534 ;;; For calcFunc-dst, when zone=nil
1535 (defun math-cal-daylight-savings-adjust (date)
1536 "Return -1 if DATE is using daylight saving, 0 otherwise."
1537 (require 'cal-dst)
1538 (unless date (setq date (calcFunc-now)))
1539 (let* ((dt (math-date-to-dt date))
1540 (time (cond
1541 ((nth 3 dt)
1542 (nth 3 dt))
1543 ((nth 4 dt)
1544 (+ (nth 3 dt) (/ (nth 4 dt) 60.0)))
1545 (t
1546 0)))
1547 (rounded-abs-date
1548 (+
1549 (calendar-absolute-from-gregorian
1550 (list (nth 1 dt) (nth 2 dt) (nth 0 dt)))
1551 (/ (round (* 60 time)) 60.0 24.0))))
1552 (if (dst-in-effect rounded-abs-date)
1553 -1
1554 0)))
1555
1556 (defun calcFunc-dsadj (date &optional zone)
1557 (if zone
1558 (or (eq (car-safe zone) 'var)
1559 (math-reject-arg zone "*Time zone variable expected"))
1560 (setq zone (calc-var-value 'var-TimeZone)))
1561 (if zone
1562 (progn
1563 (setq zone (and (eq (car-safe zone) 'var)
1564 (upcase (symbol-name (nth 1 zone)))))
1565 (let ((zadj (assoc zone math-tzone-names)))
1566 (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1567 (if (integerp (nth 2 zadj))
1568 (nth 2 zadj)
1569 (math-daylight-savings-adjust date zone))))
1570 (math-cal-daylight-savings-adjust date)))
1571
1572 ;; (defun calcFunc-dsadj (date &optional zone)
1573 ;; (if zone
1574 ;; (or (eq (car-safe zone) 'var)
1575 ;; (math-reject-arg zone "*Time zone variable expected"))
1576 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1577 ;; (progn
1578 ;; (calcFunc-tzone)
1579 ;; (calc-var-value 'var-TimeZone)))))
1580 ;; (setq zone (and (eq (car-safe zone) 'var)
1581 ;; (upcase (symbol-name (nth 1 zone)))))
1582 ;; (let ((zadj (assoc zone math-tzone-names)))
1583 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1584 ;; (if (integerp (nth 2 zadj))
1585 ;; (nth 2 zadj)
1586 ;; (math-daylight-savings-adjust date zone))))
1587
1588 (defun calcFunc-tzconv (date z1 z2)
1589 (if (math-realp date)
1590 (nth 1 (calcFunc-tzconv (list 'date date) z1 z2))
1591 (calcFunc-unixtime (calcFunc-unixtime date z1) z2)))
1592
1593 (defun math-std-daylight-savings (date dt zone bump)
1594 "Standard North American daylight saving algorithm.
1595 Before 2007, this uses `math-std-daylight-savings-old', where
1596 daylight saving began on the first Sunday of April at 2 a.m.,
1597 and ended on the last Sunday of October at 2 a.m.
1598 As of 2007, this uses `math-std-daylight-savings-new', where
1599 daylight saving begins on the second Sunday of March at 2 a.m.,
1600 and ends on the first Sunday of November at 2 a.m."
1601 (if (< (car dt) 2007)
1602 (math-std-daylight-savings-old date dt zone bump)
1603 (math-std-daylight-savings-new date dt zone bump)))
1604
1605 (defun math-std-daylight-savings-new (date dt zone bump)
1606 "Standard North American daylight saving algorithm as of 2007.
1607 This implements the rules for the U.S. and Canada.
1608 Daylight saving begins on the second Sunday of March at 2 a.m.,
1609 and ends on the first Sunday of November at 2 a.m."
1610 (cond ((< (nth 1 dt) 3) 0)
1611 ((= (nth 1 dt) 3)
1612 (let ((sunday (math-prev-weekday-in-month date dt 14 0)))
1613 (cond ((< (nth 2 dt) sunday) 0)
1614 ((= (nth 2 dt) sunday)
1615 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1616 (t -1))))
1617 ((< (nth 1 dt) 11) -1)
1618 ((= (nth 1 dt) 11)
1619 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1620 (cond ((< (nth 2 dt) sunday) -1)
1621 ((= (nth 2 dt) sunday)
1622 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1623 (t 0))))
1624 (t 0)))
1625
1626 (defun math-std-daylight-savings-old (date dt zone bump)
1627 "Standard North American daylight saving algorithm before 2007.
1628 This implements the rules for the U.S. and Canada.
1629 Daylight saving begins on the first Sunday of April at 2 a.m.,
1630 and ends on the last Sunday of October at 2 a.m."
1631 (cond ((< (nth 1 dt) 4) 0)
1632 ((= (nth 1 dt) 4)
1633 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1634 (cond ((< (nth 2 dt) sunday) 0)
1635 ((= (nth 2 dt) sunday)
1636 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1637 (t -1))))
1638 ((< (nth 1 dt) 10) -1)
1639 ((= (nth 1 dt) 10)
1640 (let ((sunday (math-prev-weekday-in-month date dt 31 0)))
1641 (cond ((< (nth 2 dt) sunday) -1)
1642 ((= (nth 2 dt) sunday)
1643 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1644 (t 0))))
1645 (t 0)))
1646
1647 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1648 ;;; day of the given month.
1649 (defun math-prev-weekday-in-month (date dt day wday)
1650 (or day (setq day (nth 2 dt)))
1651 (if (> day (math-days-in-month (car dt) (nth 1 dt)))
1652 (setq day (math-days-in-month (car dt) (nth 1 dt))))
1653 (let ((zeroth (math-sub (math-floor date) (nth 2 dt))))
1654 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day))) zeroth)))
1655
1656 (defun calcFunc-pwday (date &optional day weekday)
1657 (if (eq (car-safe date) 'date)
1658 (setq date (nth 1 date)))
1659 (or (math-realp date)
1660 (math-reject-arg date 'datep))
1661 (if (math-messy-integerp day) (setq day (math-trunc day)))
1662 (or (integerp day) (math-reject-arg day 'fixnump))
1663 (if (= day 0) (setq day 31))
1664 (and (or (< day 7) (> day 31)) (math-reject-arg day 'range))
1665 (math-prev-weekday-in-month date (math-date-to-dt date) day (or weekday 0)))
1666
1667
1668 (defun calcFunc-newweek (date &optional weekday)
1669 (if (eq (car-safe date) 'date)
1670 (setq date (nth 1 date)))
1671 (or (math-realp date)
1672 (math-reject-arg date 'datep))
1673 (or weekday (setq weekday 0))
1674 (and (math-messy-integerp weekday) (setq weekday (math-trunc weekday)))
1675 (or (integerp weekday) (math-reject-arg weekday 'fixnump))
1676 (and (or (< weekday 0) (> weekday 6)) (math-reject-arg weekday 'range))
1677 (setq date (math-floor date))
1678 (list 'date (math-sub date (calcFunc-weekday (math-sub date weekday)))))
1679
1680 (defun calcFunc-newmonth (date &optional day)
1681 (or day (setq day 1))
1682 (and (math-messy-integerp day) (setq day (math-trunc day)))
1683 (or (integerp day) (math-reject-arg day 'fixnump))
1684 (and (or (< day 0) (> day 31)) (math-reject-arg day 'range))
1685 (let* ((dt (math-date-to-dt date))
1686 (dim (math-days-in-month (car dt) (nth 1 dt)))
1687 (julian (if calc-gregorian-switch
1688 (math-date-to-dt (math-sub
1689 (or (nth 3 calc-gregorian-switch)
1690 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))
1691 1)))))
1692 (if (or (= day 0) (> day dim))
1693 (setq day (1- dim))
1694 (setq day (1- day)))
1695 ;; Adjust if this occurs near the switch to the Gregorian calendar
1696 (if calc-gregorian-switch
1697 (cond
1698 ((and (math-dt-before-p (list (car dt) (nth 1 dt) 1) calc-gregorian-switch)
1699 (math-dt-before-p julian (list (car dt) (nth 1 dt) 1)))
1700 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the month
1701 (list 'date
1702 (math-dt-to-date (list (car calc-gregorian-switch)
1703 (nth 1 calc-gregorian-switch)
1704 (if (> (+ (nth 2 calc-gregorian-switch) day) dim)
1705 dim
1706 (+ (nth 2 calc-gregorian-switch) day))))))
1707 ((and (eq (car dt) (car calc-gregorian-switch))
1708 (= (nth 1 dt) (nth 1 calc-gregorian-switch)))
1709 ;; In this case, the switch to the Gregorian calendar occurs in the given month
1710 (if (< (+ (nth 2 julian) day) (nth 2 calc-gregorian-switch))
1711 ;; If the DAYth day occurs before the switch, use it
1712 (list 'date (math-dt-to-date (list (car dt) (nth 1 dt) (1+ day))))
1713 ;; Otherwise do some computations
1714 (let ((tm (+ day (- (nth 2 calc-gregorian-switch) (nth 2 julian)))))
1715 (list 'date (math-dt-to-date
1716 (list (car dt)
1717 (nth 1 dt)
1718 ;;
1719 (if (> tm dim) dim tm)))))))
1720 ((and (eq (car dt) (car julian))
1721 (= (nth 1 dt) (nth 1 julian)))
1722 ;; In this case, the current month is truncated because of the switch
1723 ;; to the Gregorian calendar
1724 (list 'date (math-dt-to-date
1725 (list (car dt)
1726 (nth 1 dt)
1727 (if (>= day (nth 2 julian))
1728 (nth 2 julian)
1729 (1+ day))))))
1730 (t
1731 ;; The default
1732 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) day))))
1733 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) day)))))
1734
1735 (defun calcFunc-newyear (date &optional day)
1736 (if (eq (car-safe date) 'date) (setq date (nth 1 date)))
1737 (or day (setq day 1))
1738 (and (math-messy-integerp day) (setq day (math-trunc day)))
1739 (or (integerp day) (math-reject-arg day 'fixnump))
1740 (let* ((dt (math-date-to-dt date))
1741 (gregbeg (if calc-gregorian-switch
1742 (or (nth 3 calc-gregorian-switch)
1743 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))))
1744 (julianend (if calc-gregorian-switch (math-sub gregbeg 1)))
1745 (julian (if calc-gregorian-switch
1746 (math-date-to-dt julianend))))
1747 (if (and (>= day 0) (<= day 366))
1748 (let ((max (if (math-leap-year-p (car dt)) 366 365)))
1749 (if (or (= day 0) (> day max)) (setq day max))
1750 (if calc-gregorian-switch
1751 ;; Now to break this down into cases
1752 (cond
1753 ((and (math-dt-before-p (list (car dt) 1 1) calc-gregorian-switch)
1754 (math-dt-before-p julian (list (car dt) 1 1)))
1755 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the year
1756 (list 'date (math-min (math-add gregbeg (1- day))
1757 (math-dt-to-date (list (car calc-gregorian-switch) 12 31)))))
1758 ((eq (car dt) (car julian))
1759 ;; In this case, the switch to the Gregorian calendar occurs in the given year
1760 (if (Math-lessp (car julian) (car calc-gregorian-switch))
1761 ;; Here, the last Julian day is the last day of the year.
1762 (list 'date (math-min (math-add (math-dt-to-date (list (car dt) 1 1)) (1- day))
1763 julianend))
1764 ;; Otherwise, just make sure the date doesn't go past the end of the year
1765 (list 'date (math-min (math-add (math-dt-to-date (list (car dt) 1 1)) (1- day))
1766 (math-dt-to-date (list (car dt) 12 31))))))
1767 (t
1768 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1769 (1- day)))))
1770 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1771 (1- day)))))
1772 (if (and (>= day -12) (<= day -1))
1773 (if (and calc-gregorian-switch
1774 (math-dt-before-p (list (car dt) (- day) 1) calc-gregorian-switch)
1775 (math-dt-before-p julian (list (car dt) (- day) 1)))
1776 (list 'date gregbeg)
1777 (list 'date (math-dt-to-date (list (car dt) (- day) 1))))
1778 (math-reject-arg day 'range)))))
1779
1780 (defun calcFunc-incmonth (date &optional step)
1781 (or step (setq step 1))
1782 (and (math-messy-integerp step) (setq step (math-trunc step)))
1783 (or (math-integerp step) (math-reject-arg step 'integerp))
1784 (let* ((dt (math-date-to-dt date))
1785 (year (car dt))
1786 (month (math-add (1- (nth 1 dt)) step))
1787 (extra (calcFunc-idiv month 12))
1788 (day (nth 2 dt)))
1789 (setq month (1+ (math-sub month (math-mul extra 12)))
1790 year (math-add year extra)
1791 day (min day (math-days-in-month year month)))
1792 (and (math-posp (car dt)) (not (math-posp year))
1793 (setq year (math-sub year 1))) ; did we go past the year zero?
1794 (and (math-negp (car dt)) (not (math-negp year))
1795 (setq year (math-add year 1)))
1796 (list 'date (math-dt-to-date
1797 (cons year (cons month (cons day (cdr (cdr (cdr dt))))))))))
1798
1799 (defun calcFunc-incyear (date &optional step)
1800 (calcFunc-incmonth date (math-mul (or step 1) 12)))
1801
1802
1803
1804 (defun calcFunc-bsub (a b)
1805 (or (eq (car-safe a) 'date)
1806 (math-reject-arg a 'datep))
1807 (if (eq (car-safe b) 'date)
1808 (if (math-lessp (nth 1 a) (nth 1 b))
1809 (math-neg (calcFunc-bsub b a))
1810 (math-setup-holidays b)
1811 (let* ((da (math-to-business-day a))
1812 (db (math-to-business-day b)))
1813 (math-add (math-sub (car da) (car db))
1814 (if (and (cdr db) (not (cdr da))) 1 0))))
1815 (calcFunc-badd a (math-neg b))))
1816
1817 (defvar math-holidays-cache nil)
1818 (defvar math-holidays-cache-tag t)
1819 (defun calcFunc-badd (a b)
1820 (if (eq (car-safe b) 'date)
1821 (if (eq (car-safe a) 'date)
1822 (math-reject-arg nil "*Invalid combination in date arithmetic")
1823 (calcFunc-badd b a))
1824 (if (eq (car-safe a) 'date)
1825 (if (Math-realp b)
1826 (if (Math-zerop b)
1827 a
1828 (let* ((d (math-to-business-day a))
1829 (bb (math-add (car d)
1830 (if (and (cdr d) (Math-posp b))
1831 (math-sub b 1) b))))
1832 (or (math-from-business-day bb)
1833 (calcFunc-badd a b))))
1834 (if (eq (car-safe b) 'hms)
1835 (let ((hours (nth 7 math-holidays-cache)))
1836 (setq b (math-div (math-from-hms b 'deg) 24))
1837 (if hours
1838 (setq b (math-div b (cdr hours))))
1839 (calcFunc-badd a b))
1840 (math-reject-arg nil "*Invalid combination in date arithmetic")))
1841 (math-reject-arg a 'datep))))
1842
1843 (defun calcFunc-holiday (a)
1844 (if (cdr (math-to-business-day a)) 1 0))
1845
1846 ;;; Compute the number of business days since Jan 1, 1 AD.
1847
1848 (defun math-to-business-day (date &optional need-year)
1849 (if (eq (car-safe date) 'date)
1850 (setq date (nth 1 date)))
1851 (or (Math-realp date)
1852 (math-reject-arg date 'datep))
1853 (let* ((day (math-floor date))
1854 (time (math-sub date day))
1855 (dt (math-date-to-dt day))
1856 (delta 0)
1857 (holiday nil))
1858 (or (not need-year) (eq (car dt) need-year)
1859 (math-reject-arg (list 'date day) "*Generated holiday has wrong year"))
1860 (math-setup-holidays date)
1861 (let ((days (car math-holidays-cache)))
1862 (while (and (setq days (cdr days)) (< (car days) day))
1863 (setq delta (1+ delta)))
1864 (and days (= day (car days))
1865 (setq holiday t)))
1866 (let* ((weekdays (nth 3 math-holidays-cache))
1867 (weeks (1- (/ (+ day 6) 7)))
1868 (wkday (- day 1 (* weeks 7))))
1869 (setq delta (+ delta (* weeks (length weekdays))))
1870 (while (and weekdays (< (car weekdays) wkday))
1871 (setq weekdays (cdr weekdays)
1872 delta (1+ delta)))
1873 (and weekdays (eq wkday (car weekdays))
1874 (setq holiday t)))
1875 (let ((hours (nth 7 math-holidays-cache)))
1876 (if hours
1877 (progn
1878 (setq time (math-div (math-sub time (car hours)) (cdr hours)))
1879 (if (Math-lessp time 0) (setq time 0))
1880 (or (Math-lessp time 1)
1881 (setq time
1882 (math-sub 1
1883 (math-div 1 (math-mul 86400 (cdr hours)))))))))
1884 (cons (math-add (math-sub day delta) time) holiday)))
1885
1886
1887 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1888 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1889
1890 (defun math-from-business-day (num)
1891 (let* ((day (math-floor num))
1892 (time (math-sub num day)))
1893 (or (integerp day)
1894 (math-reject-arg nil "*Date is outside valid range"))
1895 (math-setup-holidays)
1896 (let ((days (nth 1 math-holidays-cache))
1897 (delta 0))
1898 (while (and (setq days (cdr days)) (< (car days) day))
1899 (setq delta (1+ delta)))
1900 (setq day (+ day delta)))
1901 (let* ((weekdays (nth 3 math-holidays-cache))
1902 (bweek (- 7 (length weekdays)))
1903 (weeks (1- (/ (+ day (1- bweek)) bweek)))
1904 (wkday (- day 1 (* weeks bweek)))
1905 (w 0))
1906 (setq day (+ day (* weeks (length weekdays))))
1907 (while (if (memq w weekdays)
1908 (setq day (1+ day))
1909 (> (setq wkday (1- wkday)) 0))
1910 (setq w (1+ w)))
1911 (let ((hours (nth 7 math-holidays-cache)))
1912 (if hours
1913 (setq time (math-add (math-mul time (cdr hours)) (car hours)))))
1914 (and (not (math-setup-holidays day))
1915 (list 'date (math-add day time))))))
1916
1917 ;; The variable math-sh-year is local to math-setup-holidays
1918 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1919 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1920 (defvar math-sh-year)
1921
1922 (defun math-setup-holidays (&optional date)
1923 (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag)
1924 (let ((h (calc-var-value 'var-Holidays))
1925 (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3)
1926 (thu . 4) (fri . 5) (sat . 6) ))
1927 (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil))
1928 (or (math-vectorp h)
1929 (math-reject-arg h "*Holidays variable must be a vector"))
1930 (while (setq h (cdr h))
1931 (cond ((or (and (eq (car-safe (car h)) 'date)
1932 (integerp (nth 1 (car h))))
1933 (and (eq (car-safe (car h)) 'intv)
1934 (eq (car-safe (nth 2 (car h))) 'date))
1935 (eq (car-safe (car h)) 'vec))
1936 (setq days (cons (car h) days)))
1937 ((and (eq (car-safe (car h)) 'var)
1938 (assq (nth 1 (car h)) wdnames))
1939 (setq weekdays (cons (cdr (assq (nth 1 (car h)) wdnames))
1940 weekdays)))
1941 ((and (eq (car-safe (car h)) 'intv)
1942 (eq (car-safe (nth 2 (car h))) 'hms)
1943 (eq (car-safe (nth 3 (car h))) 'hms))
1944 (if hours
1945 (math-reject-arg
1946 (car h) "*Only one hours interval allowed in Holidays"))
1947 (setq hours (math-div (car h) '(hms 24 0 0)))
1948 (if (or (Math-lessp (nth 2 hours) 0)
1949 (Math-lessp 1 (nth 3 hours)))
1950 (math-reject-arg
1951 (car h) "*Hours interval out of range"))
1952 (setq hours (cons (nth 2 hours)
1953 (math-sub (nth 3 hours) (nth 2 hours))))
1954 (if (Math-zerop (cdr hours))
1955 (math-reject-arg
1956 (car h) "*Degenerate hours interval")))
1957 ((or (and (eq (car-safe (car h)) 'intv)
1958 (Math-integerp (nth 2 (car h)))
1959 (Math-integerp (nth 3 (car h))))
1960 (and (integerp (car h))
1961 (> (car h) 1900) (< (car h) 2100)))
1962 (if limit
1963 (math-reject-arg
1964 (car h) "*Only one limit allowed in Holidays"))
1965 (setq limit (calcFunc-vint (car h) '(intv 3 1 2737)))
1966 (if (equal limit '(vec))
1967 (math-reject-arg (car h) "*Limit is out of range")))
1968 ((or (math-expr-contains (car h) '(var y var-y))
1969 (math-expr-contains (car h) '(var m var-m)))
1970 (setq exprs (cons (car h) exprs)))
1971 (t (math-reject-arg
1972 (car h) "*Holidays must contain a vector of holidays"))))
1973 (if (= (length weekdays) 7)
1974 (math-reject-arg nil "*Too many weekend days"))
1975 (setq math-holidays-cache (list (list -1) ; 0: days list
1976 (list -1) ; 1: inverse-days list
1977 nil ; 2: exprs
1978 (sort weekdays '<)
1979 (or limit '(intv 3 1 2737))
1980 nil ; 5: (lo.hi) expanded years
1981 (cons exprs days)
1982 hours) ; 7: business hours
1983 math-holidays-cache-tag (calc-var-value 'var-Holidays))))
1984 (if date
1985 (let ((year (calcFunc-year date))
1986 (limits (nth 5 math-holidays-cache))
1987 (done nil))
1988 (or (eq (calcFunc-in year (nth 4 math-holidays-cache)) 1)
1989 (progn
1990 (or (eq (car-safe date) 'date) (setq date (list 'date date)))
1991 (math-reject-arg date "*Date is outside valid range")))
1992 (unwind-protect
1993 (let ((days (nth 6 math-holidays-cache)))
1994 (if days
1995 (let ((math-sh-year nil)) ; see below
1996 (setcar (nthcdr 6 math-holidays-cache) nil)
1997 (math-setup-add-holidays (cons 'vec (cdr days)))
1998 (setcar (nthcdr 2 math-holidays-cache) (car days))))
1999 (cond ((not (nth 2 math-holidays-cache))
2000 (setq done t)
2001 nil)
2002 ((not limits)
2003 (setcar (nthcdr 5 math-holidays-cache) (cons year year))
2004 (math-setup-year-holidays year)
2005 (setq done t))
2006 ((< year (car limits))
2007 (message "Computing holidays, %d .. %d"
2008 year (1- (car limits)))
2009 (calc-set-command-flag 'clear-message)
2010 (while (< year (car limits))
2011 (setcar limits (1- (car limits)))
2012 (math-setup-year-holidays (car limits)))
2013 (setq done t))
2014 ((> year (cdr limits))
2015 (message "Computing holidays, %d .. %d"
2016 (1+ (cdr limits)) year)
2017 (calc-set-command-flag 'clear-message)
2018 (while (> year (cdr limits))
2019 (setcdr limits (1+ (cdr limits)))
2020 (math-setup-year-holidays (cdr limits)))
2021 (setq done t))
2022 (t
2023 (setq done t)
2024 nil)))
2025 (or done (setq math-holidays-cache-tag t))))))
2026
2027 (defun math-setup-year-holidays (math-sh-year)
2028 (let ((exprs (nth 2 math-holidays-cache)))
2029 (while exprs
2030 (let* ((var-y math-sh-year)
2031 (var-m nil)
2032 (expr (math-evaluate-expr (car exprs))))
2033 (if (math-expr-contains expr '(var m var-m))
2034 (let ((var-m 0))
2035 (while (<= (setq var-m (1+ var-m)) 12)
2036 (math-setup-add-holidays (math-evaluate-expr expr))))
2037 (math-setup-add-holidays expr)))
2038 (setq exprs (cdr exprs)))))
2039
2040 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
2041 (cond ((eq (car-safe days) 'vec)
2042 (while (setq days (cdr days))
2043 (math-setup-add-holidays (car days))))
2044 ((eq (car-safe days) 'intv)
2045 (let ((day (math-ceiling (nth 2 days))))
2046 (or (eq (calcFunc-in day days) 1)
2047 (setq day (math-add day 1)))
2048 (while (eq (calcFunc-in day days) 1)
2049 (math-setup-add-holidays day)
2050 (setq day (math-add day 1)))))
2051 ((eq (car-safe days) 'date)
2052 (math-setup-add-holidays (nth 1 days)))
2053 ((eq days 0))
2054 ((integerp days)
2055 (let ((b (math-to-business-day days math-sh-year)))
2056 (or (cdr b) ; don't register holidays twice!
2057 (let ((prev (car math-holidays-cache))
2058 (iprev (nth 1 math-holidays-cache)))
2059 (while (and (cdr prev) (< (nth 1 prev) days))
2060 (setq prev (cdr prev) iprev (cdr iprev)))
2061 (setcdr prev (cons days (cdr prev)))
2062 (setcdr iprev (cons (car b) (cdr iprev)))
2063 (while (setq iprev (cdr iprev))
2064 (setcar iprev (1- (car iprev))))))))
2065 ((Math-realp days)
2066 (math-reject-arg (list 'date days) "*Invalid holiday value"))
2067 (t
2068 (math-reject-arg days "*Holiday formula failed to evaluate"))))
2069
2070
2071
2072
2073 ;;;; Error forms.
2074
2075 ;;; Build a standard deviation form. [X X X]
2076 (defun math-make-sdev (x sigma)
2077 (if (memq (car-safe x) '(date mod sdev intv vec))
2078 (math-reject-arg x 'realp))
2079 (if (memq (car-safe sigma) '(date mod sdev intv vec))
2080 (math-reject-arg sigma 'realp))
2081 (if (or (Math-negp sigma) (memq (car-safe sigma) '(cplx polar)))
2082 (setq sigma (math-abs sigma)))
2083 (if (and (Math-zerop sigma) (Math-scalarp x))
2084 x
2085 (list 'sdev x sigma)))
2086 (defun calcFunc-sdev (x sigma)
2087 (math-make-sdev x sigma))
2088
2089
2090
2091 ;;;; Modulo forms.
2092
2093 (defun math-normalize-mod (a)
2094 (let ((n (math-normalize (nth 1 a)))
2095 (m (math-normalize (nth 2 a))))
2096 (if (and (math-anglep n) (math-anglep m) (math-posp m))
2097 (math-make-mod n m)
2098 (math-normalize (list 'calcFunc-makemod n m)))))
2099
2100 ;;; Build a modulo form. [N R R]
2101 (defun math-make-mod (n m)
2102 (setq calc-previous-modulo m)
2103 (and n
2104 (cond ((not (Math-anglep m))
2105 (math-reject-arg m 'anglep))
2106 ((not (math-posp m))
2107 (math-reject-arg m 'posp))
2108 ((Math-anglep n)
2109 (if (or (Math-negp n)
2110 (not (Math-lessp n m)))
2111 (list 'mod (math-mod n m) m)
2112 (list 'mod n m)))
2113 ((memq (car n) '(+ - / vec neg))
2114 (math-normalize
2115 (cons (car n)
2116 (mapcar (function (lambda (x) (math-make-mod x m)))
2117 (cdr n)))))
2118 ((and (eq (car n) '*) (Math-anglep (nth 1 n)))
2119 (math-mul (math-make-mod (nth 1 n) m) (nth 2 n)))
2120 ((memq (car n) '(* ^ var calcFunc-subscr))
2121 (math-mul (math-make-mod 1 m) n))
2122 (t (math-reject-arg n 'anglep)))))
2123 (defun calcFunc-makemod (n m)
2124 (math-make-mod n m))
2125
2126
2127
2128 ;;;; Interval forms.
2129
2130 ;;; Build an interval form. [X S X X]
2131 (defun math-make-intv (mask lo hi)
2132 (if (memq (car-safe lo) '(cplx polar mod sdev intv vec))
2133 (math-reject-arg lo 'realp))
2134 (if (memq (car-safe hi) '(cplx polar mod sdev intv vec))
2135 (math-reject-arg hi 'realp))
2136 (or (eq (eq (car-safe lo) 'date) (eq (car-safe hi) 'date))
2137 (math-reject-arg (if (eq (car-safe lo) 'date) hi lo) 'datep))
2138 (if (and (or (Math-realp lo) (eq (car lo) 'date))
2139 (or (Math-realp hi) (eq (car hi) 'date)))
2140 (let ((cmp (math-compare lo hi)))
2141 (if (= cmp 0)
2142 (if (= mask 3)
2143 lo
2144 (list 'intv mask lo hi))
2145 (if (> cmp 0)
2146 (if (= mask 3)
2147 (list 'intv 2 lo lo)
2148 (list 'intv mask lo lo))
2149 (list 'intv mask lo hi))))
2150 (list 'intv mask lo hi)))
2151 (defun calcFunc-intv (mask lo hi)
2152 (if (math-messy-integerp mask) (setq mask (math-trunc mask)))
2153 (or (natnump mask) (math-reject-arg mask 'fixnatnump))
2154 (or (<= mask 3) (math-reject-arg mask 'range))
2155 (math-make-intv mask lo hi))
2156
2157 (defun math-sort-intv (mask lo hi)
2158 (if (Math-lessp hi lo)
2159 (math-make-intv (aref [0 2 1 3] mask) hi lo)
2160 (math-make-intv mask lo hi)))
2161
2162
2163
2164
2165 (defun math-combine-intervals (a am b bm c cm d dm)
2166 (let (res)
2167 (if (= (setq res (math-compare a c)) 1)
2168 (setq a c am cm)
2169 (if (= res 0)
2170 (setq am (or am cm))))
2171 (if (= (setq res (math-compare b d)) -1)
2172 (setq b d bm dm)
2173 (if (= res 0)
2174 (setq bm (or bm dm))))
2175 (math-make-intv (+ (if am 2 0) (if bm 1 0)) a b)))
2176
2177
2178 (defun math-div-mod (a b m) ; [R R R R] (Returns nil if no solution)
2179 (and (Math-integerp a) (Math-integerp b) (Math-integerp m)
2180 (let ((u1 1) (u3 b) (v1 0) (v3 m))
2181 (while (not (eq v3 0)) ; See Knuth sec 4.5.2, exercise 15
2182 (let* ((q (math-idivmod u3 v3))
2183 (t1 (math-sub u1 (math-mul v1 (car q)))))
2184 (setq u1 v1 u3 v3 v1 t1 v3 (cdr q))))
2185 (let ((q (math-idivmod a u3)))
2186 (and (eq (cdr q) 0)
2187 (math-mod (math-mul (car q) u1) m))))))
2188
2189 (defun math-mod-intv (a b)
2190 (let* ((q1 (math-floor (math-div (nth 2 a) b)))
2191 (q2 (math-floor (math-div (nth 3 a) b)))
2192 (m1 (math-sub (nth 2 a) (math-mul q1 b)))
2193 (m2 (math-sub (nth 3 a) (math-mul q2 b))))
2194 (cond ((equal q1 q2)
2195 (math-sort-intv (nth 1 a) m1 m2))
2196 ((and (math-equal-int (math-sub q2 q1) 1)
2197 (math-zerop m2)
2198 (memq (nth 1 a) '(0 2)))
2199 (math-make-intv (nth 1 a) m1 b))
2200 (t
2201 (math-make-intv 2 0 b)))))
2202
2203 ;; The variables math-exp-str and math-exp-pos are local to
2204 ;; math-read-exprs in math-aent.el, but are used by
2205 ;; math-read-angle-brackets, which is called (indirectly) by
2206 ;; math-read-exprs.
2207 (defvar math-exp-str)
2208 (defvar math-exp-pos)
2209
2210 (defun math-read-angle-brackets ()
2211 (let* ((last (or (math-check-for-commas t) (length math-exp-str)))
2212 (str (substring math-exp-str math-exp-pos last))
2213 (res
2214 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str)
2215 (let ((str1 (substring str 0 (1- (match-end 0))))
2216 (str2 (substring str (match-end 0)))
2217 (calc-hashes-used 0))
2218 (setq str1 (math-read-expr (concat "[" str1 "]")))
2219 (if (eq (car-safe str1) 'error)
2220 str1
2221 (setq str2 (math-read-expr str2))
2222 (if (eq (car-safe str2) 'error)
2223 str2
2224 (append '(calcFunc-lambda) (cdr str1) (list str2)))))
2225 (if (string-match "#" str)
2226 (let ((calc-hashes-used 0))
2227 (and (setq str (math-read-expr str))
2228 (if (eq (car-safe str) 'error)
2229 str
2230 (append '(calcFunc-lambda)
2231 (calc-invent-args calc-hashes-used)
2232 (list str)))))
2233 (math-parse-date str)))))
2234 (if (stringp res)
2235 (throw 'syntax res))
2236 (if (eq (car-safe res) 'error)
2237 (throw 'syntax (nth 2 res)))
2238 (setq math-exp-pos (1+ last))
2239 (math-read-token)
2240 res))
2241
2242 (provide 'calc-forms)
2243
2244 ;;; calc-forms.el ends here