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