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