]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-forms.el
(calc-original-buffer): Declare it.
[gnu-emacs] / lisp / calc / calc-forms.el
1 ;;; calc-forms.el --- data format conversion functions for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2004 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
17
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; This file is autoloaded from calc-ext.el.
31 (require 'calc-ext)
32
33 (require 'calc-macs)
34
35 (defun calc-Need-calc-forms () nil)
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-int (substring time 11 13))
46 (string-to-int (substring time 14 16))
47 (string-to-int (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 (equal fmt "")
84 (setq fmt "1"))
85 (if (string-match "\\` *[0-9] *\\'" fmt)
86 (setq fmt (nth (string-to-int fmt) calc-standard-date-formats)))
87 (or (string-match "[a-zA-Z]" fmt)
88 (error "Bad date format specifier"))
89 (and arg
90 (>= (setq arg (prefix-numeric-value arg)) 0)
91 (<= arg 9)
92 (setq calc-standard-date-formats
93 (copy-sequence calc-standard-date-formats))
94 (setcar (nthcdr arg calc-standard-date-formats) fmt))
95 (let ((case-fold-search nil))
96 (and (not (string-match "<.*>" fmt))
97 (string-match "\\`[^hHspP]*\\([^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*[bBhHmpPsS]+[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*\\)[^hHspP]*\\'" fmt)
98 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
99 (regexp-quote (math-match-substring fmt 1))
100 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt)
101 (setq fmt (concat (substring fmt 0 (match-beginning 0))
102 "<"
103 (substring fmt (match-beginning 0) (match-end 0))
104 ">"
105 (substring fmt (match-end 0))))))
106 (let ((lfmt nil)
107 (fullfmt nil)
108 (time nil)
109 pos pos2 sym temp)
110 (let ((case-fold-search nil))
111 (and (setq temp (string-match ":[BS]S" fmt))
112 (aset fmt temp ?C)))
113 (while (setq pos (string-match "[<>a-zA-Z]" fmt))
114 (if (> pos 0)
115 (setq lfmt (cons (substring fmt 0 pos) lfmt)))
116 (setq pos2 (1+ pos))
117 (cond ((= (aref fmt pos) ?\<)
118 (and time (error "Nested <'s not allowed"))
119 (and lfmt (setq fullfmt (nconc lfmt fullfmt)
120 lfmt nil))
121 (setq time t))
122 ((= (aref fmt pos) ?\>)
123 (or time (error "Misplaced > in format"))
124 (and lfmt (setq fullfmt (cons (nreverse lfmt) fullfmt)
125 lfmt nil))
126 (setq time nil))
127 (t
128 (if (string-match "\\`[^a-zA-Z]*[bB][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 aa AA aaa AAA aaaa AAAA
137 bb BB bbb BBB bbbb BBBB
138 M MM BM mmm Mmm Mmmm MMM MMMM
139 D DD BD d ddd bdd
140 W www Www Wwww WWW WWWW
141 h hh bh H HH BH
142 p P pp PP pppp PPPP
143 m mm bm s ss bss SS BS C
144 N n J j U b))
145 (and (eq sym 'X) (not lfmt) (not fullfmt))
146 (error "Bad format code: %s" sym))
147 (and (memq sym '(bb BB bbb BBB bbbb BBBB))
148 (setq lfmt (cons 'b lfmt)))
149 (setq lfmt (cons sym lfmt))))
150 (setq fmt (substring fmt pos2)))
151 (or (equal fmt "")
152 (setq lfmt (cons fmt lfmt)))
153 (and lfmt (if time
154 (setq fullfmt (cons (nreverse lfmt) fullfmt))
155 (setq fullfmt (nconc lfmt fullfmt))))
156 (calc-change-mode 'calc-date-format (nreverse fullfmt) t))))
157
158
159 (defun calc-hms-mode ()
160 (interactive)
161 (calc-wrapper
162 (calc-change-mode 'calc-angle-mode 'hms)
163 (message "Angles measured in degrees-minutes-seconds")))
164
165
166 (defun calc-now (arg)
167 (interactive "P")
168 (calc-date-zero-args "now" 'calcFunc-now arg))
169
170 (defun calc-date-part (arg)
171 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
172 (if (or (< arg 1) (> arg 9))
173 (error "Part code out of range"))
174 (calc-wrapper
175 (calc-enter-result 1
176 (nth arg '(nil "year" "mnth" "day" "hour" "minu"
177 "sec" "wday" "yday" "hmst"))
178 (list (nth arg '(nil calcFunc-year calcFunc-month
179 calcFunc-day calcFunc-hour
180 calcFunc-minute calcFunc-second
181 calcFunc-weekday calcFunc-yearday
182 calcFunc-time))
183 (calc-top-n 1)))))
184
185 (defun calc-date (arg)
186 (interactive "p")
187 (if (or (< arg 1) (> arg 6))
188 (error "Between one and six arguments are allowed"))
189 (calc-wrapper
190 (calc-enter-result arg "date" (cons 'calcFunc-date (calc-top-list-n arg)))))
191
192 (defun calc-julian (arg)
193 (interactive "P")
194 (calc-date-one-arg "juln" 'calcFunc-julian arg))
195
196 (defun calc-unix-time (arg)
197 (interactive "P")
198 (calc-date-one-arg "unix" 'calcFunc-unixtime arg))
199
200 (defun calc-time-zone (arg)
201 (interactive "P")
202 (calc-date-zero-args "zone" 'calcFunc-tzone arg))
203
204 (defun calc-convert-time-zones (old &optional new)
205 (interactive "sFrom time zone: ")
206 (calc-wrapper
207 (if (equal old "$")
208 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv (calc-top-list-n 3)))
209 (if (equal old "") (setq old "local"))
210 (or new
211 (setq new (read-string (concat "From time zone: " old
212 ", to zone: "))))
213 (if (stringp old) (setq old (math-read-expr old)))
214 (if (eq (car-safe old) 'error)
215 (error "Error in expression: %S" (nth 1 old)))
216 (if (equal new "") (setq new "local"))
217 (if (stringp new) (setq new (math-read-expr new)))
218 (if (eq (car-safe new) 'error)
219 (error "Error in expression: %S" (nth 1 new)))
220 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
221 (calc-top-n 1) old new)))))
222
223 (defun calc-new-week (arg)
224 (interactive "P")
225 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg))
226
227 (defun calc-new-month (arg)
228 (interactive "P")
229 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg))
230
231 (defun calc-new-year (arg)
232 (interactive "P")
233 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg))
234
235 (defun calc-inc-month (arg)
236 (interactive "p")
237 (calc-date-one-arg "incm" 'calcFunc-incmonth arg))
238
239 (defun calc-business-days-plus (arg)
240 (interactive "P")
241 (calc-wrapper
242 (calc-binary-op "bus+" 'calcFunc-badd arg)))
243
244 (defun calc-business-days-minus (arg)
245 (interactive "P")
246 (calc-wrapper
247 (calc-binary-op "bus-" 'calcFunc-bsub arg)))
248
249 (defun calc-date-zero-args (prefix func arg)
250 (calc-wrapper
251 (if (consp arg)
252 (calc-enter-result 1 prefix (list func (calc-top-n 1)))
253 (calc-enter-result 0 prefix (if arg
254 (list func (prefix-numeric-value arg))
255 (list func))))))
256
257 (defun calc-date-one-arg (prefix func arg)
258 (calc-wrapper
259 (if (consp arg)
260 (calc-enter-result 2 prefix (cons func (calc-top-list-n 2)))
261 (calc-enter-result 1 prefix (if arg
262 (list func (calc-top-n 1)
263 (prefix-numeric-value arg))
264 (list func (calc-top-n 1)))))))
265
266
267 ;;;; Hours-minutes-seconds forms.
268
269 (defun math-normalize-hms (a)
270 (let ((h (math-normalize (nth 1 a)))
271 (m (math-normalize (nth 2 a)))
272 (s (let ((calc-internal-prec (max (- calc-internal-prec 4) 3)))
273 (math-normalize (nth 3 a)))))
274 (if (math-negp h)
275 (progn
276 (if (math-posp s)
277 (setq s (math-add s -60)
278 m (math-add m 1)))
279 (if (math-posp m)
280 (setq m (math-add m -60)
281 h (math-add h 1)))
282 (if (not (Math-lessp -60 s))
283 (setq s (math-add s 60)
284 m (math-add m -1)))
285 (if (not (Math-lessp -60 m))
286 (setq m (math-add m 60)
287 h (math-add h -1))))
288 (if (math-negp s)
289 (setq s (math-add s 60)
290 m (math-add m -1)))
291 (if (math-negp m)
292 (setq m (math-add m 60)
293 h (math-add h -1)))
294 (if (not (Math-lessp s 60))
295 (setq s (math-add s -60)
296 m (math-add m 1)))
297 (if (not (Math-lessp m 60))
298 (setq m (math-add m -60)
299 h (math-add h 1))))
300 (if (and (eq (car-safe s) 'float)
301 (<= (+ (math-numdigs (nth 1 s)) (nth 2 s))
302 (- 2 calc-internal-prec)))
303 (setq s 0))
304 (list 'hms h m s)))
305
306 ;;; Convert A from ANG or current angular mode to HMS format.
307 (defun math-to-hms (a &optional ang) ; [X R] [Public]
308 (cond ((eq (car-safe a) 'hms) a)
309 ((eq (car-safe a) 'sdev)
310 (math-make-sdev (math-to-hms (nth 1 a))
311 (math-to-hms (nth 2 a))))
312 ((not (Math-numberp a))
313 (list 'calcFunc-hms a))
314 ((math-negp a)
315 (math-neg (math-to-hms (math-neg a) ang)))
316 ((eq (or ang calc-angle-mode) 'rad)
317 (math-to-hms (math-div a (math-pi-over-180)) 'deg))
318 ((memq (car-safe a) '(cplx polar)) a)
319 (t
320 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
321 ; (math-normalize a)))
322 (math-normalize
323 (let* ((b (math-mul a 3600))
324 (hm (math-trunc (math-div b 60)))
325 (hmd (math-idivmod hm 60)))
326 (list 'hms
327 (car hmd)
328 (cdr hmd)
329 (math-sub b (math-mul hm 60))))))))
330 (defun calcFunc-hms (h &optional m s)
331 (or (Math-realp h) (math-reject-arg h 'realp))
332 (or m (setq m 0))
333 (or (Math-realp m) (math-reject-arg m 'realp))
334 (or s (setq s 0))
335 (or (Math-realp s) (math-reject-arg s 'realp))
336 (if (and (not (Math-lessp m 0)) (Math-lessp m 60)
337 (not (Math-lessp s 0)) (Math-lessp s 60))
338 (math-add (math-to-hms h)
339 (list 'hms 0 m s))
340 (math-to-hms (math-add h
341 (math-add (math-div (or m 0) 60)
342 (math-div (or s 0) 3600)))
343 'deg)))
344
345 ;;; Convert A from HMS format to ANG or current angular mode.
346 (defun math-from-hms (a &optional ang) ; [R X] [Public]
347 (cond ((not (eq (car-safe a) 'hms))
348 (if (Math-numberp a)
349 a
350 (if (eq (car-safe a) 'sdev)
351 (math-make-sdev (math-from-hms (nth 1 a) ang)
352 (math-from-hms (nth 2 a) ang))
353 (if (eq (or ang calc-angle-mode) 'rad)
354 (list 'calcFunc-rad a)
355 (list 'calcFunc-deg a)))))
356 ((math-negp a)
357 (math-neg (math-from-hms (math-neg a) ang)))
358 ((eq (or ang calc-angle-mode) 'rad)
359 (math-mul (math-from-hms a 'deg) (math-pi-over-180)))
360 (t
361 (math-add (math-div (math-add (math-div (nth 3 a)
362 '(float 6 1))
363 (nth 2 a))
364 60)
365 (nth 1 a)))))
366
367 ;;;; Date forms.
368
369
370 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
371 ;;; These versions are rewritten to use arbitrary-size integers.
372 ;;; The Julian calendar is used up to 9/2/1752, after which the Gregorian
373 ;;; calendar is used; the first day after 9/2/1752 is 9/14/1752.
374
375 ;;; A numerical date is the number of days since midnight on
376 ;;; the morning of January 1, 1 A.D. If the date is a non-integer,
377 ;;; it represents a specific date and time.
378 ;;; A "dt" is a list of the form, (year month day), corresponding to
379 ;;; an integer code, or (year month day hour minute second), corresponding
380 ;;; to a non-integer code.
381
382 (defun math-date-to-dt (value)
383 (if (eq (car-safe value) 'date)
384 (setq value (nth 1 value)))
385 (or (math-realp value)
386 (math-reject-arg value 'datep))
387 (let* ((parts (math-date-parts value))
388 (date (car parts))
389 (time (nth 1 parts))
390 (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 value) 366 365)))
396 ; this result may be an overestimate
397 temp)
398 (while (Math-lessp date (setq temp (math-absolute-from-date 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 (and (eq year 1752) (>= date 247)
403 (setq date (+ date 11)))
404 (setq temp (if (math-leap-year-p year)
405 [1 32 61 92 122 153 183 214 245 275 306 336 999]
406 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
407 (while (>= date (aref temp month))
408 (setq month (1+ month)))
409 (setq day (1+ (- date (aref temp (1- month)))))
410 (if (math-integerp value)
411 (list year month day)
412 (list year month day
413 (/ time 3600)
414 (% (/ time 60) 60)
415 (math-add (% time 60) (nth 2 parts))))))
416
417 (defun math-dt-to-date (dt)
418 (or (integerp (nth 1 dt))
419 (math-reject-arg (nth 1 dt) 'fixnump))
420 (if (or (< (nth 1 dt) 1) (> (nth 1 dt) 12))
421 (math-reject-arg (nth 1 dt) "Month value is out of range"))
422 (or (integerp (nth 2 dt))
423 (math-reject-arg (nth 2 dt) 'fixnump))
424 (if (or (< (nth 2 dt) 1) (> (nth 2 dt) 31))
425 (math-reject-arg (nth 2 dt) "Day value is out of range"))
426 (let ((date (math-absolute-from-date (car dt) (nth 1 dt) (nth 2 dt))))
427 (if (nth 3 dt)
428 (math-add (math-float date)
429 (math-div (math-add (+ (* (nth 3 dt) 3600)
430 (* (nth 4 dt) 60))
431 (nth 5 dt))
432 '(float 864 2)))
433 date)))
434
435 (defun math-date-parts (value &optional offset)
436 (let* ((date (math-floor value))
437 (time (math-round (math-mul (math-sub value (or offset date)) 86400)
438 (and (> calc-internal-prec 12)
439 (- calc-internal-prec 12))))
440 (ftime (math-floor time)))
441 (list date
442 ftime
443 (math-sub time ftime))))
444
445
446 (defun math-this-year ()
447 (string-to-int (substring (current-time-string) -4)))
448
449 (defun math-leap-year-p (year)
450 (if (Math-lessp year 1752)
451 (if (math-negp year)
452 (= (math-imod (math-neg year) 4) 1)
453 (= (math-imod year 4) 0))
454 (setq year (math-imod year 400))
455 (or (and (= (% year 4) 0) (/= (% year 100) 0))
456 (= year 0))))
457
458 (defun math-days-in-month (year month)
459 (if (and (= month 2) (math-leap-year-p year))
460 29
461 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
462
463 (defun math-day-number (year month day)
464 (let ((day-of-year (+ day (* 31 (1- month)))))
465 (if (> month 2)
466 (progn
467 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
468 (if (math-leap-year-p year)
469 (setq day-of-year (1+ day-of-year)))))
470 (and (eq year 1752)
471 (or (> month 9)
472 (and (= month 9) (>= day 14)))
473 (setq day-of-year (- day-of-year 11)))
474 day-of-year))
475
476 (defun math-absolute-from-date (year month day)
477 (if (eq year 0) (setq year -1))
478 (let ((yearm1 (math-sub year 1)))
479 (math-sub (math-add (math-day-number year month day)
480 (math-add (math-mul 365 yearm1)
481 (if (math-posp year)
482 (math-quotient yearm1 4)
483 (math-sub 365
484 (math-quotient (math-sub 3 year)
485 4)))))
486 (if (or (Math-lessp year 1753)
487 (and (eq year 1752) (<= month 9)))
488 1
489 (let ((correction (math-mul (math-quotient yearm1 100) 3)))
490 (let ((res (math-idivmod correction 4)))
491 (math-add (if (= (cdr res) 0)
492 -1
493 0)
494 (car res))))))))
495
496
497 ;;; It is safe to redefine these in your .emacs file to use a different
498 ;;; language.
499
500 (defvar math-long-weekday-names '( "Sunday" "Monday" "Tuesday" "Wednesday"
501 "Thursday" "Friday" "Saturday" ))
502 (defvar math-short-weekday-names '( "Sun" "Mon" "Tue" "Wed"
503 "Thu" "Fri" "Sat" ))
504
505 (defvar math-long-month-names '( "January" "February" "March" "April"
506 "May" "June" "July" "August"
507 "September" "October" "November" "December" ))
508 (defvar math-short-month-names '( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
509 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
510
511
512 (defvar math-format-date-cache nil)
513
514 ;; The variables math-fd-date, math-fd-dt, math-fd-year,
515 ;; math-fd-month, math-fd-day, math-fd-weekday, math-fd-hour,
516 ;; math-fd-minute, math-fd-second, math-fd-bc-flag are local
517 ;; to math-format-date, but are used by math-format-date-part,
518 ;; which is called by math-format-date.
519 (defvar math-fd-date)
520 (defvar math-fd-dt)
521 (defvar math-fd-year)
522 (defvar math-fd-month)
523 (defvar math-fd-day)
524 (defvar math-fd-weekday)
525 (defvar math-fd-hour)
526 (defvar math-fd-minute)
527 (defvar math-fd-second)
528 (defvar math-fd-bc-flag)
529
530 (defun math-format-date (math-fd-date)
531 (if (eq (car-safe math-fd-date) 'date)
532 (setq math-fd-date (nth 1 math-fd-date)))
533 (let ((entry (list math-fd-date calc-internal-prec calc-date-format)))
534 (or (cdr (assoc entry math-format-date-cache))
535 (let* ((math-fd-dt nil)
536 (calc-group-digits nil)
537 (calc-leading-zeros nil)
538 (calc-number-radix 10)
539 math-fd-year math-fd-month math-fd-day math-fd-weekday
540 math-fd-hour math-fd-minute math-fd-second
541 (math-fd-bc-flag nil)
542 (fmt (apply 'concat (mapcar 'math-format-date-part
543 calc-date-format))))
544 (setq math-format-date-cache (cons (cons entry fmt)
545 math-format-date-cache))
546 (and (setq math-fd-dt (nthcdr 10 math-format-date-cache))
547 (setcdr math-fd-dt nil))
548 fmt))))
549
550 (defun math-format-date-part (x)
551 (cond ((stringp x)
552 x)
553 ((listp x)
554 (if (math-integerp math-fd-date)
555 ""
556 (apply 'concat (mapcar 'math-format-date-part x))))
557 ((eq x 'X)
558 "")
559 ((eq x 'N)
560 (math-format-number math-fd-date))
561 ((eq x 'n)
562 (math-format-number (math-floor math-fd-date)))
563 ((eq x 'J)
564 (math-format-number (math-add math-fd-date '(float (bigpos 235 214 17) -1))))
565 ((eq x 'j)
566 (math-format-number (math-add (math-floor math-fd-date) '(bigpos 424 721 1))))
567 ((eq x 'U)
568 (math-format-number (nth 1 (math-date-parts math-fd-date 719164))))
569 ((progn
570 (or math-fd-dt
571 (progn
572 (setq math-fd-dt (math-date-to-dt math-fd-date)
573 math-fd-year (car math-fd-dt)
574 math-fd-month (nth 1 math-fd-dt)
575 math-fd-day (nth 2 math-fd-dt)
576 math-fd-weekday (math-mod
577 (math-add (math-floor math-fd-date) 6) 7)
578 math-fd-hour (nth 3 math-fd-dt)
579 math-fd-minute (nth 4 math-fd-dt)
580 math-fd-second (nth 5 math-fd-dt))
581 (and (memq 'b calc-date-format)
582 (math-negp math-fd-year)
583 (setq math-fd-year (math-neg math-fd-year)
584 math-fd-bc-flag t))))
585 (memq x '(Y YY BY)))
586 (if (and (integerp math-fd-year) (> math-fd-year 1940) (< math-fd-year 2040))
587 (format (cond ((eq x 'YY) "%02d")
588 ((eq x 'BYY) "%2d")
589 (t "%d"))
590 (% math-fd-year 100))
591 (if (and (natnump math-fd-year) (< math-fd-year 100))
592 (format "+%d" math-fd-year)
593 (math-format-number math-fd-year))))
594 ((eq x 'YYY)
595 (math-format-number math-fd-year))
596 ((eq x 'YYYY)
597 (if (and (natnump math-fd-year) (< math-fd-year 100))
598 (format "+%d" math-fd-year)
599 (math-format-number math-fd-year)))
600 ((eq x 'b) "")
601 ((eq x 'aa)
602 (and (not math-fd-bc-flag) "ad"))
603 ((eq x 'AA)
604 (and (not math-fd-bc-flag) "AD"))
605 ((eq x 'aaa)
606 (and (not math-fd-bc-flag) "ad "))
607 ((eq x 'AAA)
608 (and (not math-fd-bc-flag) "AD "))
609 ((eq x 'aaaa)
610 (and (not math-fd-bc-flag) "a.d."))
611 ((eq x 'AAAA)
612 (and (not math-fd-bc-flag) "A.D."))
613 ((eq x 'bb)
614 (and math-fd-bc-flag "bc"))
615 ((eq x 'BB)
616 (and math-fd-bc-flag "BC"))
617 ((eq x 'bbb)
618 (and math-fd-bc-flag " bc"))
619 ((eq x 'BBB)
620 (and math-fd-bc-flag " BC"))
621 ((eq x 'bbbb)
622 (and math-fd-bc-flag "b.c."))
623 ((eq x 'BBBB)
624 (and math-fd-bc-flag "B.C."))
625 ((eq x 'M)
626 (format "%d" math-fd-month))
627 ((eq x 'MM)
628 (format "%02d" math-fd-month))
629 ((eq x 'BM)
630 (format "%2d" math-fd-month))
631 ((eq x 'mmm)
632 (downcase (nth (1- math-fd-month) math-short-month-names)))
633 ((eq x 'Mmm)
634 (nth (1- math-fd-month) math-short-month-names))
635 ((eq x 'MMM)
636 (upcase (nth (1- math-fd-month) math-short-month-names)))
637 ((eq x 'Mmmm)
638 (nth (1- math-fd-month) math-long-month-names))
639 ((eq x 'MMMM)
640 (upcase (nth (1- math-fd-month) math-long-month-names)))
641 ((eq x 'D)
642 (format "%d" math-fd-day))
643 ((eq x 'DD)
644 (format "%02d" math-fd-day))
645 ((eq x 'BD)
646 (format "%2d" math-fd-day))
647 ((eq x 'W)
648 (format "%d" math-fd-weekday))
649 ((eq x 'www)
650 (downcase (nth math-fd-weekday math-short-weekday-names)))
651 ((eq x 'Www)
652 (nth math-fd-weekday math-short-weekday-names))
653 ((eq x 'WWW)
654 (upcase (nth math-fd-weekday math-short-weekday-names)))
655 ((eq x 'Wwww)
656 (nth math-fd-weekday math-long-weekday-names))
657 ((eq x 'WWWW)
658 (upcase (nth math-fd-weekday math-long-weekday-names)))
659 ((eq x 'd)
660 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day)))
661 ((eq x 'ddd)
662 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day)))
663 ((eq x 'bdd)
664 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day)))
665 ((eq x 'h)
666 (and math-fd-hour (format "%d" math-fd-hour)))
667 ((eq x 'hh)
668 (and math-fd-hour (format "%02d" math-fd-hour)))
669 ((eq x 'bh)
670 (and math-fd-hour (format "%2d" math-fd-hour)))
671 ((eq x 'H)
672 (and math-fd-hour (format "%d" (1+ (% (+ math-fd-hour 11) 12)))))
673 ((eq x 'HH)
674 (and math-fd-hour (format "%02d" (1+ (% (+ math-fd-hour 11) 12)))))
675 ((eq x 'BH)
676 (and math-fd-hour (format "%2d" (1+ (% (+ math-fd-hour 11) 12)))))
677 ((eq x 'p)
678 (and math-fd-hour (if (< math-fd-hour 12) "a" "p")))
679 ((eq x 'P)
680 (and math-fd-hour (if (< math-fd-hour 12) "A" "P")))
681 ((eq x 'pp)
682 (and math-fd-hour (if (< math-fd-hour 12) "am" "pm")))
683 ((eq x 'PP)
684 (and math-fd-hour (if (< math-fd-hour 12) "AM" "PM")))
685 ((eq x 'pppp)
686 (and math-fd-hour (if (< math-fd-hour 12) "a.m." "p.m.")))
687 ((eq x 'PPPP)
688 (and math-fd-hour (if (< math-fd-hour 12) "A.M." "P.M.")))
689 ((eq x 'm)
690 (and math-fd-minute (format "%d" math-fd-minute)))
691 ((eq x 'mm)
692 (and math-fd-minute (format "%02d" math-fd-minute)))
693 ((eq x 'bm)
694 (and math-fd-minute (format "%2d" math-fd-minute)))
695 ((eq x 'C)
696 (and math-fd-second (not (math-zerop math-fd-second))
697 ":"))
698 ((memq x '(s ss bs SS BS))
699 (and math-fd-second
700 (not (and (memq x '(SS BS)) (math-zerop math-fd-second)))
701 (if (integerp math-fd-second)
702 (format (cond ((memq x '(ss SS)) "%02d")
703 ((memq x '(bs BS)) "%2d")
704 (t "%d"))
705 math-fd-second)
706 (concat (if (Math-lessp math-fd-second 10)
707 (cond ((memq x '(ss SS)) "0")
708 ((memq x '(bs BS)) " ")
709 (t ""))
710 "")
711 (let ((calc-float-format
712 (list 'fix (min (- 12 calc-internal-prec)
713 0))))
714 (math-format-number math-fd-second))))))))
715
716 ;; The variable math-pd-str is local to math-parse-date and
717 ;; math-parse-standard-date, but is used by math-parse-date-word,
718 ;; which is called by math-parse-date and math-parse-standard-date.
719 (defvar math-pd-str)
720
721 (defun math-parse-date (math-pd-str)
722 (catch 'syntax
723 (or (math-parse-standard-date math-pd-str t)
724 (math-parse-standard-date math-pd-str nil)
725 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str)
726 (list 'date (math-read-number (math-match-substring math-pd-str 1))))
727 (let ((case-fold-search t)
728 (year nil) (month nil) (day nil) (weekday nil)
729 (hour nil) (minute nil) (second nil) (bc-flag nil)
730 (a nil) (b nil) (c nil) (bigyear nil) temp)
731
732 ;; Extract the time, if any.
733 (if (or (string-match "\\([0-9][0-9]?\\):\\([0-9][0-9]?\\)\\(:\\([0-9][0-9]?\\(\\.[0-9]+\\)?\\)\\)? *\\([ap]m?\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)?" math-pd-str)
734 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]m?\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str))
735 (let ((ampm (math-match-substring math-pd-str 6)))
736 (setq hour (string-to-int (math-match-substring math-pd-str 1))
737 minute (math-match-substring math-pd-str 2)
738 second (math-match-substring math-pd-str 4)
739 math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
740 (substring math-pd-str (match-end 0))))
741 (if (equal minute "")
742 (setq minute 0)
743 (setq minute (string-to-int minute)))
744 (if (equal second "")
745 (setq second 0)
746 (setq second (math-read-number second)))
747 (if (equal ampm "")
748 (if (> hour 23)
749 (throw 'syntax "Hour value out of range"))
750 (setq ampm (upcase (aref ampm 0)))
751 (if (memq ampm '(?N ?M))
752 (if (and (= hour 12) (= minute 0) (eq second 0))
753 (if (eq ampm ?M) (setq hour 0))
754 (throw 'syntax
755 "Time must be 12:00:00 in this context"))
756 (if (or (= hour 0) (> hour 12))
757 (throw 'syntax "Hour value out of range"))
758 (if (eq (= ampm ?A) (= hour 12))
759 (setq hour (% (+ hour 12) 24)))))))
760
761 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
762 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str)
763 (progn
764 (setq math-pd-str (copy-sequence math-pd-str))
765 (aset math-pd-str (match-beginning 1) ?\/)))
766
767 ;; Extract obvious month or weekday names.
768 (if (string-match "[a-zA-Z]" math-pd-str)
769 (progn
770 (setq month (math-parse-date-word math-long-month-names))
771 (setq weekday (math-parse-date-word math-long-weekday-names))
772 (or month (setq month
773 (math-parse-date-word math-short-month-names)))
774 (or weekday (math-parse-date-word math-short-weekday-names))
775 (or hour
776 (if (setq temp (math-parse-date-word
777 '( "noon" "midnight" "mid" )))
778 (setq hour (if (= temp 1) 12 0) minute 0 second 0)))
779 (or (math-parse-date-word '( "ad" "a.d." ))
780 (if (math-parse-date-word '( "bc" "b.c." ))
781 (setq bc-flag t)))
782 (if (string-match "[a-zA-Z]+" math-pd-str)
783 (throw 'syntax (format "Bad word in date: \"%s\""
784 (math-match-substring math-pd-str 0))))))
785
786 ;; If there is a huge number other than the year, ignore it.
787 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str)
788 (setq temp (concat (substring math-pd-str 0 (match-beginning 0))
789 (substring math-pd-str (match-end 0))))
790 (string-match
791 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp))
792 (setq math-pd-str temp))
793
794 ;; If there is a number with a sign or a large number, it is a year.
795 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str)
796 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str))
797 (setq year (math-match-substring math-pd-str 1)
798 math-pd-str (concat (substring math-pd-str 0 (match-beginning 1))
799 (substring math-pd-str (match-end 1)))
800 year (math-read-number year)
801 bigyear t))
802
803 ;; Collect remaining numbers.
804 (setq temp 0)
805 (while (string-match "[0-9]+" math-pd-str temp)
806 (and c (throw 'syntax "Too many numbers in date"))
807 (setq c (string-to-int (math-match-substring math-pd-str 0)))
808 (or b (setq b c c nil))
809 (or a (setq a b b nil))
810 (setq temp (match-end 0)))
811
812 ;; Check that we have the right amount of information.
813 (setq temp (+ (if year 1 0) (if month 1 0) (if day 1 0)
814 (if a 1 0) (if b 1 0) (if c 1 0)))
815 (if (> temp 3)
816 (throw 'syntax "Too many numbers in date")
817 (if (or (< temp 2) (and year (= temp 2)))
818 (throw 'syntax "Not enough numbers in date")
819 (if (= temp 2) ; if year omitted, assume current year
820 (setq year (math-this-year)))))
821
822 ;; A large number must be a year.
823 (or year
824 (if (and a (or (> a 31) (< a 1)))
825 (setq year a a b b c c nil)
826 (if (and b (or (> b 31) (< b 1)))
827 (setq year b b c c nil)
828 (if (and c (or (> c 31) (< c 1)))
829 (setq year c c nil)))))
830
831 ;; A medium-large number must be a day.
832 (if year
833 (if (and a (> a 12))
834 (setq day a a b b c c nil)
835 (if (and b (> b 12))
836 (setq day b b c c nil)
837 (if (and c (> c 12))
838 (setq day c c nil)))))
839
840 ;; We may know enough to sort it out now.
841 (if (and year day)
842 (or month (setq month a))
843 (if (and year month)
844 (setq day a)
845
846 ;; Interpret order of numbers as same as for display format.
847 (setq temp calc-date-format)
848 (while temp
849 (cond ((not (symbolp (car temp))))
850 ((memq (car temp) '(Y YY BY YYY YYYY))
851 (or year (setq year a a b b c)))
852 ((memq (car temp) '(M MM BM mmm Mmm Mmmm MMM MMMM))
853 (or month (setq month a a b b c)))
854 ((memq (car temp) '(D DD BD))
855 (or day (setq day a a b b c))))
856 (setq temp (cdr temp)))
857
858 ;; If display format was not complete, assume American style.
859 (or month (setq month a a b b c))
860 (or day (setq day a a b b c))
861 (or year (setq year a a b b c))))
862
863 (if bc-flag
864 (setq year (math-neg (math-abs year))))
865
866 (math-parse-date-validate year bigyear month day
867 hour minute second)))))
868
869 (defun math-parse-date-validate (year bigyear month day hour minute second)
870 (and (not bigyear) (natnump year) (< year 100)
871 (setq year (+ year (if (< year 40) 2000 1900))))
872 (if (eq year 0)
873 (throw 'syntax "Year value is out of range"))
874 (if (or (< month 1) (> month 12))
875 (throw 'syntax "Month value is out of range"))
876 (if (or (< day 1) (> day (math-days-in-month year month)))
877 (throw 'syntax "Day value is out of range"))
878 (and hour
879 (progn
880 (if (or (< hour 0) (> hour 23))
881 (throw 'syntax "Hour value is out of range"))
882 (if (or (< minute 0) (> minute 59))
883 (throw 'syntax "Minute value is out of range"))
884 (if (or (math-negp second) (not (Math-lessp second 60)))
885 (throw 'syntax "Seconds value is out of range"))))
886 (list 'date (math-dt-to-date (append (list year month day)
887 (and hour (list hour minute second))))))
888
889 (defun math-parse-date-word (names &optional front)
890 (let ((n 1))
891 (while (and names (not (string-match (if (equal (car names) "Sep")
892 "Sept?"
893 (regexp-quote (car names)))
894 math-pd-str)))
895 (setq names (cdr names)
896 n (1+ n)))
897 (and names
898 (or (not front) (= (match-beginning 0) 0))
899 (progn
900 (setq math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
901 (if front "" " ")
902 (substring math-pd-str (match-end 0))))
903 n))))
904
905 (defun math-parse-standard-date (math-pd-str with-time)
906 (let ((case-fold-search t)
907 (okay t) num
908 (fmt calc-date-format) this next (gnext nil)
909 (year nil) (month nil) (day nil) (bigyear nil) (yearday nil)
910 (hour nil) (minute nil) (second nil) (bc-flag nil))
911 (while (and fmt okay)
912 (setq this (car fmt)
913 fmt (setq fmt (or (cdr fmt)
914 (prog1
915 gnext
916 (setq gnext nil))))
917 next (car fmt))
918 (if (consp next) (setq next (car next)))
919 (or (cond ((listp this)
920 (or (not with-time)
921 (not this)
922 (setq gnext fmt
923 fmt this)))
924 ((stringp this)
925 (if (and (<= (length this) (length math-pd-str))
926 (equal this
927 (substring math-pd-str 0 (length this))))
928 (setq math-pd-str (substring math-pd-str (length this)))))
929 ((eq this 'X)
930 t)
931 ((memq this '(n N j J))
932 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str)
933 (setq num (math-match-substring math-pd-str 0)
934 math-pd-str (substring math-pd-str (match-end 0))
935 num (math-date-to-dt (math-read-number num))
936 num (math-sub num
937 (if (memq this '(n N))
938 0
939 (if (or (eq this 'j)
940 (math-integerp num))
941 '(bigpos 424 721 1)
942 '(float (bigpos 235 214 17)
943 -1))))
944 hour (or (nth 3 num) hour)
945 minute (or (nth 4 num) minute)
946 second (or (nth 5 num) second)
947 year (car num)
948 month (nth 1 num)
949 day (nth 2 num))))
950 ((eq this 'U)
951 (and (string-match "\\`[-+]?[0-9]+" math-pd-str)
952 (setq num (math-match-substring math-pd-str 0)
953 math-pd-str (substring math-pd-str (match-end 0))
954 num (math-date-to-dt
955 (math-add 719164
956 (math-div (math-read-number num)
957 '(float 864 2))))
958 hour (nth 3 num)
959 minute (nth 4 num)
960 second (nth 5 num)
961 year (car num)
962 month (nth 1 num)
963 day (nth 2 num))))
964 ((memq this '(mmm Mmm MMM))
965 (setq month (math-parse-date-word math-short-month-names t)))
966 ((memq this '(Mmmm MMMM))
967 (setq month (math-parse-date-word math-long-month-names t)))
968 ((memq this '(www Www WWW))
969 (math-parse-date-word math-short-weekday-names t))
970 ((memq this '(Wwww WWWW))
971 (math-parse-date-word math-long-weekday-names t))
972 ((memq this '(p P))
973 (if (string-match "\\`a" math-pd-str)
974 (setq hour (if (= hour 12) 0 hour)
975 math-pd-str (substring math-pd-str 1))
976 (if (string-match "\\`p" math-pd-str)
977 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
978 math-pd-str (substring math-pd-str 1)))))
979 ((memq this '(pp PP pppp PPPP))
980 (if (string-match "\\`am\\|a\\.m\\." math-pd-str)
981 (setq hour (if (= hour 12) 0 hour)
982 math-pd-str (substring math-pd-str (match-end 0)))
983 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str)
984 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
985 math-pd-str (substring math-pd-str (match-end 0))))))
986 ((memq this '(Y YY BY YYY YYYY))
987 (and (if (memq next '(MM DD ddd hh HH mm ss SS))
988 (if (memq this '(Y YY BYY))
989 (string-match "\\` *[0-9][0-9]" math-pd-str)
990 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str))
991 (string-match "\\`[-+]?[0-9]+" math-pd-str))
992 (setq year (math-match-substring math-pd-str 0)
993 bigyear (or (eq this 'YYY)
994 (memq (aref math-pd-str 0) '(?\+ ?\-)))
995 math-pd-str (substring math-pd-str (match-end 0))
996 year (math-read-number year))))
997 ((eq this 'b)
998 t)
999 ((memq this '(aa AA aaaa AAAA))
1000 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str)
1001 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1002 ((memq this '(aaa AAA))
1003 (if (string-match "\\` *ad *" math-pd-str)
1004 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1005 ((memq this '(bb BB bbb BBB bbbb BBBB))
1006 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str)
1007 (setq math-pd-str (substring math-pd-str (match-end 0))
1008 bc-flag t)))
1009 ((memq this '(s ss bs SS BS))
1010 (and (if (memq next '(YY YYYY MM DD hh HH mm))
1011 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str)
1012 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str))
1013 (setq second (math-match-substring math-pd-str 0)
1014 math-pd-str (substring math-pd-str (match-end 0))
1015 second (math-read-number second))))
1016 ((eq this 'C)
1017 (if (string-match "\\`:[0-9][0-9]" math-pd-str)
1018 (setq math-pd-str (substring math-pd-str 1))
1019 t))
1020 ((or (not (if (and (memq this '(ddd MM DD hh HH mm))
1021 (memq next '(YY YYYY MM DD ddd
1022 hh HH mm ss SS)))
1023 (if (eq this 'ddd)
1024 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str)
1025 (string-match "\\` *[0-9][0-9]" math-pd-str))
1026 (string-match "\\` *[0-9]+" math-pd-str)))
1027 (and (setq num (string-to-int
1028 (math-match-substring math-pd-str 0))
1029 math-pd-str (substring math-pd-str (match-end 0)))
1030 nil))
1031 nil)
1032 ((eq this 'W)
1033 (and (>= num 0) (< num 7)))
1034 ((memq this '(d ddd bdd))
1035 (setq yearday num))
1036 ((memq this '(M MM BM))
1037 (setq month num))
1038 ((memq this '(D DD BD))
1039 (setq day num))
1040 ((memq this '(h hh bh H HH BH))
1041 (setq hour num))
1042 ((memq this '(m mm bm))
1043 (setq minute num)))
1044 (setq okay nil)))
1045 (if yearday
1046 (if (and month day)
1047 (setq yearday nil)
1048 (setq month 1 day 1)))
1049 (if (and okay (equal math-pd-str ""))
1050 (and month day (or (not (or hour minute second))
1051 (and hour minute))
1052 (progn
1053 (or year (setq year (math-this-year)))
1054 (or second (setq second 0))
1055 (if bc-flag
1056 (setq year (math-neg (math-abs year))))
1057 (setq day (math-parse-date-validate year bigyear month day
1058 hour minute second))
1059 (if yearday
1060 (setq day (math-add day (1- yearday))))
1061 day)))))
1062
1063
1064 (defun calcFunc-now (&optional zone)
1065 (let ((date (let ((calc-date-format nil))
1066 (math-parse-date (current-time-string)))))
1067 (if (consp date)
1068 (if zone
1069 (math-add date (math-div (math-sub (calcFunc-tzone nil date)
1070 (calcFunc-tzone zone date))
1071 '(float 864 2)))
1072 date)
1073 (calc-record-why "*Unable to interpret current date from system")
1074 (append (list 'calcFunc-now) (and zone (list zone))))))
1075
1076 (defun calcFunc-year (date)
1077 (car (math-date-to-dt date)))
1078
1079 (defun calcFunc-month (date)
1080 (nth 1 (math-date-to-dt date)))
1081
1082 (defun calcFunc-day (date)
1083 (nth 2 (math-date-to-dt date)))
1084
1085 (defun calcFunc-weekday (date)
1086 (if (eq (car-safe date) 'date)
1087 (setq date (nth 1 date)))
1088 (or (math-realp date)
1089 (math-reject-arg date 'datep))
1090 (math-mod (math-add (math-floor date) 6) 7))
1091
1092 (defun calcFunc-yearday (date)
1093 (let ((dt (math-date-to-dt date)))
1094 (math-day-number (car dt) (nth 1 dt) (nth 2 dt))))
1095
1096 (defun calcFunc-hour (date)
1097 (if (eq (car-safe date) 'hms)
1098 (nth 1 date)
1099 (or (nth 3 (math-date-to-dt date)) 0)))
1100
1101 (defun calcFunc-minute (date)
1102 (if (eq (car-safe date) 'hms)
1103 (nth 2 date)
1104 (or (nth 4 (math-date-to-dt date)) 0)))
1105
1106 (defun calcFunc-second (date)
1107 (if (eq (car-safe date) 'hms)
1108 (nth 3 date)
1109 (or (nth 5 (math-date-to-dt date)) 0)))
1110
1111 (defun calcFunc-time (date)
1112 (let ((dt (math-date-to-dt date)))
1113 (if (nth 3 dt)
1114 (cons 'hms (nthcdr 3 dt))
1115 (list 'hms 0 0 0))))
1116
1117 (defun calcFunc-date (date &optional month day hour minute second)
1118 (and (math-messy-integerp month) (setq month (math-trunc month)))
1119 (and month (not (integerp month)) (math-reject-arg month 'fixnump))
1120 (and (math-messy-integerp day) (setq day (math-trunc day)))
1121 (and day (not (integerp day)) (math-reject-arg day 'fixnump))
1122 (if (and (eq (car-safe hour) 'hms) (not minute))
1123 (setq second (nth 3 hour)
1124 minute (nth 2 hour)
1125 hour (nth 1 hour)))
1126 (and (math-messy-integerp hour) (setq hour (math-trunc hour)))
1127 (and hour (not (integerp hour)) (math-reject-arg hour 'fixnump))
1128 (and (math-messy-integerp minute) (setq minute (math-trunc minute)))
1129 (and minute (not (integerp minute)) (math-reject-arg minute 'fixnump))
1130 (and (math-messy-integerp second) (setq second (math-trunc second)))
1131 (and second (not (math-realp second)) (math-reject-arg second 'realp))
1132 (if month
1133 (progn
1134 (and (math-messy-integerp date) (setq date (math-trunc date)))
1135 (and date (not (math-integerp date)) (math-reject-arg date 'integerp))
1136 (if day
1137 (if hour
1138 (list 'date (math-dt-to-date (list date month day hour
1139 (or minute 0)
1140 (or second 0))))
1141 (list 'date (math-dt-to-date (list date month day))))
1142 (list 'date (math-dt-to-date (list (math-this-year) date month)))))
1143 (if (math-realp date)
1144 (list 'date date)
1145 (if (eq (car date) 'date)
1146 (nth 1 date)
1147 (math-reject-arg date 'datep)))))
1148
1149 (defun calcFunc-julian (date &optional zone)
1150 (if (math-realp date)
1151 (list 'date (if (math-integerp date)
1152 (math-sub date '(bigpos 424 721 1))
1153 (setq date (math-sub date '(float (bigpos 235 214 17) -1)))
1154 (math-sub date (math-div (calcFunc-tzone zone date)
1155 '(float 864 2)))))
1156 (if (eq (car date) 'date)
1157 (math-add (nth 1 date) (if (math-integerp (nth 1 date))
1158 '(bigpos 424 721 1)
1159 (math-add '(float (bigpos 235 214 17) -1)
1160 (math-div (calcFunc-tzone zone date)
1161 '(float 864 2)))))
1162 (math-reject-arg date 'datep))))
1163
1164 (defun calcFunc-unixtime (date &optional zone)
1165 (if (math-realp date)
1166 (progn
1167 (setq date (math-add 719164 (math-div date '(float 864 2))))
1168 (list 'date (math-sub date (math-div (calcFunc-tzone zone date)
1169 '(float 864 2)))))
1170 (if (eq (car date) 'date)
1171 (math-add (nth 1 (math-date-parts (nth 1 date) 719164))
1172 (calcFunc-tzone zone date))
1173 (math-reject-arg date 'datep))))
1174
1175
1176 ;;; Note: Longer names must appear before shorter names which are
1177 ;;; substrings of them.
1178 (defvar math-tzone-names
1179 '(( "UTC" 0 0)
1180 ( "MEGT" -1 "MET" "METDST" ) ; Middle Europe
1181 ( "METDST" -1 -1 ) ( "MET" -1 0 )
1182 ( "MEGZ" -1 "MEZ" "MESZ" ) ( "MEZ" -1 0 ) ( "MESZ" -1 -1 )
1183 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1184 ( "WETDST" 0 -1 ) ( "WET" 0 0 )
1185 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -1 ) ; Britain
1186 ( "NGT" (float 35 -1) "NST" "NDT" ) ; Newfoundland
1187 ( "NST" (float 35 -1) 0 ) ( "NDT" (float 35 -1) -1 )
1188 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -1 ) ; Atlantic
1189 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -1 ) ; Eastern
1190 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -1 ) ; Central
1191 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -1 ) ; Mountain
1192 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -1 ) ; Pacific
1193 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -1 ) ; Yukon
1194 )
1195 "No doc yet. See calc manual for now. ")
1196
1197 (defvar var-TimeZone)
1198
1199 (defun calcFunc-tzone (&optional zone date)
1200 (if zone
1201 (cond ((math-realp zone)
1202 (math-round (math-mul zone 3600)))
1203 ((eq (car zone) 'hms)
1204 (math-round (math-mul (math-from-hms zone 'deg) 3600)))
1205 ((eq (car zone) '+)
1206 (math-add (calcFunc-tzone (nth 1 zone) date)
1207 (calcFunc-tzone (nth 2 zone) date)))
1208 ((eq (car zone) '-)
1209 (math-sub (calcFunc-tzone (nth 1 zone) date)
1210 (calcFunc-tzone (nth 2 zone) date)))
1211 ((eq (car zone) 'var)
1212 (let ((name (upcase (symbol-name (nth 1 zone))))
1213 found)
1214 (if (setq found (assoc name math-tzone-names))
1215 (calcFunc-tzone (math-add (nth 1 found)
1216 (if (integerp (nth 2 found))
1217 (nth 2 found)
1218 (or
1219 (math-daylight-savings-adjust
1220 date (car found))
1221 0)))
1222 date)
1223 (if (equal name "LOCAL")
1224 (calcFunc-tzone nil date)
1225 (math-reject-arg zone "*Unrecognized time zone name")))))
1226 (t (math-reject-arg zone "*Expected a time zone")))
1227 (if (calc-var-value 'var-TimeZone)
1228 (calcFunc-tzone (calc-var-value 'var-TimeZone) date)
1229 (let ((p math-tzone-names)
1230 (offset 0)
1231 (tz '(var error var-error)))
1232 (save-excursion
1233 (set-buffer (get-buffer-create " *Calc Temporary*"))
1234 (erase-buffer)
1235 (call-process "date" nil t)
1236 (goto-char 1)
1237 (let ((case-fold-search t))
1238 (while (and p (not (search-forward (car (car p)) nil t)))
1239 (setq p (cdr p))))
1240 (if (looking-at "\\([-+][0-9]?[0-9]\\)\\([0-9][0-9]\\)?\\(\\'\\|[^0-9]\\)")
1241 (setq offset (math-add
1242 (string-to-int (buffer-substring
1243 (match-beginning 1)
1244 (match-end 1)))
1245 (if (match-beginning 2)
1246 (math-div (string-to-int (buffer-substring
1247 (match-beginning 2)
1248 (match-end 2)))
1249 60)
1250 0)))))
1251 (if p
1252 (progn
1253 (setq p (car p))
1254 ;; Try to convert to a generalized time zone.
1255 (if (integerp (nth 2 p))
1256 (let ((gen math-tzone-names))
1257 (while (and gen
1258 (not (equal (nth 2 (car gen)) (car p)))
1259 (not (equal (nth 3 (car gen)) (car p)))
1260 (not (equal (nth 4 (car gen)) (car p)))
1261 (not (equal (nth 5 (car gen)) (car p))))
1262 (setq gen (cdr gen)))
1263 (and gen
1264 (setq gen (car gen))
1265 (equal (math-daylight-savings-adjust nil (car gen))
1266 (nth 2 p))
1267 (setq p gen))))
1268 (setq tz (math-add (list 'var
1269 (intern (car p))
1270 (intern (concat "var-" (car p))))
1271 offset))))
1272 (kill-buffer " *Calc Temporary*")
1273 (setq var-TimeZone tz)
1274 (calc-refresh-evaltos 'var-TimeZone)
1275 (calcFunc-tzone tz date)))))
1276
1277 (defvar math-daylight-savings-hook 'math-std-daylight-savings)
1278
1279 (defun math-daylight-savings-adjust (date zone &optional dt)
1280 (or date (setq date (nth 1 (calcFunc-now))))
1281 (let (bump)
1282 (if (eq (car-safe date) 'date)
1283 (setq bump 0
1284 date (nth 1 date))
1285 (if (and date (math-realp date))
1286 (let ((zadj (assoc zone math-tzone-names)))
1287 (if zadj (setq bump -1
1288 date (math-sub date (math-div (nth 1 zadj)
1289 '(float 24 0))))))
1290 (math-reject-arg date 'datep)))
1291 (setq date (math-float date))
1292 (or dt (setq dt (math-date-to-dt date)))
1293 (and math-daylight-savings-hook
1294 (funcall math-daylight-savings-hook date dt zone bump))))
1295
1296 (defun calcFunc-dsadj (date &optional zone)
1297 (if zone
1298 (or (eq (car-safe zone) 'var)
1299 (math-reject-arg zone "*Time zone variable expected"))
1300 (setq zone (or (calc-var-value 'var-TimeZone)
1301 (progn
1302 (calcFunc-tzone)
1303 (calc-var-value 'var-TimeZone)))))
1304 (setq zone (and (eq (car-safe zone) 'var)
1305 (upcase (symbol-name (nth 1 zone)))))
1306 (let ((zadj (assoc zone math-tzone-names)))
1307 (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1308 (if (integerp (nth 2 zadj))
1309 (nth 2 zadj)
1310 (math-daylight-savings-adjust date zone))))
1311
1312 (defun calcFunc-tzconv (date z1 z2)
1313 (if (math-realp date)
1314 (nth 1 (calcFunc-tzconv (list 'date date) z1 z2))
1315 (calcFunc-unixtime (calcFunc-unixtime date z1) z2)))
1316
1317 (defun math-std-daylight-savings (date dt zone bump)
1318 "Standard North American daylight savings algorithm.
1319 This implements the rules for the U.S. and Canada as of 1987.
1320 Daylight savings begins on the first Sunday of April at 2 a.m.,
1321 and ends on the last Sunday of October at 2 a.m."
1322 (cond ((< (nth 1 dt) 4) 0)
1323 ((= (nth 1 dt) 4)
1324 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1325 (cond ((< (nth 2 dt) sunday) 0)
1326 ((= (nth 2 dt) sunday)
1327 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1328 (t -1))))
1329 ((< (nth 1 dt) 10) -1)
1330 ((= (nth 1 dt) 10)
1331 (let ((sunday (math-prev-weekday-in-month date dt 31 0)))
1332 (cond ((< (nth 2 dt) sunday) -1)
1333 ((= (nth 2 dt) sunday)
1334 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1335 (t 0))))
1336 (t 0)))
1337
1338 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1339 ;;; day of the given month.
1340 (defun math-prev-weekday-in-month (date dt day wday)
1341 (or day (setq day (nth 2 dt)))
1342 (if (> day (math-days-in-month (car dt) (nth 1 dt)))
1343 (setq day (math-days-in-month (car dt) (nth 1 dt))))
1344 (let ((zeroth (math-sub (math-floor date) (nth 2 dt))))
1345 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day))) zeroth)))
1346
1347 (defun calcFunc-pwday (date &optional day weekday)
1348 (if (eq (car-safe date) 'date)
1349 (setq date (nth 1 date)))
1350 (or (math-realp date)
1351 (math-reject-arg date 'datep))
1352 (if (math-messy-integerp day) (setq day (math-trunc day)))
1353 (or (integerp day) (math-reject-arg day 'fixnump))
1354 (if (= day 0) (setq day 31))
1355 (and (or (< day 7) (> day 31)) (math-reject-arg day 'range))
1356 (math-prev-weekday-in-month date (math-date-to-dt date) day (or weekday 0)))
1357
1358
1359 (defun calcFunc-newweek (date &optional weekday)
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 (or weekday (setq weekday 0))
1365 (and (math-messy-integerp weekday) (setq weekday (math-trunc weekday)))
1366 (or (integerp weekday) (math-reject-arg weekday 'fixnump))
1367 (and (or (< weekday 0) (> weekday 6)) (math-reject-arg weekday 'range))
1368 (setq date (math-floor date))
1369 (list 'date (math-sub date (calcFunc-weekday (math-sub date weekday)))))
1370
1371 (defun calcFunc-newmonth (date &optional day)
1372 (or day (setq day 1))
1373 (and (math-messy-integerp day) (setq day (math-trunc day)))
1374 (or (integerp day) (math-reject-arg day 'fixnump))
1375 (and (or (< day 0) (> day 31)) (math-reject-arg day 'range))
1376 (let ((dt (math-date-to-dt date)))
1377 (if (or (= day 0) (> day (math-days-in-month (car dt) (nth 1 dt))))
1378 (setq day (math-days-in-month (car dt) (nth 1 dt))))
1379 (and (eq (car dt) 1752) (= (nth 1 dt) 9)
1380 (if (>= day 14) (setq day (- day 11))))
1381 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1))
1382 (1- day)))))
1383
1384 (defun calcFunc-newyear (date &optional day)
1385 (or day (setq day 1))
1386 (and (math-messy-integerp day) (setq day (math-trunc day)))
1387 (or (integerp day) (math-reject-arg day 'fixnump))
1388 (let ((dt (math-date-to-dt date)))
1389 (if (and (>= day 0) (<= day 366))
1390 (let ((max (if (eq (car dt) 1752) 355
1391 (if (math-leap-year-p (car dt)) 366 365))))
1392 (if (or (= day 0) (> day max)) (setq day max))
1393 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1394 (1- day))))
1395 (if (and (>= day -12) (<= day -1))
1396 (list 'date (math-dt-to-date (list (car dt) (- day) 1)))
1397 (math-reject-arg day 'range)))))
1398
1399 (defun calcFunc-incmonth (date &optional step)
1400 (or step (setq step 1))
1401 (and (math-messy-integerp step) (setq step (math-trunc step)))
1402 (or (math-integerp step) (math-reject-arg step 'integerp))
1403 (let* ((dt (math-date-to-dt date))
1404 (year (car dt))
1405 (month (math-add (1- (nth 1 dt)) step))
1406 (extra (calcFunc-idiv month 12))
1407 (day (nth 2 dt)))
1408 (setq month (1+ (math-sub month (math-mul extra 12)))
1409 year (math-add year extra)
1410 day (min day (math-days-in-month year month)))
1411 (and (math-posp (car dt)) (not (math-posp year))
1412 (setq year (math-sub year 1))) ; did we go past the year zero?
1413 (and (math-negp (car dt)) (not (math-negp year))
1414 (setq year (math-add year 1)))
1415 (list 'date (math-dt-to-date
1416 (cons year (cons month (cons day (cdr (cdr (cdr dt))))))))))
1417
1418 (defun calcFunc-incyear (date &optional step)
1419 (calcFunc-incmonth date (math-mul (or step 1) 12)))
1420
1421
1422
1423 (defun calcFunc-bsub (a b)
1424 (or (eq (car-safe a) 'date)
1425 (math-reject-arg a 'datep))
1426 (if (eq (car-safe b) 'date)
1427 (if (math-lessp (nth 1 a) (nth 1 b))
1428 (math-neg (calcFunc-bsub b a))
1429 (math-setup-holidays b)
1430 (let* ((da (math-to-business-day a))
1431 (db (math-to-business-day b)))
1432 (math-add (math-sub (car da) (car db))
1433 (if (and (cdr db) (not (cdr da))) 1 0))))
1434 (calcFunc-badd a (math-neg b))))
1435
1436 (defvar math-holidays-cache nil)
1437 (defvar math-holidays-cache-tag t)
1438 (defun calcFunc-badd (a b)
1439 (if (eq (car-safe b) 'date)
1440 (if (eq (car-safe a) 'date)
1441 (math-reject-arg nil "*Illegal combination in date arithmetic")
1442 (calcFunc-badd b a))
1443 (if (eq (car-safe a) 'date)
1444 (if (Math-realp b)
1445 (if (Math-zerop b)
1446 a
1447 (let* ((d (math-to-business-day a))
1448 (bb (math-add (car d)
1449 (if (and (cdr d) (Math-posp b))
1450 (math-sub b 1) b))))
1451 (or (math-from-business-day bb)
1452 (calcFunc-badd a b))))
1453 (if (eq (car-safe b) 'hms)
1454 (let ((hours (nth 7 math-holidays-cache)))
1455 (setq b (math-div (math-from-hms b 'deg) 24))
1456 (if hours
1457 (setq b (math-div b (cdr hours))))
1458 (calcFunc-badd a b))
1459 (math-reject-arg nil "*Illegal combination in date arithmetic")))
1460 (math-reject-arg a 'datep))))
1461
1462 (defun calcFunc-holiday (a)
1463 (if (cdr (math-to-business-day a)) 1 0))
1464
1465 ;;; Compute the number of business days since Jan 1, 1 AD.
1466
1467 (defun math-to-business-day (date &optional need-year)
1468 (if (eq (car-safe date) 'date)
1469 (setq date (nth 1 date)))
1470 (or (Math-realp date)
1471 (math-reject-arg date 'datep))
1472 (let* ((day (math-floor date))
1473 (time (math-sub date day))
1474 (dt (math-date-to-dt day))
1475 (delta 0)
1476 (holiday nil))
1477 (or (not need-year) (eq (car dt) need-year)
1478 (math-reject-arg (list 'date day) "*Generated holiday has wrong year"))
1479 (math-setup-holidays date)
1480 (let ((days (car math-holidays-cache)))
1481 (while (and (setq days (cdr days)) (< (car days) day))
1482 (setq delta (1+ delta)))
1483 (and days (= day (car days))
1484 (setq holiday t)))
1485 (let* ((weekdays (nth 3 math-holidays-cache))
1486 (weeks (1- (/ (+ day 6) 7)))
1487 (wkday (- day 1 (* weeks 7))))
1488 (setq delta (+ delta (* weeks (length weekdays))))
1489 (while (and weekdays (< (car weekdays) wkday))
1490 (setq weekdays (cdr weekdays)
1491 delta (1+ delta)))
1492 (and weekdays (eq wkday (car weekdays))
1493 (setq holiday t)))
1494 (let ((hours (nth 7 math-holidays-cache)))
1495 (if hours
1496 (progn
1497 (setq time (math-div (math-sub time (car hours)) (cdr hours)))
1498 (if (Math-lessp time 0) (setq time 0))
1499 (or (Math-lessp time 1)
1500 (setq time
1501 (math-sub 1
1502 (math-div 1 (math-mul 86400 (cdr hours)))))))))
1503 (cons (math-add (math-sub day delta) time) holiday)))
1504
1505
1506 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1507 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1508
1509 (defun math-from-business-day (num)
1510 (let* ((day (math-floor num))
1511 (time (math-sub num day)))
1512 (or (integerp day)
1513 (math-reject-arg nil "*Date is outside valid range"))
1514 (math-setup-holidays)
1515 (let ((days (nth 1 math-holidays-cache))
1516 (delta 0))
1517 (while (and (setq days (cdr days)) (< (car days) day))
1518 (setq delta (1+ delta)))
1519 (setq day (+ day delta)))
1520 (let* ((weekdays (nth 3 math-holidays-cache))
1521 (bweek (- 7 (length weekdays)))
1522 (weeks (1- (/ (+ day (1- bweek)) bweek)))
1523 (wkday (- day 1 (* weeks bweek)))
1524 (w 0))
1525 (setq day (+ day (* weeks (length weekdays))))
1526 (while (if (memq w weekdays)
1527 (setq day (1+ day))
1528 (> (setq wkday (1- wkday)) 0))
1529 (setq w (1+ w)))
1530 (let ((hours (nth 7 math-holidays-cache)))
1531 (if hours
1532 (setq time (math-add (math-mul time (cdr hours)) (car hours)))))
1533 (and (not (math-setup-holidays day))
1534 (list 'date (math-add day time))))))
1535
1536 ;; The variable math-sh-year is local to math-setup-holidays
1537 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1538 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1539 (defvar math-sh-year)
1540
1541 (defun math-setup-holidays (&optional date)
1542 (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag)
1543 (let ((h (calc-var-value 'var-Holidays))
1544 (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3)
1545 (thu . 4) (fri . 5) (sat . 6) ))
1546 (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil))
1547 (or (math-vectorp h)
1548 (math-reject-arg h "*Holidays variable must be a vector"))
1549 (while (setq h (cdr h))
1550 (cond ((or (and (eq (car-safe (car h)) 'date)
1551 (integerp (nth 1 (car h))))
1552 (and (eq (car-safe (car h)) 'intv)
1553 (eq (car-safe (nth 2 (car h))) 'date))
1554 (eq (car-safe (car h)) 'vec))
1555 (setq days (cons (car h) days)))
1556 ((and (eq (car-safe (car h)) 'var)
1557 (assq (nth 1 (car h)) wdnames))
1558 (setq weekdays (cons (cdr (assq (nth 1 (car h)) wdnames))
1559 weekdays)))
1560 ((and (eq (car-safe (car h)) 'intv)
1561 (eq (car-safe (nth 2 (car h))) 'hms)
1562 (eq (car-safe (nth 3 (car h))) 'hms))
1563 (if hours
1564 (math-reject-arg
1565 (car h) "*Only one hours interval allowed in Holidays"))
1566 (setq hours (math-div (car h) '(hms 24 0 0)))
1567 (if (or (Math-lessp (nth 2 hours) 0)
1568 (Math-lessp 1 (nth 3 hours)))
1569 (math-reject-arg
1570 (car h) "*Hours interval out of range"))
1571 (setq hours (cons (nth 2 hours)
1572 (math-sub (nth 3 hours) (nth 2 hours))))
1573 (if (Math-zerop (cdr hours))
1574 (math-reject-arg
1575 (car h) "*Degenerate hours interval")))
1576 ((or (and (eq (car-safe (car h)) 'intv)
1577 (Math-integerp (nth 2 (car h)))
1578 (Math-integerp (nth 3 (car h))))
1579 (and (integerp (car h))
1580 (> (car h) 1900) (< (car h) 2100)))
1581 (if limit
1582 (math-reject-arg
1583 (car h) "*Only one limit allowed in Holidays"))
1584 (setq limit (calcFunc-vint (car h) '(intv 3 1 2737)))
1585 (if (equal limit '(vec))
1586 (math-reject-arg (car h) "*Limit is out of range")))
1587 ((or (math-expr-contains (car h) '(var y var-y))
1588 (math-expr-contains (car h) '(var m var-m)))
1589 (setq exprs (cons (car h) exprs)))
1590 (t (math-reject-arg
1591 (car h) "*Holidays must contain a vector of holidays"))))
1592 (if (= (length weekdays) 7)
1593 (math-reject-arg nil "*Too many weekend days"))
1594 (setq math-holidays-cache (list (list -1) ; 0: days list
1595 (list -1) ; 1: inverse-days list
1596 nil ; 2: exprs
1597 (sort weekdays '<)
1598 (or limit '(intv 3 1 2737))
1599 nil ; 5: (lo.hi) expanded years
1600 (cons exprs days)
1601 hours) ; 7: business hours
1602 math-holidays-cache-tag (calc-var-value 'var-Holidays))))
1603 (if date
1604 (let ((year (calcFunc-year date))
1605 (limits (nth 5 math-holidays-cache))
1606 (done nil))
1607 (or (eq (calcFunc-in year (nth 4 math-holidays-cache)) 1)
1608 (progn
1609 (or (eq (car-safe date) 'date) (setq date (list 'date date)))
1610 (math-reject-arg date "*Date is outside valid range")))
1611 (unwind-protect
1612 (let ((days (nth 6 math-holidays-cache)))
1613 (if days
1614 (let ((math-sh-year nil)) ; see below
1615 (setcar (nthcdr 6 math-holidays-cache) nil)
1616 (math-setup-add-holidays (cons 'vec (cdr days)))
1617 (setcar (nthcdr 2 math-holidays-cache) (car days))))
1618 (cond ((not (nth 2 math-holidays-cache))
1619 (setq done t)
1620 nil)
1621 ((not limits)
1622 (setcar (nthcdr 5 math-holidays-cache) (cons year year))
1623 (math-setup-year-holidays year)
1624 (setq done t))
1625 ((< year (car limits))
1626 (message "Computing holidays, %d .. %d"
1627 year (1- (car limits)))
1628 (calc-set-command-flag 'clear-message)
1629 (while (< year (car limits))
1630 (setcar limits (1- (car limits)))
1631 (math-setup-year-holidays (car limits)))
1632 (setq done t))
1633 ((> year (cdr limits))
1634 (message "Computing holidays, %d .. %d"
1635 (1+ (cdr limits)) year)
1636 (calc-set-command-flag 'clear-message)
1637 (while (> year (cdr limits))
1638 (setcdr limits (1+ (cdr limits)))
1639 (math-setup-year-holidays (cdr limits)))
1640 (setq done t))
1641 (t
1642 (setq done t)
1643 nil)))
1644 (or done (setq math-holidays-cache-tag t))))))
1645
1646 (defun math-setup-year-holidays (math-sh-year)
1647 (let ((exprs (nth 2 math-holidays-cache)))
1648 (while exprs
1649 (let* ((var-y math-sh-year)
1650 (var-m nil)
1651 (expr (math-evaluate-expr (car exprs))))
1652 (if (math-expr-contains expr '(var m var-m))
1653 (let ((var-m 0))
1654 (while (<= (setq var-m (1+ var-m)) 12)
1655 (math-setup-add-holidays (math-evaluate-expr expr))))
1656 (math-setup-add-holidays expr)))
1657 (setq exprs (cdr exprs)))))
1658
1659 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
1660 (cond ((eq (car-safe days) 'vec)
1661 (while (setq days (cdr days))
1662 (math-setup-add-holidays (car days))))
1663 ((eq (car-safe days) 'intv)
1664 (let ((day (math-ceiling (nth 2 days))))
1665 (or (eq (calcFunc-in day days) 1)
1666 (setq day (math-add day 1)))
1667 (while (eq (calcFunc-in day days) 1)
1668 (math-setup-add-holidays day)
1669 (setq day (math-add day 1)))))
1670 ((eq (car-safe days) 'date)
1671 (math-setup-add-holidays (nth 1 days)))
1672 ((eq days 0))
1673 ((integerp days)
1674 (let ((b (math-to-business-day days math-sh-year)))
1675 (or (cdr b) ; don't register holidays twice!
1676 (let ((prev (car math-holidays-cache))
1677 (iprev (nth 1 math-holidays-cache)))
1678 (while (and (cdr prev) (< (nth 1 prev) days))
1679 (setq prev (cdr prev) iprev (cdr iprev)))
1680 (setcdr prev (cons days (cdr prev)))
1681 (setcdr iprev (cons (car b) (cdr iprev)))
1682 (while (setq iprev (cdr iprev))
1683 (setcar iprev (1- (car iprev))))))))
1684 ((Math-realp days)
1685 (math-reject-arg (list 'date days) "*Invalid holiday value"))
1686 (t
1687 (math-reject-arg days "*Holiday formula failed to evaluate"))))
1688
1689
1690
1691
1692 ;;;; Error forms.
1693
1694 ;;; Build a standard deviation form. [X X X]
1695 (defun math-make-sdev (x sigma)
1696 (if (memq (car-safe x) '(date mod sdev intv vec))
1697 (math-reject-arg x 'realp))
1698 (if (memq (car-safe sigma) '(date mod sdev intv vec))
1699 (math-reject-arg sigma 'realp))
1700 (if (or (Math-negp sigma) (memq (car-safe sigma) '(cplx polar)))
1701 (setq sigma (math-abs sigma)))
1702 (if (and (Math-zerop sigma) (Math-scalarp x))
1703 x
1704 (list 'sdev x sigma)))
1705 (defun calcFunc-sdev (x sigma)
1706 (math-make-sdev x sigma))
1707
1708
1709
1710 ;;;; Modulo forms.
1711
1712 (defun math-normalize-mod (a)
1713 (let ((n (math-normalize (nth 1 a)))
1714 (m (math-normalize (nth 2 a))))
1715 (if (and (math-anglep n) (math-anglep m) (math-posp m))
1716 (math-make-mod n m)
1717 (math-normalize (list 'calcFunc-makemod n m)))))
1718
1719 ;;; Build a modulo form. [N R R]
1720 (defun math-make-mod (n m)
1721 (setq calc-previous-modulo m)
1722 (and n
1723 (cond ((not (Math-anglep m))
1724 (math-reject-arg m 'anglep))
1725 ((not (math-posp m))
1726 (math-reject-arg m 'posp))
1727 ((Math-anglep n)
1728 (if (or (Math-negp n)
1729 (not (Math-lessp n m)))
1730 (list 'mod (math-mod n m) m)
1731 (list 'mod n m)))
1732 ((memq (car n) '(+ - / vec neg))
1733 (math-normalize
1734 (cons (car n)
1735 (mapcar (function (lambda (x) (math-make-mod x m)))
1736 (cdr n)))))
1737 ((and (eq (car n) '*) (Math-anglep (nth 1 n)))
1738 (math-mul (math-make-mod (nth 1 n) m) (nth 2 n)))
1739 ((memq (car n) '(* ^ var calcFunc-subscr))
1740 (math-mul (math-make-mod 1 m) n))
1741 (t (math-reject-arg n 'anglep)))))
1742 (defun calcFunc-makemod (n m)
1743 (math-make-mod n m))
1744
1745
1746
1747 ;;;; Interval forms.
1748
1749 ;;; Build an interval form. [X S X X]
1750 (defun math-make-intv (mask lo hi)
1751 (if (memq (car-safe lo) '(cplx polar mod sdev intv vec))
1752 (math-reject-arg lo 'realp))
1753 (if (memq (car-safe hi) '(cplx polar mod sdev intv vec))
1754 (math-reject-arg hi 'realp))
1755 (or (eq (eq (car-safe lo) 'date) (eq (car-safe hi) 'date))
1756 (math-reject-arg (if (eq (car-safe lo) 'date) hi lo) 'datep))
1757 (if (and (or (Math-realp lo) (eq (car lo) 'date))
1758 (or (Math-realp hi) (eq (car hi) 'date)))
1759 (let ((cmp (math-compare lo hi)))
1760 (if (= cmp 0)
1761 (if (= mask 3)
1762 lo
1763 (list 'intv mask lo hi))
1764 (if (> cmp 0)
1765 (if (= mask 3)
1766 (list 'intv 2 lo lo)
1767 (list 'intv mask lo lo))
1768 (list 'intv mask lo hi))))
1769 (list 'intv mask lo hi)))
1770 (defun calcFunc-intv (mask lo hi)
1771 (if (math-messy-integerp mask) (setq mask (math-trunc mask)))
1772 (or (natnump mask) (math-reject-arg mask 'fixnatnump))
1773 (or (<= mask 3) (math-reject-arg mask 'range))
1774 (math-make-intv mask lo hi))
1775
1776 (defun math-sort-intv (mask lo hi)
1777 (if (Math-lessp hi lo)
1778 (math-make-intv (aref [0 2 1 3] mask) hi lo)
1779 (math-make-intv mask lo hi)))
1780
1781
1782
1783
1784 (defun math-combine-intervals (a am b bm c cm d dm)
1785 (let (res)
1786 (if (= (setq res (math-compare a c)) 1)
1787 (setq a c am cm)
1788 (if (= res 0)
1789 (setq am (or am cm))))
1790 (if (= (setq res (math-compare b d)) -1)
1791 (setq b d bm dm)
1792 (if (= res 0)
1793 (setq bm (or bm dm))))
1794 (math-make-intv (+ (if am 2 0) (if bm 1 0)) a b)))
1795
1796
1797 (defun math-div-mod (a b m) ; [R R R R] (Returns nil if no solution)
1798 (and (Math-integerp a) (Math-integerp b) (Math-integerp m)
1799 (let ((u1 1) (u3 b) (v1 0) (v3 m))
1800 (while (not (eq v3 0)) ; See Knuth sec 4.5.2, exercise 15
1801 (let* ((q (math-idivmod u3 v3))
1802 (t1 (math-sub u1 (math-mul v1 (car q)))))
1803 (setq u1 v1 u3 v3 v1 t1 v3 (cdr q))))
1804 (let ((q (math-idivmod a u3)))
1805 (and (eq (cdr q) 0)
1806 (math-mod (math-mul (car q) u1) m))))))
1807
1808 (defun math-mod-intv (a b)
1809 (let* ((q1 (math-floor (math-div (nth 2 a) b)))
1810 (q2 (math-floor (math-div (nth 3 a) b)))
1811 (m1 (math-sub (nth 2 a) (math-mul q1 b)))
1812 (m2 (math-sub (nth 3 a) (math-mul q2 b))))
1813 (cond ((equal q1 q2)
1814 (math-sort-intv (nth 1 a) m1 m2))
1815 ((and (math-equal-int (math-sub q2 q1) 1)
1816 (math-zerop m2)
1817 (memq (nth 1 a) '(0 2)))
1818 (math-make-intv (nth 1 a) m1 b))
1819 (t
1820 (math-make-intv 2 0 b)))))
1821
1822 ;; The variables math-exp-str and math-exp-pos are local to
1823 ;; math-read-exprs in math-aent.el, but are used by
1824 ;; math-read-angle-brackets, which is called (indirectly) by
1825 ;; math-read-exprs.
1826 (defvar math-exp-str)
1827 (defvar math-exp-pos)
1828
1829 (defun math-read-angle-brackets ()
1830 (let* ((last (or (math-check-for-commas t) (length math-exp-str)))
1831 (str (substring math-exp-str math-exp-pos last))
1832 (res
1833 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str)
1834 (let ((str1 (substring str 0 (1- (match-end 0))))
1835 (str2 (substring str (match-end 0)))
1836 (calc-hashes-used 0))
1837 (setq str1 (math-read-expr (concat "[" str1 "]")))
1838 (if (eq (car-safe str1) 'error)
1839 str1
1840 (setq str2 (math-read-expr str2))
1841 (if (eq (car-safe str2) 'error)
1842 str2
1843 (append '(calcFunc-lambda) (cdr str1) (list str2)))))
1844 (if (string-match "#" str)
1845 (let ((calc-hashes-used 0))
1846 (and (setq str (math-read-expr str))
1847 (if (eq (car-safe str) 'error)
1848 str
1849 (append '(calcFunc-lambda)
1850 (calc-invent-args calc-hashes-used)
1851 (list str)))))
1852 (math-parse-date str)))))
1853 (if (stringp res)
1854 (throw 'syntax res))
1855 (if (eq (car-safe res) 'error)
1856 (throw 'syntax (nth 2 res)))
1857 (setq math-exp-pos (1+ last))
1858 (math-read-token)
1859 res))
1860
1861 ;;; arch-tag: a3d8f33b-9508-4043-8060-d02b8c9c750c
1862 ;;; calc-forms.el ends here