1 ;;; calc-forms.el --- data format conversion functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 ;; Declare functions which are defined elsewhere.
33 (declare-function calendar-current-time-zone "cal-dst" ())
34 (declare-function calendar-absolute-from-gregorian "calendar" (date))
35 (declare-function dst-in-effect "cal-dst" (date))
41 (let ((time (current-time-string)))
42 (calc-enter-result 0 "time"
45 (string-to-number (substring time 11 13))
46 (string-to-number (substring time 14 16))
47 (string-to-number (substring time 17 19)))
48 (list 'hms 24 0 0))))))
50 (defun calc-to-hms (arg)
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))))
59 (defun calc-from-hms (arg)
65 (defun calc-hms-notation (fmt)
66 (interactive "sHours-minutes-seconds format (hms, @ ' \", etc.): ")
68 (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt)
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))
77 (setq-default calc-hms-format calc-hms-format)) ; for minibuffer
78 (error "Bad hours-minutes-seconds format"))))
80 (defun calc-date-notation (fmt arg)
81 (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP")
83 (if (string-match-p "\\`\\s-*\\'" fmt)
85 (if (string-match "\\` *[0-9] *\\'" fmt)
86 (setq fmt (nth (string-to-number fmt) calc-standard-date-formats)))
87 (or (string-match "[a-zA-Z]" fmt)
88 (error "Bad date format specifier"))
90 (>= (setq arg (prefix-numeric-value arg)) 0)
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))
103 (substring fmt (match-beginning 0) (match-end 0))
105 (substring fmt (match-end 0))))))
110 (let ((case-fold-search nil))
111 (and (setq temp (string-match ":[BS]S" fmt))
113 (while (setq pos (string-match "[<>a-zA-Z]" fmt))
115 (setq lfmt (cons (substring fmt 0 pos) lfmt)))
117 (cond ((= (aref fmt pos) ?\<)
118 (and time (error "Nested <'s not allowed"))
119 (and lfmt (setq fullfmt (nconc lfmt fullfmt)
122 ((= (aref fmt pos) ?\>)
123 (or time (error "Misplaced > in format"))
124 (and lfmt (setq fullfmt (cons (nreverse lfmt) fullfmt)
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
140 W www Www Wwww WWW WWWW
143 m mm bm s ss bss SS BS C
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)))
152 (setq lfmt (cons fmt lfmt)))
154 (setq fullfmt (cons (nreverse lfmt) fullfmt))
155 (setq fullfmt (nconc lfmt fullfmt))))
156 (calc-change-mode 'calc-date-format (nreverse fullfmt) t))))
159 (defun calc-hms-mode ()
162 (calc-change-mode 'calc-angle-mode 'hms)
163 (message "Angles measured in degrees-minutes-seconds")))
166 (defun calc-now (arg)
168 (calc-date-zero-args "now" 'calcFunc-now arg))
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"))
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
185 (defun calc-date (arg)
187 (if (or (< arg 1) (> arg 6))
188 (error "Between one and six arguments are allowed"))
190 (calc-enter-result arg "date" (cons 'calcFunc-date (calc-top-list-n arg)))))
192 (defun calc-julian (arg)
194 (calc-date-one-arg "juln" 'calcFunc-julian arg))
196 (defun calc-unix-time (arg)
198 (calc-date-one-arg "unix" 'calcFunc-unixtime arg))
200 (defun calc-time-zone (arg)
202 (calc-date-zero-args "zone" 'calcFunc-tzone arg))
204 (defun calc-convert-time-zones (old &optional new)
205 (interactive "sFrom time zone: ")
208 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv (calc-top-list-n 3)))
209 (if (equal old "") (setq old "local"))
211 (setq new (read-string (concat "From time zone: " old
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)))))
223 (defun calc-new-week (arg)
225 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg))
227 (defun calc-new-month (arg)
229 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg))
231 (defun calc-new-year (arg)
233 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg))
235 (defun calc-inc-month (arg)
237 (calc-date-one-arg "incm" 'calcFunc-incmonth arg))
239 (defun calc-business-days-plus (arg)
242 (calc-binary-op "bus+" 'calcFunc-badd arg)))
244 (defun calc-business-days-minus (arg)
247 (calc-binary-op "bus-" 'calcFunc-bsub arg)))
249 (defun calc-date-zero-args (prefix func 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))
257 (defun calc-date-one-arg (prefix func 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)))))))
267 ;;;; Hours-minutes-seconds forms.
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)))))
277 (setq s (math-add s -60)
280 (setq m (math-add m -60)
282 (if (not (Math-lessp -60 s))
283 (setq s (math-add s 60)
285 (if (not (Math-lessp -60 m))
286 (setq m (math-add m 60)
289 (setq s (math-add s 60)
292 (setq m (math-add m 60)
294 (if (not (Math-lessp s 60))
295 (setq s (math-add s -60)
297 (if (not (Math-lessp m 60))
298 (setq m (math-add m -60)
300 (if (and (eq (car-safe s) 'float)
301 (<= (+ (math-numdigs (nth 1 s)) (nth 2 s))
302 (- 2 calc-internal-prec)))
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))
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)
320 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
321 ; (math-normalize a)))
323 (let* ((b (math-mul a 3600))
324 (hm (math-trunc (math-div b 60)))
325 (hmd (math-idivmod hm 60)))
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))
333 (or (Math-realp m) (math-reject-arg m 'realp))
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)
340 (math-to-hms (math-add h
341 (math-add (math-div (or m 0) 60)
342 (math-div (or s 0) 3600)))
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))
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)))))
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)))
361 (math-add (math-div (math-add (math-div (nth 3 a)
370 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
371 ;;; These versions are rewritten to use arbitrary-size integers.
373 ;;; A numerical date is the number of days since midnight on
374 ;;; the morning of December 31, 1 B.C. (Gregorian) or January 2, 1 A.D. (Julian).
375 ;;; Emacs's calendar refers to such a date as an absolute date, some Calc function
376 ;;; names also use that terminology. If the date is a non-integer, it represents
377 ;;; 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.
382 (defun math-date-to-gregorian-dt (date)
383 "Return the day (YEAR MONTH DAY) in the Gregorian calendar.
384 DATE is the number of days since December 31, -1 in the Gregorian calendar."
387 (year (math-quotient (math-add date (if (Math-lessp date 711859)
388 365 ; for speed, we take
389 -108)) ; >1950 as a special case
390 (if (math-negp date) 366 365)))
391 ; this result may be an overestimate
393 (while (Math-lessp date (setq temp (math-absolute-from-gregorian-dt year 1 1)))
394 (setq year (math-add year -1)))
395 (if (eq year 0) (setq year -1))
396 (setq date (1+ (math-sub date temp)))
398 (if (math-leap-year-p year)
399 [1 32 61 92 122 153 183 214 245 275 306 336 999]
400 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
401 (while (>= date (aref temp month))
402 (setq month (1+ month)))
403 (setq day (1+ (- date (aref temp (1- month)))))
404 (list year month day)))
406 (defun math-date-to-julian-dt (date)
407 "Return the day (YEAR MONTH DAY) in the Julian calendar.
408 DATE is the number of days since December 31, -1 in the Gregorian calendar."
411 (year (math-quotient (math-add date (if (Math-lessp date 711859)
412 367 ; for speed, we take
413 -106)) ; >1950 as a special case
414 (if (math-negp date) 366 365)))
415 ; this result may be an overestimate
417 (while (Math-lessp date (setq temp (math-absolute-from-julian-dt year 1 1)))
418 (setq year (math-add year -1)))
419 (if (eq year 0) (setq year -1))
420 (setq date (1+ (math-sub date temp)))
422 (if (math-leap-year-p year t)
423 [1 32 61 92 122 153 183 214 245 275 306 336 999]
424 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
425 (while (>= date (aref temp month))
426 (setq month (1+ month)))
427 (setq day (1+ (- date (aref temp (1- month)))))
428 (list year month day)))
430 (defun math-date-to-dt (value)
431 "Return the day and time of VALUE.
432 The integer part of VALUE is the number of days since Dec 31, -1
433 in the Gregorian calendar and the remaining part determines the time."
434 (if (eq (car-safe value) 'date)
435 (setq value (nth 1 value)))
436 (or (math-realp value)
437 (math-reject-arg value 'datep))
438 (let* ((parts (math-date-parts value))
441 (dt (if (and calc-gregorian-switch
444 (nth 3 calc-gregorian-switch)
445 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))
447 (math-date-to-julian-dt date)
448 (math-date-to-gregorian-dt date))))
449 (if (math-integerp value)
455 (math-add (% time 60) (nth 2 parts)))))))
457 (defun math-dt-to-date (dt)
458 (or (integerp (nth 1 dt))
459 (math-reject-arg (nth 1 dt) 'fixnump))
460 (if (or (< (nth 1 dt) 1) (> (nth 1 dt) 12))
461 (math-reject-arg (nth 1 dt) "Month value is out of range"))
462 (or (integerp (nth 2 dt))
463 (math-reject-arg (nth 2 dt) 'fixnump))
464 (if (or (< (nth 2 dt) 1) (> (nth 2 dt) 31))
465 (math-reject-arg (nth 2 dt) "Day value is out of range"))
466 (let ((date (math-absolute-from-dt (car dt) (nth 1 dt) (nth 2 dt))))
468 (math-add (math-float date)
469 (math-div (math-add (+ (* (nth 3 dt) 3600)
475 (defun math-date-parts (value &optional offset)
476 (let* ((date (math-floor value))
477 (time (math-round (math-mul (math-sub value (or offset date)) 86400)
478 (and (> calc-internal-prec 12)
479 (- calc-internal-prec 12))))
480 (ftime (math-floor time)))
483 (math-sub time ftime))))
486 (defun math-this-year ()
487 (nth 5 (decode-time)))
489 (defun math-leap-year-p (year &optional julian)
490 "Non-nil if YEAR is a leap year.
491 If JULIAN is non-nil, then use the criterion for leap years
492 in the Julian calendar, otherwise use the criterion in the
496 (= (math-imod (math-neg year) 4) 1)
497 (= (math-imod year 4) 0))
499 (setq year (math-sub -1 year)))
500 (setq year (math-imod year 400))
501 (or (and (= (% year 4) 0) (/= (% year 100) 0))
504 (defun math-days-in-month (year month)
505 (if (and (= month 2) (math-leap-year-p year))
507 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
509 (defun math-day-in-year (year month day &optional julian)
510 "Return the number of days of the year up to YEAR MONTH DAY.
511 The count includes the given date.
512 If JULIAN is non-nil, use the Julian calendar, otherwise
513 use the Gregorian calendar."
514 (let ((day-of-year (+ day (* 31 (1- month)))))
517 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
518 (if (math-leap-year-p year julian)
519 (setq day-of-year (1+ day-of-year)))))
522 (defun math-day-number (year month day)
523 "Return the number of days of the year up to YEAR MONTH DAY.
524 The count includes the given date."
525 (if calc-gregorian-switch
526 (cond ((eq year (nth 0 calc-gregorian-switch))
528 (- (math-absolute-from-dt year month day)
529 (math-absolute-from-dt year 1 1))))
530 ((Math-lessp year (nth 0 calc-gregorian-switch))
531 (math-day-in-year year month day t))
533 (math-day-in-year year month day)))
534 (math-day-in-year year month day)))
536 (defun math-dt-before-p (dt1 dt2)
537 "Non-nil if DT1 occurs before DT2.
538 A DT is a list of the form (YEAR MONTH DAY)."
539 (or (Math-lessp (nth 0 dt1) (nth 0 dt2))
540 (and (equal (nth 0 dt1) (nth 0 dt2))
541 (or (< (nth 1 dt1) (nth 1 dt2))
542 (and (= (nth 1 dt1) (nth 1 dt2))
543 (< (nth 2 dt1) (nth 2 dt2)))))))
545 (defun math-absolute-from-gregorian-dt (year month day)
546 "Return the DATE of the day given by the Gregorian day YEAR MONTH DAY.
547 Recall that DATE is the number of days since December 31, -1
548 in the Gregorian calendar."
549 (if (eq year 0) (setq year -1))
550 (let ((yearm1 (math-sub year 1)))
552 ;; Add the number of days of the year and the numbers of days
553 ;; in the previous years (leap year days to be added separately)
554 (math-add (math-day-in-year year month day)
555 (math-add (math-mul 365 yearm1)
556 ;; Add the number of Julian leap years
558 (math-quotient yearm1 4)
560 (math-quotient (math-sub 3 year)
562 ;; Subtract the number of Julian leap years which are not
563 ;; Gregorian leap years. In C=4N+r centuries, there will
564 ;; be 3N+r of these days. The following will compute
566 (let* ((correction (math-mul (math-quotient yearm1 100) 3))
567 (res (math-idivmod correction 4)))
568 (math-add (if (= (cdr res) 0)
573 (defun math-absolute-from-julian-dt (year month day)
574 "Return the DATE of the day given by the Julian day YEAR MONTH DAY.
575 Recall that DATE is the number of days since December 31, -1
576 in the Gregorian calendar."
577 (if (eq year 0) (setq year -1))
578 (let ((yearm1 (math-sub year 1)))
580 ;; Add the number of days of the year and the numbers of days
581 ;; in the previous years (leap year days to be added separately)
582 (math-add (math-day-in-year year month day)
583 (math-add (math-mul 365 yearm1)
584 ;; Add the number of Julian leap years
586 (math-quotient yearm1 4)
588 (math-quotient (math-sub 3 year)
590 ;; Adjustment, since January 1, 1 (Julian) is absolute day -1
593 ;; calc-gregorian-switch is a customizable variable defined in calc.el
594 (defvar calc-gregorian-switch)
597 (defun math-absolute-from-dt (year month day)
598 "Return the DATE of the day given by the day YEAR MONTH DAY.
599 Recall that DATE is the number of days since December 31, -1
600 in the Gregorian calendar."
601 (if (and calc-gregorian-switch
602 ;; The next few lines determine if the given date
603 ;; occurs before the switch to the Gregorian calendar.
604 (math-dt-before-p (list year month day) calc-gregorian-switch))
605 (math-absolute-from-julian-dt year month day)
606 (math-absolute-from-gregorian-dt year month day)))
608 ;;; It is safe to redefine these in your init file to use a different
611 (defvar math-long-weekday-names '( "Sunday" "Monday" "Tuesday" "Wednesday"
612 "Thursday" "Friday" "Saturday" ))
613 (defvar math-short-weekday-names '( "Sun" "Mon" "Tue" "Wed"
616 (defvar math-long-month-names '( "January" "February" "March" "April"
617 "May" "June" "July" "August"
618 "September" "October" "November" "December" ))
619 (defvar math-short-month-names '( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
620 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
623 (defvar math-format-date-cache nil)
625 ;; The variables math-fd-date, math-fd-dt, math-fd-year,
626 ;; math-fd-month, math-fd-day, math-fd-weekday, math-fd-hour,
627 ;; math-fd-minute, math-fd-second, math-fd-bc-flag are local
628 ;; to math-format-date, but are used by math-format-date-part,
629 ;; which is called by math-format-date.
630 (defvar math-fd-date)
632 (defvar math-fd-year)
633 (defvar math-fd-month)
635 (defvar math-fd-weekday)
636 (defvar math-fd-hour)
637 (defvar math-fd-minute)
638 (defvar math-fd-second)
639 (defvar math-fd-bc-flag)
641 (defun math-format-date (math-fd-date)
642 (if (eq (car-safe math-fd-date) 'date)
643 (setq math-fd-date (nth 1 math-fd-date)))
644 (let ((entry (list math-fd-date calc-internal-prec calc-date-format)))
645 (or (cdr (assoc entry math-format-date-cache))
646 (let* ((math-fd-dt nil)
647 (calc-group-digits nil)
648 (calc-leading-zeros nil)
649 (calc-number-radix 10)
650 (calc-twos-complement-mode nil)
651 math-fd-year math-fd-month math-fd-day math-fd-weekday
652 math-fd-hour math-fd-minute math-fd-second
653 (math-fd-bc-flag nil)
654 (fmt (apply 'concat (mapcar 'math-format-date-part
656 (setq math-format-date-cache (cons (cons entry fmt)
657 math-format-date-cache))
658 (and (setq math-fd-dt (nthcdr 10 math-format-date-cache))
659 (setcdr math-fd-dt nil))
662 (defconst math-julian-date-beginning '(float 17214225 -1)
663 "The beginning of the Julian date calendar,
664 as measured in the number of days before December 31, 1 BC (Gregorian).")
666 (defconst math-julian-date-beginning-int 1721423
667 "The beginning of the Julian date calendar,
668 as measured in the integer number of days before December 31, 1 BC (Gregorian).")
670 (defun math-format-date-part (x)
674 (if (math-integerp math-fd-date)
676 (apply 'concat (mapcar 'math-format-date-part x))))
680 (math-format-number math-fd-date))
682 (math-format-number (math-floor math-fd-date)))
685 (math-add math-fd-date math-julian-date-beginning)))
687 (math-format-number (math-add
688 (math-floor math-fd-date)
689 math-julian-date-beginning-int)))
691 (math-format-number (nth 1 (math-date-parts math-fd-date 719164))))
695 (setq math-fd-dt (math-date-to-dt math-fd-date)
696 math-fd-year (car math-fd-dt)
697 math-fd-month (nth 1 math-fd-dt)
698 math-fd-day (nth 2 math-fd-dt)
699 math-fd-weekday (math-mod (math-floor math-fd-date) 7)
700 math-fd-hour (nth 3 math-fd-dt)
701 math-fd-minute (nth 4 math-fd-dt)
702 math-fd-second (nth 5 math-fd-dt))
703 (and (memq 'b calc-date-format)
704 (math-negp math-fd-year)
705 (setq math-fd-year (math-neg math-fd-year)
706 math-fd-bc-flag t))))
708 (if (and (integerp math-fd-year) (> math-fd-year 1940) (< math-fd-year 2040))
709 (format (cond ((eq x 'YY) "%02d")
712 (% math-fd-year 100))
713 (if (and (natnump math-fd-year) (< math-fd-year 100))
714 (format "+%d" math-fd-year)
715 (math-format-number math-fd-year))))
717 (math-format-number math-fd-year))
719 (if (and (natnump math-fd-year) (< math-fd-year 100))
720 (format "+%d" math-fd-year)
721 (math-format-number math-fd-year)))
724 (and (not math-fd-bc-flag) "ad"))
726 (and (not math-fd-bc-flag) "AD"))
728 (and (not math-fd-bc-flag) "ad "))
730 (and (not math-fd-bc-flag) "AD "))
732 (and (not math-fd-bc-flag) "a.d."))
734 (and (not math-fd-bc-flag) "A.D."))
736 (and math-fd-bc-flag "bc"))
738 (and math-fd-bc-flag "BC"))
740 (and math-fd-bc-flag " bc"))
742 (and math-fd-bc-flag " BC"))
744 (and math-fd-bc-flag "b.c."))
746 (and math-fd-bc-flag "B.C."))
748 (format "%d" math-fd-month))
750 (format "%02d" math-fd-month))
752 (format "%2d" math-fd-month))
754 (downcase (nth (1- math-fd-month) math-short-month-names)))
756 (nth (1- math-fd-month) math-short-month-names))
758 (upcase (nth (1- math-fd-month) math-short-month-names)))
760 (nth (1- math-fd-month) math-long-month-names))
762 (upcase (nth (1- math-fd-month) math-long-month-names)))
764 (format "%d" math-fd-day))
766 (format "%02d" math-fd-day))
768 (format "%2d" math-fd-day))
770 (format "%d" math-fd-weekday))
772 (downcase (nth math-fd-weekday math-short-weekday-names)))
774 (nth math-fd-weekday math-short-weekday-names))
776 (upcase (nth math-fd-weekday math-short-weekday-names)))
778 (nth math-fd-weekday math-long-weekday-names))
780 (upcase (nth math-fd-weekday math-long-weekday-names)))
782 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day)))
784 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day)))
786 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day)))
788 (and math-fd-hour (format "%d" math-fd-hour)))
790 (and math-fd-hour (format "%02d" math-fd-hour)))
792 (and math-fd-hour (format "%2d" math-fd-hour)))
794 (and math-fd-hour (format "%d" (1+ (% (+ math-fd-hour 11) 12)))))
796 (and math-fd-hour (format "%02d" (1+ (% (+ math-fd-hour 11) 12)))))
798 (and math-fd-hour (format "%2d" (1+ (% (+ math-fd-hour 11) 12)))))
800 (and math-fd-hour (if (< math-fd-hour 12) "a" "p")))
802 (and math-fd-hour (if (< math-fd-hour 12) "A" "P")))
804 (and math-fd-hour (if (< math-fd-hour 12) "am" "pm")))
806 (and math-fd-hour (if (< math-fd-hour 12) "AM" "PM")))
808 (and math-fd-hour (if (< math-fd-hour 12) "a.m." "p.m.")))
810 (and math-fd-hour (if (< math-fd-hour 12) "A.M." "P.M.")))
812 (and math-fd-minute (format "%d" math-fd-minute)))
814 (and math-fd-minute (format "%02d" math-fd-minute)))
816 (and math-fd-minute (format "%2d" math-fd-minute)))
818 (and math-fd-second (not (math-zerop math-fd-second))
820 ((memq x '(s ss bs SS BS))
822 (not (and (memq x '(SS BS)) (math-zerop math-fd-second)))
823 (if (integerp math-fd-second)
824 (format (cond ((memq x '(ss SS)) "%02d")
825 ((memq x '(bs BS)) "%2d")
828 (concat (if (Math-lessp math-fd-second 10)
829 (cond ((memq x '(ss SS)) "0")
830 ((memq x '(bs BS)) " ")
833 (let ((calc-float-format
834 (list 'fix (min (- 12 calc-internal-prec)
836 (math-format-number math-fd-second))))))))
838 ;; The variable math-pd-str is local to math-parse-date and
839 ;; math-parse-standard-date, but is used by math-parse-date-word,
840 ;; which is called by math-parse-date and math-parse-standard-date.
843 (defun math-parse-date (math-pd-str)
845 (or (math-parse-standard-date math-pd-str t)
846 (math-parse-standard-date math-pd-str nil)
847 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str)
848 (list 'date (math-read-number (math-match-substring math-pd-str 1))))
849 (let ((case-fold-search t)
850 (year nil) (month nil) (day nil) (weekday nil)
851 (hour nil) (minute nil) (second nil) (bc-flag nil)
852 (a nil) (b nil) (c nil) (bigyear nil) temp)
854 ;; Extract the time, if any.
855 (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)
856 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str))
857 (let ((ampm (math-match-substring math-pd-str 6)))
858 (setq hour (string-to-number (math-match-substring math-pd-str 1))
859 minute (math-match-substring math-pd-str 2)
860 second (math-match-substring math-pd-str 4)
861 math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
862 (substring math-pd-str (match-end 0))))
863 (if (equal minute "")
865 (setq minute (string-to-number minute)))
866 (if (equal second "")
868 (setq second (math-read-number second)))
871 (throw 'syntax "Hour value out of range"))
872 (setq ampm (upcase (aref ampm 0)))
873 (if (memq ampm '(?N ?M))
874 (if (and (= hour 12) (= minute 0) (eq second 0))
875 (if (eq ampm ?M) (setq hour 0))
877 "Time must be 12:00:00 in this context"))
878 (if (or (= hour 0) (> hour 12))
879 (throw 'syntax "Hour value out of range"))
880 (if (eq (= ampm ?A) (= hour 12))
881 (setq hour (% (+ hour 12) 24)))))))
883 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
884 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str)
886 (setq math-pd-str (copy-sequence math-pd-str))
887 (aset math-pd-str (match-beginning 1) ?\/)))
889 ;; Extract obvious month or weekday names.
890 (if (string-match "[a-zA-Z]" math-pd-str)
892 (setq month (math-parse-date-word math-long-month-names))
893 (setq weekday (math-parse-date-word math-long-weekday-names))
894 (or month (setq month
895 (math-parse-date-word math-short-month-names)))
896 (or weekday (math-parse-date-word math-short-weekday-names))
898 (if (setq temp (math-parse-date-word
899 '( "noon" "midnight" "mid" )))
900 (setq hour (if (= temp 1) 12 0) minute 0 second 0)))
901 (or (math-parse-date-word '( "ad" "a.d." ))
902 (if (math-parse-date-word '( "bc" "b.c." ))
904 (if (string-match "[a-zA-Z]+" math-pd-str)
905 (throw 'syntax (format "Bad word in date: \"%s\""
906 (math-match-substring math-pd-str 0))))))
908 ;; If there is a huge number other than the year, ignore it.
909 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str)
910 (setq temp (concat (substring math-pd-str 0 (match-beginning 0))
911 (substring math-pd-str (match-end 0))))
913 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp))
914 (setq math-pd-str temp))
916 ;; If there is a number with a sign or a large number, it is a year.
917 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str)
918 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str))
919 (setq year (math-match-substring math-pd-str 1)
920 math-pd-str (concat (substring math-pd-str 0 (match-beginning 1))
921 (substring math-pd-str (match-end 1)))
922 year (math-read-number year)
925 ;; Collect remaining numbers.
927 (while (string-match "[0-9]+" math-pd-str temp)
928 (and c (throw 'syntax "Too many numbers in date"))
929 (setq c (string-to-number (math-match-substring math-pd-str 0)))
930 (or b (setq b c c nil))
931 (or a (setq a b b nil))
932 (setq temp (match-end 0)))
934 ;; Check that we have the right amount of information.
935 (setq temp (+ (if year 1 0) (if month 1 0) (if day 1 0)
936 (if a 1 0) (if b 1 0) (if c 1 0)))
938 (throw 'syntax "Too many numbers in date")
939 (if (or (< temp 2) (and year (= temp 2)))
940 (throw 'syntax "Not enough numbers in date")
941 (if (= temp 2) ; if year omitted, assume current year
942 (setq year (math-this-year)))))
944 ;; A large number must be a year.
946 (if (and a (or (> a 31) (< a 1)))
947 (setq year a a b b c c nil)
948 (if (and b (or (> b 31) (< b 1)))
949 (setq year b b c c nil)
950 (if (and c (or (> c 31) (< c 1)))
951 (setq year c c nil)))))
953 ;; A medium-large number must be a day.
956 (setq day a a b b c c nil)
958 (setq day b b c c nil)
960 (setq day c c nil)))))
962 ;; We may know enough to sort it out now.
964 (or month (setq month a))
968 ;; Interpret order of numbers as same as for display format.
969 (setq temp calc-date-format)
971 (cond ((not (symbolp (car temp))))
972 ((memq (car temp) '(Y YY BY YYY YYYY))
973 (or year (setq year a a b b c)))
974 ((memq (car temp) '(M MM BM mmm Mmm Mmmm MMM MMMM))
975 (or month (setq month a a b b c)))
976 ((memq (car temp) '(D DD BD))
977 (or day (setq day a a b b c))))
978 (setq temp (cdr temp)))
980 ;; If display format was not complete, assume American style.
981 (or month (setq month a a b b c))
982 (or day (setq day a a b b c))
983 (or year (setq year a a b b c))))
986 (setq year (math-neg (math-abs year))))
988 (math-parse-date-validate year bigyear month day
989 hour minute second)))))
991 (defun math-parse-date-validate (year bigyear month day hour minute second)
992 (and (not bigyear) (natnump year) (< year 100)
993 (setq year (+ year (if (< year 40) 2000 1900))))
995 (throw 'syntax "Year value is out of range"))
996 (if (or (< month 1) (> month 12))
997 (throw 'syntax "Month value is out of range"))
998 (if (or (< day 1) (> day (math-days-in-month year month)))
999 (throw 'syntax "Day value is out of range"))
1002 (if (or (< hour 0) (> hour 23))
1003 (throw 'syntax "Hour value is out of range"))
1004 (if (or (< minute 0) (> minute 59))
1005 (throw 'syntax "Minute value is out of range"))
1006 (if (or (math-negp second) (not (Math-lessp second 60)))
1007 (throw 'syntax "Seconds value is out of range"))))
1008 (list 'date (math-dt-to-date (append (list year month day)
1009 (and hour (list hour minute second))))))
1011 (defun math-parse-date-word (names &optional front)
1013 (while (and names (not (string-match (if (equal (car names) "Sep")
1015 (regexp-quote (car names)))
1017 (setq names (cdr names)
1020 (or (not front) (= (match-beginning 0) 0))
1022 (setq math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
1024 (substring math-pd-str (match-end 0))))
1027 (defun math-parse-standard-date (math-pd-str with-time)
1028 (let ((case-fold-search t)
1030 (fmt calc-date-format) this next (gnext nil)
1031 (year nil) (month nil) (day nil) (bigyear nil) (yearday nil)
1032 (hour nil) (minute nil) (second nil) (bc-flag nil))
1033 (while (and fmt okay)
1034 (setq this (car fmt)
1035 fmt (setq fmt (or (cdr fmt)
1040 (if (consp next) (setq next (car next)))
1041 (or (cond ((listp this)
1047 (if (and (<= (length this) (length math-pd-str))
1049 (substring math-pd-str 0 (length this))))
1050 (setq math-pd-str (substring math-pd-str (length this)))))
1053 ((memq this '(n N j J))
1054 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str)
1055 (setq num (math-match-substring math-pd-str 0)
1056 math-pd-str (substring math-pd-str (match-end 0))
1057 num (math-date-to-dt (math-read-number num))
1059 (if (memq this '(n N))
1061 (if (or (eq this 'j)
1062 (math-integerp num))
1063 math-julian-date-beginning-int
1064 math-julian-date-beginning)))
1065 hour (or (nth 3 num) hour)
1066 minute (or (nth 4 num) minute)
1067 second (or (nth 5 num) second)
1072 (and (string-match "\\`[-+]?[0-9]+" math-pd-str)
1073 (setq num (math-match-substring math-pd-str 0)
1074 math-pd-str (substring math-pd-str (match-end 0))
1075 num (math-date-to-dt
1077 (math-div (math-read-number num)
1085 ((memq this '(mmm Mmm MMM))
1086 (setq month (math-parse-date-word math-short-month-names t)))
1087 ((memq this '(Mmmm MMMM))
1088 (setq month (math-parse-date-word math-long-month-names t)))
1089 ((memq this '(www Www WWW))
1090 (math-parse-date-word math-short-weekday-names t))
1091 ((memq this '(Wwww WWWW))
1092 (math-parse-date-word math-long-weekday-names t))
1094 (if (string-match "\\`a" math-pd-str)
1095 (setq hour (if (= hour 12) 0 hour)
1096 math-pd-str (substring math-pd-str 1))
1097 (if (string-match "\\`p" math-pd-str)
1098 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
1099 math-pd-str (substring math-pd-str 1)))))
1100 ((memq this '(pp PP pppp PPPP))
1101 (if (string-match "\\`am\\|a\\.m\\." math-pd-str)
1102 (setq hour (if (= hour 12) 0 hour)
1103 math-pd-str (substring math-pd-str (match-end 0)))
1104 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str)
1105 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
1106 math-pd-str (substring math-pd-str (match-end 0))))))
1107 ((memq this '(Y YY BY YYY YYYY))
1108 (and (if (memq next '(MM DD ddd hh HH mm ss SS))
1109 (if (memq this '(Y YY BYY))
1110 (string-match "\\` *[0-9][0-9]" math-pd-str)
1111 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str))
1112 (string-match "\\`[-+]?[0-9]+" math-pd-str))
1113 (setq year (math-match-substring math-pd-str 0)
1114 bigyear (or (eq this 'YYY)
1115 (memq (aref math-pd-str 0) '(?\+ ?\-)))
1116 math-pd-str (substring math-pd-str (match-end 0))
1117 year (math-read-number year))))
1120 ((memq this '(aa AA aaaa AAAA))
1121 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str)
1122 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1123 ((memq this '(aaa AAA))
1124 (if (string-match "\\` *ad *" math-pd-str)
1125 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1126 ((memq this '(bb BB bbb BBB bbbb BBBB))
1127 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str)
1128 (setq math-pd-str (substring math-pd-str (match-end 0))
1130 ((memq this '(s ss bs SS BS))
1131 (and (if (memq next '(YY YYYY MM DD hh HH mm))
1132 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str)
1133 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str))
1134 (setq second (math-match-substring math-pd-str 0)
1135 math-pd-str (substring math-pd-str (match-end 0))
1136 second (math-read-number second))))
1138 (if (string-match "\\`:[0-9][0-9]" math-pd-str)
1139 (setq math-pd-str (substring math-pd-str 1))
1141 ((or (not (if (and (memq this '(ddd MM DD hh HH mm))
1142 (memq next '(YY YYYY MM DD ddd
1145 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str)
1146 (string-match "\\` *[0-9][0-9]" math-pd-str))
1147 (string-match "\\` *[0-9]+" math-pd-str)))
1148 (and (setq num (string-to-number
1149 (math-match-substring math-pd-str 0))
1150 math-pd-str (substring math-pd-str (match-end 0)))
1154 (and (>= num 0) (< num 7)))
1155 ((memq this '(d ddd bdd))
1157 ((memq this '(M MM BM))
1159 ((memq this '(D DD BD))
1161 ((memq this '(h hh bh H HH BH))
1163 ((memq this '(m mm bm))
1169 (setq month 1 day 1)))
1170 (if (and okay (equal math-pd-str ""))
1171 (and month day (or (not (or hour minute second))
1174 (or year (setq year (math-this-year)))
1175 (or second (setq second 0))
1177 (setq year (math-neg (math-abs year))))
1178 (setq day (math-parse-date-validate year bigyear month day
1179 hour minute second))
1181 (setq day (math-add day (1- yearday))))
1185 (defun calcFunc-now (&optional zone)
1186 (let ((date (let ((calc-date-format nil))
1187 (math-parse-date (current-time-string)))))
1190 (math-add date (math-div (math-sub (calcFunc-tzone nil date)
1191 (calcFunc-tzone zone date))
1194 (calc-record-why "*Unable to interpret current date from system")
1195 (append (list 'calcFunc-now) (and zone (list zone))))))
1197 (defun calcFunc-year (date)
1198 (car (math-date-to-dt date)))
1200 (defun calcFunc-month (date)
1201 (nth 1 (math-date-to-dt date)))
1203 (defun calcFunc-day (date)
1204 (nth 2 (math-date-to-dt date)))
1206 (defun calcFunc-weekday (date)
1207 (if (eq (car-safe date) 'date)
1208 (setq date (nth 1 date)))
1209 (or (math-realp date)
1210 (math-reject-arg date 'datep))
1211 (math-mod (math-floor date) 7))
1213 (defun calcFunc-yearday (date)
1214 (let ((dt (math-date-to-dt date)))
1215 (math-day-number (car dt) (nth 1 dt) (nth 2 dt))))
1217 (defun calcFunc-hour (date)
1218 (if (eq (car-safe date) 'hms)
1220 (or (nth 3 (math-date-to-dt date)) 0)))
1222 (defun calcFunc-minute (date)
1223 (if (eq (car-safe date) 'hms)
1225 (or (nth 4 (math-date-to-dt date)) 0)))
1227 (defun calcFunc-second (date)
1228 (if (eq (car-safe date) 'hms)
1230 (or (nth 5 (math-date-to-dt date)) 0)))
1232 (defun calcFunc-time (date)
1233 (let ((dt (math-date-to-dt date)))
1235 (cons 'hms (nthcdr 3 dt))
1236 (list 'hms 0 0 0))))
1238 (defun calcFunc-date (date &optional month day hour minute second)
1239 (and (math-messy-integerp month) (setq month (math-trunc month)))
1240 (and month (not (integerp month)) (math-reject-arg month 'fixnump))
1241 (and (math-messy-integerp day) (setq day (math-trunc day)))
1242 (and day (not (integerp day)) (math-reject-arg day 'fixnump))
1243 (if (and (eq (car-safe hour) 'hms) (not minute))
1244 (setq second (nth 3 hour)
1247 (and (math-messy-integerp hour) (setq hour (math-trunc hour)))
1248 (and hour (not (integerp hour)) (math-reject-arg hour 'fixnump))
1249 (and (math-messy-integerp minute) (setq minute (math-trunc minute)))
1250 (and minute (not (integerp minute)) (math-reject-arg minute 'fixnump))
1251 (and (math-messy-integerp second) (setq second (math-trunc second)))
1252 (and second (not (math-realp second)) (math-reject-arg second 'realp))
1255 (and (math-messy-integerp date) (setq date (math-trunc date)))
1256 (and date (not (math-integerp date)) (math-reject-arg date 'integerp))
1259 (list 'date (math-dt-to-date (list date month day hour
1262 (list 'date (math-dt-to-date (list date month day))))
1263 (list 'date (math-dt-to-date (list (math-this-year) date month)))))
1264 (if (math-realp date)
1266 (if (eq (car date) 'date)
1268 (math-reject-arg date 'datep)))))
1270 (defun calcFunc-julian (date &optional zone)
1271 (if (math-realp date)
1272 (list 'date (if (math-integerp date)
1273 (math-sub date math-julian-date-beginning-int)
1274 (setq date (math-sub date math-julian-date-beginning))
1275 (math-sub date (math-div (calcFunc-tzone zone date)
1277 (if (eq (car date) 'date)
1278 (math-add (nth 1 date) (if (math-integerp (nth 1 date))
1279 math-julian-date-beginning-int
1280 (math-add math-julian-date-beginning
1281 (math-div (calcFunc-tzone zone date)
1283 (math-reject-arg date 'datep))))
1285 (defun calcFunc-unixtime (date &optional zone)
1286 (if (math-realp date)
1288 (setq date (math-add 719164 (math-div date '(float 864 2))))
1289 (list 'date (math-sub date (math-div (calcFunc-tzone zone date)
1291 (if (eq (car date) 'date)
1292 (math-add (nth 1 (math-date-parts (nth 1 date) 719164))
1293 (calcFunc-tzone zone date))
1294 (math-reject-arg date 'datep))))
1297 ;;; Note: Longer names must appear before shorter names which are
1298 ;;; substrings of them.
1299 (defvar math-tzone-names
1301 ( "MEGT" -1 "MET" "METDST" ) ; Middle Europe
1302 ( "METDST" -1 -1 ) ( "MET" -1 0 )
1303 ( "MEGZ" -1 "MEZ" "MESZ" ) ( "MEZ" -1 0 ) ( "MESZ" -1 -1 )
1304 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1305 ( "WETDST" 0 -1 ) ( "WET" 0 0 )
1306 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -1 ) ; Britain
1307 ( "NGT" (float 35 -1) "NST" "NDT" ) ; Newfoundland
1308 ( "NST" (float 35 -1) 0 ) ( "NDT" (float 35 -1) -1 )
1309 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -1 ) ; Atlantic
1310 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -1 ) ; Eastern
1311 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -1 ) ; Central
1312 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -1 ) ; Mountain
1313 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -1 ) ; Pacific
1314 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -1 ) ; Yukon
1316 "No doc yet. See calc manual for now. ")
1318 (defvar var-TimeZone nil)
1321 (defvar calendar-current-time-zone-cache)
1323 (defvar math-calendar-tzinfo
1325 "Information about the timezone, retrieved from the calendar.")
1327 (defun math-get-calendar-tzinfo ()
1328 "Get information about the timezone from the calendar.
1329 The result should be a list of two items about the current time zone:
1330 first, the number of seconds difference from GMT
1331 second, the number of seconds offset for daylight savings."
1332 (if math-calendar-tzinfo
1333 math-calendar-tzinfo
1335 (let ((tzinfo (progn
1336 (calendar-current-time-zone)
1337 calendar-current-time-zone-cache)))
1338 (setq math-calendar-tzinfo
1339 (list (* 60 (abs (nth 0 tzinfo)))
1340 (* 60 (nth 1 tzinfo)))))))
1342 (defun calcFunc-tzone (&optional zone date)
1344 (cond ((math-realp zone)
1345 (math-round (math-mul zone 3600)))
1346 ((eq (car zone) 'hms)
1347 (math-round (math-mul (math-from-hms zone 'deg) 3600)))
1349 (math-add (calcFunc-tzone (nth 1 zone) date)
1350 (calcFunc-tzone (nth 2 zone) date)))
1352 (math-sub (calcFunc-tzone (nth 1 zone) date)
1353 (calcFunc-tzone (nth 2 zone) date)))
1354 ((eq (car zone) 'var)
1355 (let ((name (upcase (symbol-name (nth 1 zone))))
1357 (if (setq found (assoc name math-tzone-names))
1358 (calcFunc-tzone (math-add (nth 1 found)
1359 (if (integerp (nth 2 found))
1362 (math-daylight-savings-adjust
1366 (if (equal name "LOCAL")
1367 (calcFunc-tzone nil date)
1368 (math-reject-arg zone "*Unrecognized time zone name")))))
1369 (t (math-reject-arg zone "*Expected a time zone")))
1370 (if (calc-var-value 'var-TimeZone)
1371 (calcFunc-tzone (calc-var-value 'var-TimeZone) date)
1372 (let ((tzinfo (math-get-calendar-tzinfo)))
1374 (* (math-cal-daylight-savings-adjust date) (nth 1 tzinfo)))))))
1376 (defvar math-daylight-savings-hook 'math-std-daylight-savings)
1378 (defun math-daylight-savings-adjust (date zone &optional dt)
1379 (or date (setq date (nth 1 (calcFunc-now))))
1381 (if (eq (car-safe date) 'date)
1384 (if (and date (math-realp date))
1385 (let ((zadj (assoc zone math-tzone-names)))
1386 (if zadj (setq bump -1
1387 date (math-sub date (math-div (nth 1 zadj)
1389 (math-reject-arg date 'datep)))
1390 (setq date (math-float date))
1391 (or dt (setq dt (math-date-to-dt date)))
1392 (and math-daylight-savings-hook
1393 (funcall math-daylight-savings-hook date dt zone bump))))
1395 ;;; Based on part of dst-adjust-time in cal-dst.el
1396 ;;; For calcFunc-dst, when zone=nil
1397 (defun math-cal-daylight-savings-adjust (date)
1398 "Return -1 if DATE is using daylight saving, 0 otherwise."
1400 (unless date (setq date (calcFunc-now)))
1401 (let* ((dt (math-date-to-dt date))
1406 (+ (nth 3 dt) (/ (nth 4 dt) 60.0)))
1411 (calendar-absolute-from-gregorian
1412 (list (nth 1 dt) (nth 2 dt) (nth 0 dt)))
1413 (/ (round (* 60 time)) 60.0 24.0))))
1414 (if (dst-in-effect rounded-abs-date)
1418 (defun calcFunc-dsadj (date &optional zone)
1420 (or (eq (car-safe zone) 'var)
1421 (math-reject-arg zone "*Time zone variable expected"))
1422 (setq zone (calc-var-value 'var-TimeZone)))
1425 (setq zone (and (eq (car-safe zone) 'var)
1426 (upcase (symbol-name (nth 1 zone)))))
1427 (let ((zadj (assoc zone math-tzone-names)))
1428 (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1429 (if (integerp (nth 2 zadj))
1431 (math-daylight-savings-adjust date zone))))
1432 (math-cal-daylight-savings-adjust date)))
1434 ;; (defun calcFunc-dsadj (date &optional zone)
1436 ;; (or (eq (car-safe zone) 'var)
1437 ;; (math-reject-arg zone "*Time zone variable expected"))
1438 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1441 ;; (calc-var-value 'var-TimeZone)))))
1442 ;; (setq zone (and (eq (car-safe zone) 'var)
1443 ;; (upcase (symbol-name (nth 1 zone)))))
1444 ;; (let ((zadj (assoc zone math-tzone-names)))
1445 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1446 ;; (if (integerp (nth 2 zadj))
1448 ;; (math-daylight-savings-adjust date zone))))
1450 (defun calcFunc-tzconv (date z1 z2)
1451 (if (math-realp date)
1452 (nth 1 (calcFunc-tzconv (list 'date date) z1 z2))
1453 (calcFunc-unixtime (calcFunc-unixtime date z1) z2)))
1455 (defun math-std-daylight-savings (date dt zone bump)
1456 "Standard North American daylight saving algorithm.
1457 Before 2007, this uses `math-std-daylight-savings-old', where
1458 daylight saving began on the first Sunday of April at 2 a.m.,
1459 and ended on the last Sunday of October at 2 a.m.
1460 As of 2007, this uses `math-std-daylight-savings-new', where
1461 daylight saving begins on the second Sunday of March at 2 a.m.,
1462 and ends on the first Sunday of November at 2 a.m."
1463 (if (< (car dt) 2007)
1464 (math-std-daylight-savings-old date dt zone bump)
1465 (math-std-daylight-savings-new date dt zone bump)))
1467 (defun math-std-daylight-savings-new (date dt zone bump)
1468 "Standard North American daylight saving algorithm as of 2007.
1469 This implements the rules for the U.S. and Canada.
1470 Daylight saving begins on the second Sunday of March at 2 a.m.,
1471 and ends on the first Sunday of November at 2 a.m."
1472 (cond ((< (nth 1 dt) 3) 0)
1474 (let ((sunday (math-prev-weekday-in-month date dt 14 0)))
1475 (cond ((< (nth 2 dt) sunday) 0)
1476 ((= (nth 2 dt) sunday)
1477 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1479 ((< (nth 1 dt) 11) -1)
1481 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1482 (cond ((< (nth 2 dt) sunday) -1)
1483 ((= (nth 2 dt) sunday)
1484 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1488 (defun math-std-daylight-savings-old (date dt zone bump)
1489 "Standard North American daylight saving algorithm before 2007.
1490 This implements the rules for the U.S. and Canada.
1491 Daylight saving begins on the first Sunday of April at 2 a.m.,
1492 and ends on the last Sunday of October at 2 a.m."
1493 (cond ((< (nth 1 dt) 4) 0)
1495 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1496 (cond ((< (nth 2 dt) sunday) 0)
1497 ((= (nth 2 dt) sunday)
1498 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1500 ((< (nth 1 dt) 10) -1)
1502 (let ((sunday (math-prev-weekday-in-month date dt 31 0)))
1503 (cond ((< (nth 2 dt) sunday) -1)
1504 ((= (nth 2 dt) sunday)
1505 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1509 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1510 ;;; day of the given month.
1511 (defun math-prev-weekday-in-month (date dt day wday)
1512 (or day (setq day (nth 2 dt)))
1513 (if (> day (math-days-in-month (car dt) (nth 1 dt)))
1514 (setq day (math-days-in-month (car dt) (nth 1 dt))))
1515 (let ((zeroth (math-sub (math-floor date) (nth 2 dt))))
1516 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day))) zeroth)))
1518 (defun calcFunc-pwday (date &optional day weekday)
1519 (if (eq (car-safe date) 'date)
1520 (setq date (nth 1 date)))
1521 (or (math-realp date)
1522 (math-reject-arg date 'datep))
1523 (if (math-messy-integerp day) (setq day (math-trunc day)))
1524 (or (integerp day) (math-reject-arg day 'fixnump))
1525 (if (= day 0) (setq day 31))
1526 (and (or (< day 7) (> day 31)) (math-reject-arg day 'range))
1527 (math-prev-weekday-in-month date (math-date-to-dt date) day (or weekday 0)))
1530 (defun calcFunc-newweek (date &optional weekday)
1531 (if (eq (car-safe date) 'date)
1532 (setq date (nth 1 date)))
1533 (or (math-realp date)
1534 (math-reject-arg date 'datep))
1535 (or weekday (setq weekday 0))
1536 (and (math-messy-integerp weekday) (setq weekday (math-trunc weekday)))
1537 (or (integerp weekday) (math-reject-arg weekday 'fixnump))
1538 (and (or (< weekday 0) (> weekday 6)) (math-reject-arg weekday 'range))
1539 (setq date (math-floor date))
1540 (list 'date (math-sub date (calcFunc-weekday (math-sub date weekday)))))
1542 (defun calcFunc-newmonth (date &optional day)
1543 (or day (setq day 1))
1544 (and (math-messy-integerp day) (setq day (math-trunc day)))
1545 (or (integerp day) (math-reject-arg day 'fixnump))
1546 (and (or (< day 0) (> day 31)) (math-reject-arg day 'range))
1547 (let* ((dt (math-date-to-dt date))
1548 (dim (math-days-in-month (car dt) (nth 1 dt)))
1549 (julian (if calc-gregorian-switch
1550 (math-date-to-dt (math-sub
1551 (or (nth 3 calc-gregorian-switch)
1552 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))
1554 (if (or (= day 0) (> day dim))
1556 (setq day (1- day)))
1557 ;; Adjust if this occurs near the switch to the Gregorian calendar
1558 (if calc-gregorian-switch
1560 ((and (math-dt-before-p (list (car dt) (nth 1 dt) 1) calc-gregorian-switch)
1561 (math-dt-before-p julian (list (car dt) (nth 1 dt) 1)))
1562 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the month
1564 (math-dt-to-date (list (car calc-gregorian-switch)
1565 (nth 1 calc-gregorian-switch)
1566 (if (> (+ (nth 2 calc-gregorian-switch) day) dim)
1568 (+ (nth 2 calc-gregorian-switch) day))))))
1569 ((and (eq (car dt) (car calc-gregorian-switch))
1570 (= (nth 1 dt) (nth 1 calc-gregorian-switch)))
1571 ;; In this case, the switch to the Gregorian calendar occurs in the given month
1572 (if (< (+ (nth 2 julian) day) (nth 2 calc-gregorian-switch))
1573 ;; If the DAYth day occurs before the switch, use it
1574 (list 'date (math-dt-to-date (list (car dt) (nth 1 dt) (1+ day))))
1575 ;; Otherwise do some computations
1576 (let ((tm (+ day (- (nth 2 calc-gregorian-switch) (nth 2 julian)))))
1577 (list 'date (math-dt-to-date
1581 (if (> tm dim) dim tm)))))))
1582 ((and (eq (car dt) (car julian))
1583 (= (nth 1 dt) (nth 1 julian)))
1584 ;; In this case, the current month is truncated because of the switch
1585 ;; to the Gregorian calendar
1586 (list 'date (math-dt-to-date
1589 (if (>= day (nth 2 julian))
1594 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) day))))
1595 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) day)))))
1597 (defun calcFunc-newyear (date &optional day)
1598 (if (eq (car-safe date) 'date) (setq date (nth 1 date)))
1599 (or day (setq day 1))
1600 (and (math-messy-integerp day) (setq day (math-trunc day)))
1601 (or (integerp day) (math-reject-arg day 'fixnump))
1602 (let* ((dt (math-date-to-dt date))
1603 (gregbeg (if calc-gregorian-switch
1604 (or (nth 3 calc-gregorian-switch)
1605 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))))
1606 (julianend (if calc-gregorian-switch (math-sub gregbeg 1)))
1607 (julian (if calc-gregorian-switch
1608 (math-date-to-dt julianend))))
1609 (if (and (>= day 0) (<= day 366))
1610 (let ((max (if (math-leap-year-p (car dt)) 366 365)))
1611 (if (or (= day 0) (> day max)) (setq day max))
1612 (if calc-gregorian-switch
1613 ;; Now to break this down into cases
1615 ((and (math-dt-before-p (list (car dt) 1 1) calc-gregorian-switch)
1616 (math-dt-before-p julian (list (car dt) 1 1)))
1617 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the year
1618 (list 'date (math-min (math-add gregbeg (1- day))
1619 (math-dt-to-date (list (car calc-gregorian-switch) 12 31)))))
1620 ((eq (car dt) (car julian))
1621 ;; In this case, the switch to the Gregorian calendar occurs in the given year
1622 (if (Math-lessp (car julian) (car calc-gregorian-switch))
1623 ;; Here, the last Julian day is the last day of the year.
1624 (list 'date (math-min (math-add (math-dt-to-date (list (car dt) 1 1)) (1- day))
1626 ;; Otherwise, just make sure the date doesn't go past the end of the year
1627 (list 'date (math-min (math-add (math-dt-to-date (list (car dt) 1 1)) (1- day))
1628 (math-dt-to-date (list (car dt) 12 31))))))
1630 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1632 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1634 (if (and (>= day -12) (<= day -1))
1635 (if (and calc-gregorian-switch
1636 (math-dt-before-p (list (car dt) (- day) 1) calc-gregorian-switch)
1637 (math-dt-before-p julian (list (car dt) (- day) 1)))
1638 (list 'date gregbeg)
1639 (list 'date (math-dt-to-date (list (car dt) (- day) 1))))
1640 (math-reject-arg day 'range)))))
1642 (defun calcFunc-incmonth (date &optional step)
1643 (or step (setq step 1))
1644 (and (math-messy-integerp step) (setq step (math-trunc step)))
1645 (or (math-integerp step) (math-reject-arg step 'integerp))
1646 (let* ((dt (math-date-to-dt date))
1648 (month (math-add (1- (nth 1 dt)) step))
1649 (extra (calcFunc-idiv month 12))
1651 (setq month (1+ (math-sub month (math-mul extra 12)))
1652 year (math-add year extra)
1653 day (min day (math-days-in-month year month)))
1654 (and (math-posp (car dt)) (not (math-posp year))
1655 (setq year (math-sub year 1))) ; did we go past the year zero?
1656 (and (math-negp (car dt)) (not (math-negp year))
1657 (setq year (math-add year 1)))
1658 (list 'date (math-dt-to-date
1659 (cons year (cons month (cons day (cdr (cdr (cdr dt))))))))))
1661 (defun calcFunc-incyear (date &optional step)
1662 (calcFunc-incmonth date (math-mul (or step 1) 12)))
1666 (defun calcFunc-bsub (a b)
1667 (or (eq (car-safe a) 'date)
1668 (math-reject-arg a 'datep))
1669 (if (eq (car-safe b) 'date)
1670 (if (math-lessp (nth 1 a) (nth 1 b))
1671 (math-neg (calcFunc-bsub b a))
1672 (math-setup-holidays b)
1673 (let* ((da (math-to-business-day a))
1674 (db (math-to-business-day b)))
1675 (math-add (math-sub (car da) (car db))
1676 (if (and (cdr db) (not (cdr da))) 1 0))))
1677 (calcFunc-badd a (math-neg b))))
1679 (defvar math-holidays-cache nil)
1680 (defvar math-holidays-cache-tag t)
1681 (defun calcFunc-badd (a b)
1682 (if (eq (car-safe b) 'date)
1683 (if (eq (car-safe a) 'date)
1684 (math-reject-arg nil "*Invalid combination in date arithmetic")
1685 (calcFunc-badd b a))
1686 (if (eq (car-safe a) 'date)
1690 (let* ((d (math-to-business-day a))
1691 (bb (math-add (car d)
1692 (if (and (cdr d) (Math-posp b))
1693 (math-sub b 1) b))))
1694 (or (math-from-business-day bb)
1695 (calcFunc-badd a b))))
1696 (if (eq (car-safe b) 'hms)
1697 (let ((hours (nth 7 math-holidays-cache)))
1698 (setq b (math-div (math-from-hms b 'deg) 24))
1700 (setq b (math-div b (cdr hours))))
1701 (calcFunc-badd a b))
1702 (math-reject-arg nil "*Invalid combination in date arithmetic")))
1703 (math-reject-arg a 'datep))))
1705 (defun calcFunc-holiday (a)
1706 (if (cdr (math-to-business-day a)) 1 0))
1708 ;;; Compute the number of business days since Jan 1, 1 AD.
1710 (defun math-to-business-day (date &optional need-year)
1711 (if (eq (car-safe date) 'date)
1712 (setq date (nth 1 date)))
1713 (or (Math-realp date)
1714 (math-reject-arg date 'datep))
1715 (let* ((day (math-floor date))
1716 (time (math-sub date day))
1717 (dt (math-date-to-dt day))
1720 (or (not need-year) (eq (car dt) need-year)
1721 (math-reject-arg (list 'date day) "*Generated holiday has wrong year"))
1722 (math-setup-holidays date)
1723 (let ((days (car math-holidays-cache)))
1724 (while (and (setq days (cdr days)) (< (car days) day))
1725 (setq delta (1+ delta)))
1726 (and days (= day (car days))
1728 (let* ((weekdays (nth 3 math-holidays-cache))
1729 (weeks (1- (/ (+ day 6) 7)))
1730 (wkday (- day 1 (* weeks 7))))
1731 (setq delta (+ delta (* weeks (length weekdays))))
1732 (while (and weekdays (< (car weekdays) wkday))
1733 (setq weekdays (cdr weekdays)
1735 (and weekdays (eq wkday (car weekdays))
1737 (let ((hours (nth 7 math-holidays-cache)))
1740 (setq time (math-div (math-sub time (car hours)) (cdr hours)))
1741 (if (Math-lessp time 0) (setq time 0))
1742 (or (Math-lessp time 1)
1745 (math-div 1 (math-mul 86400 (cdr hours)))))))))
1746 (cons (math-add (math-sub day delta) time) holiday)))
1749 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1750 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1752 (defun math-from-business-day (num)
1753 (let* ((day (math-floor num))
1754 (time (math-sub num day)))
1756 (math-reject-arg nil "*Date is outside valid range"))
1757 (math-setup-holidays)
1758 (let ((days (nth 1 math-holidays-cache))
1760 (while (and (setq days (cdr days)) (< (car days) day))
1761 (setq delta (1+ delta)))
1762 (setq day (+ day delta)))
1763 (let* ((weekdays (nth 3 math-holidays-cache))
1764 (bweek (- 7 (length weekdays)))
1765 (weeks (1- (/ (+ day (1- bweek)) bweek)))
1766 (wkday (- day 1 (* weeks bweek)))
1768 (setq day (+ day (* weeks (length weekdays))))
1769 (while (if (memq w weekdays)
1771 (> (setq wkday (1- wkday)) 0))
1773 (let ((hours (nth 7 math-holidays-cache)))
1775 (setq time (math-add (math-mul time (cdr hours)) (car hours)))))
1776 (and (not (math-setup-holidays day))
1777 (list 'date (math-add day time))))))
1779 ;; The variable math-sh-year is local to math-setup-holidays
1780 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1781 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1782 (defvar math-sh-year)
1784 (defun math-setup-holidays (&optional date)
1785 (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag)
1786 (let ((h (calc-var-value 'var-Holidays))
1787 (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3)
1788 (thu . 4) (fri . 5) (sat . 6) ))
1789 (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil))
1790 (or (math-vectorp h)
1791 (math-reject-arg h "*Holidays variable must be a vector"))
1792 (while (setq h (cdr h))
1793 (cond ((or (and (eq (car-safe (car h)) 'date)
1794 (integerp (nth 1 (car h))))
1795 (and (eq (car-safe (car h)) 'intv)
1796 (eq (car-safe (nth 2 (car h))) 'date))
1797 (eq (car-safe (car h)) 'vec))
1798 (setq days (cons (car h) days)))
1799 ((and (eq (car-safe (car h)) 'var)
1800 (assq (nth 1 (car h)) wdnames))
1801 (setq weekdays (cons (cdr (assq (nth 1 (car h)) wdnames))
1803 ((and (eq (car-safe (car h)) 'intv)
1804 (eq (car-safe (nth 2 (car h))) 'hms)
1805 (eq (car-safe (nth 3 (car h))) 'hms))
1808 (car h) "*Only one hours interval allowed in Holidays"))
1809 (setq hours (math-div (car h) '(hms 24 0 0)))
1810 (if (or (Math-lessp (nth 2 hours) 0)
1811 (Math-lessp 1 (nth 3 hours)))
1813 (car h) "*Hours interval out of range"))
1814 (setq hours (cons (nth 2 hours)
1815 (math-sub (nth 3 hours) (nth 2 hours))))
1816 (if (Math-zerop (cdr hours))
1818 (car h) "*Degenerate hours interval")))
1819 ((or (and (eq (car-safe (car h)) 'intv)
1820 (Math-integerp (nth 2 (car h)))
1821 (Math-integerp (nth 3 (car h))))
1822 (and (integerp (car h))
1823 (> (car h) 1900) (< (car h) 2100)))
1826 (car h) "*Only one limit allowed in Holidays"))
1827 (setq limit (calcFunc-vint (car h) '(intv 3 1 2737)))
1828 (if (equal limit '(vec))
1829 (math-reject-arg (car h) "*Limit is out of range")))
1830 ((or (math-expr-contains (car h) '(var y var-y))
1831 (math-expr-contains (car h) '(var m var-m)))
1832 (setq exprs (cons (car h) exprs)))
1834 (car h) "*Holidays must contain a vector of holidays"))))
1835 (if (= (length weekdays) 7)
1836 (math-reject-arg nil "*Too many weekend days"))
1837 (setq math-holidays-cache (list (list -1) ; 0: days list
1838 (list -1) ; 1: inverse-days list
1841 (or limit '(intv 3 1 2737))
1842 nil ; 5: (lo.hi) expanded years
1844 hours) ; 7: business hours
1845 math-holidays-cache-tag (calc-var-value 'var-Holidays))))
1847 (let ((year (calcFunc-year date))
1848 (limits (nth 5 math-holidays-cache))
1850 (or (eq (calcFunc-in year (nth 4 math-holidays-cache)) 1)
1852 (or (eq (car-safe date) 'date) (setq date (list 'date date)))
1853 (math-reject-arg date "*Date is outside valid range")))
1855 (let ((days (nth 6 math-holidays-cache)))
1857 (let ((math-sh-year nil)) ; see below
1858 (setcar (nthcdr 6 math-holidays-cache) nil)
1859 (math-setup-add-holidays (cons 'vec (cdr days)))
1860 (setcar (nthcdr 2 math-holidays-cache) (car days))))
1861 (cond ((not (nth 2 math-holidays-cache))
1865 (setcar (nthcdr 5 math-holidays-cache) (cons year year))
1866 (math-setup-year-holidays year)
1868 ((< year (car limits))
1869 (message "Computing holidays, %d .. %d"
1870 year (1- (car limits)))
1871 (calc-set-command-flag 'clear-message)
1872 (while (< year (car limits))
1873 (setcar limits (1- (car limits)))
1874 (math-setup-year-holidays (car limits)))
1876 ((> year (cdr limits))
1877 (message "Computing holidays, %d .. %d"
1878 (1+ (cdr limits)) year)
1879 (calc-set-command-flag 'clear-message)
1880 (while (> year (cdr limits))
1881 (setcdr limits (1+ (cdr limits)))
1882 (math-setup-year-holidays (cdr limits)))
1887 (or done (setq math-holidays-cache-tag t))))))
1889 (defun math-setup-year-holidays (math-sh-year)
1890 (let ((exprs (nth 2 math-holidays-cache)))
1892 (let* ((var-y math-sh-year)
1894 (expr (math-evaluate-expr (car exprs))))
1895 (if (math-expr-contains expr '(var m var-m))
1897 (while (<= (setq var-m (1+ var-m)) 12)
1898 (math-setup-add-holidays (math-evaluate-expr expr))))
1899 (math-setup-add-holidays expr)))
1900 (setq exprs (cdr exprs)))))
1902 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
1903 (cond ((eq (car-safe days) 'vec)
1904 (while (setq days (cdr days))
1905 (math-setup-add-holidays (car days))))
1906 ((eq (car-safe days) 'intv)
1907 (let ((day (math-ceiling (nth 2 days))))
1908 (or (eq (calcFunc-in day days) 1)
1909 (setq day (math-add day 1)))
1910 (while (eq (calcFunc-in day days) 1)
1911 (math-setup-add-holidays day)
1912 (setq day (math-add day 1)))))
1913 ((eq (car-safe days) 'date)
1914 (math-setup-add-holidays (nth 1 days)))
1917 (let ((b (math-to-business-day days math-sh-year)))
1918 (or (cdr b) ; don't register holidays twice!
1919 (let ((prev (car math-holidays-cache))
1920 (iprev (nth 1 math-holidays-cache)))
1921 (while (and (cdr prev) (< (nth 1 prev) days))
1922 (setq prev (cdr prev) iprev (cdr iprev)))
1923 (setcdr prev (cons days (cdr prev)))
1924 (setcdr iprev (cons (car b) (cdr iprev)))
1925 (while (setq iprev (cdr iprev))
1926 (setcar iprev (1- (car iprev))))))))
1928 (math-reject-arg (list 'date days) "*Invalid holiday value"))
1930 (math-reject-arg days "*Holiday formula failed to evaluate"))))
1937 ;;; Build a standard deviation form. [X X X]
1938 (defun math-make-sdev (x sigma)
1939 (if (memq (car-safe x) '(date mod sdev intv vec))
1940 (math-reject-arg x 'realp))
1941 (if (memq (car-safe sigma) '(date mod sdev intv vec))
1942 (math-reject-arg sigma 'realp))
1943 (if (or (Math-negp sigma) (memq (car-safe sigma) '(cplx polar)))
1944 (setq sigma (math-abs sigma)))
1945 (if (and (Math-zerop sigma) (Math-scalarp x))
1947 (list 'sdev x sigma)))
1948 (defun calcFunc-sdev (x sigma)
1949 (math-make-sdev x sigma))
1955 (defun math-normalize-mod (a)
1956 (let ((n (math-normalize (nth 1 a)))
1957 (m (math-normalize (nth 2 a))))
1958 (if (and (math-anglep n) (math-anglep m) (math-posp m))
1960 (math-normalize (list 'calcFunc-makemod n m)))))
1962 ;;; Build a modulo form. [N R R]
1963 (defun math-make-mod (n m)
1964 (setq calc-previous-modulo m)
1966 (cond ((not (Math-anglep m))
1967 (math-reject-arg m 'anglep))
1968 ((not (math-posp m))
1969 (math-reject-arg m 'posp))
1971 (if (or (Math-negp n)
1972 (not (Math-lessp n m)))
1973 (list 'mod (math-mod n m) m)
1975 ((memq (car n) '(+ - / vec neg))
1978 (mapcar (function (lambda (x) (math-make-mod x m)))
1980 ((and (eq (car n) '*) (Math-anglep (nth 1 n)))
1981 (math-mul (math-make-mod (nth 1 n) m) (nth 2 n)))
1982 ((memq (car n) '(* ^ var calcFunc-subscr))
1983 (math-mul (math-make-mod 1 m) n))
1984 (t (math-reject-arg n 'anglep)))))
1985 (defun calcFunc-makemod (n m)
1986 (math-make-mod n m))
1990 ;;;; Interval forms.
1992 ;;; Build an interval form. [X S X X]
1993 (defun math-make-intv (mask lo hi)
1994 (if (memq (car-safe lo) '(cplx polar mod sdev intv vec))
1995 (math-reject-arg lo 'realp))
1996 (if (memq (car-safe hi) '(cplx polar mod sdev intv vec))
1997 (math-reject-arg hi 'realp))
1998 (or (eq (eq (car-safe lo) 'date) (eq (car-safe hi) 'date))
1999 (math-reject-arg (if (eq (car-safe lo) 'date) hi lo) 'datep))
2000 (if (and (or (Math-realp lo) (eq (car lo) 'date))
2001 (or (Math-realp hi) (eq (car hi) 'date)))
2002 (let ((cmp (math-compare lo hi)))
2006 (list 'intv mask lo hi))
2009 (list 'intv 2 lo lo)
2010 (list 'intv mask lo lo))
2011 (list 'intv mask lo hi))))
2012 (list 'intv mask lo hi)))
2013 (defun calcFunc-intv (mask lo hi)
2014 (if (math-messy-integerp mask) (setq mask (math-trunc mask)))
2015 (or (natnump mask) (math-reject-arg mask 'fixnatnump))
2016 (or (<= mask 3) (math-reject-arg mask 'range))
2017 (math-make-intv mask lo hi))
2019 (defun math-sort-intv (mask lo hi)
2020 (if (Math-lessp hi lo)
2021 (math-make-intv (aref [0 2 1 3] mask) hi lo)
2022 (math-make-intv mask lo hi)))
2027 (defun math-combine-intervals (a am b bm c cm d dm)
2029 (if (= (setq res (math-compare a c)) 1)
2032 (setq am (or am cm))))
2033 (if (= (setq res (math-compare b d)) -1)
2036 (setq bm (or bm dm))))
2037 (math-make-intv (+ (if am 2 0) (if bm 1 0)) a b)))
2040 (defun math-div-mod (a b m) ; [R R R R] (Returns nil if no solution)
2041 (and (Math-integerp a) (Math-integerp b) (Math-integerp m)
2042 (let ((u1 1) (u3 b) (v1 0) (v3 m))
2043 (while (not (eq v3 0)) ; See Knuth sec 4.5.2, exercise 15
2044 (let* ((q (math-idivmod u3 v3))
2045 (t1 (math-sub u1 (math-mul v1 (car q)))))
2046 (setq u1 v1 u3 v3 v1 t1 v3 (cdr q))))
2047 (let ((q (math-idivmod a u3)))
2049 (math-mod (math-mul (car q) u1) m))))))
2051 (defun math-mod-intv (a b)
2052 (let* ((q1 (math-floor (math-div (nth 2 a) b)))
2053 (q2 (math-floor (math-div (nth 3 a) b)))
2054 (m1 (math-sub (nth 2 a) (math-mul q1 b)))
2055 (m2 (math-sub (nth 3 a) (math-mul q2 b))))
2056 (cond ((equal q1 q2)
2057 (math-sort-intv (nth 1 a) m1 m2))
2058 ((and (math-equal-int (math-sub q2 q1) 1)
2060 (memq (nth 1 a) '(0 2)))
2061 (math-make-intv (nth 1 a) m1 b))
2063 (math-make-intv 2 0 b)))))
2065 ;; The variables math-exp-str and math-exp-pos are local to
2066 ;; math-read-exprs in math-aent.el, but are used by
2067 ;; math-read-angle-brackets, which is called (indirectly) by
2069 (defvar math-exp-str)
2070 (defvar math-exp-pos)
2072 (defun math-read-angle-brackets ()
2073 (let* ((last (or (math-check-for-commas t) (length math-exp-str)))
2074 (str (substring math-exp-str math-exp-pos last))
2076 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str)
2077 (let ((str1 (substring str 0 (1- (match-end 0))))
2078 (str2 (substring str (match-end 0)))
2079 (calc-hashes-used 0))
2080 (setq str1 (math-read-expr (concat "[" str1 "]")))
2081 (if (eq (car-safe str1) 'error)
2083 (setq str2 (math-read-expr str2))
2084 (if (eq (car-safe str2) 'error)
2086 (append '(calcFunc-lambda) (cdr str1) (list str2)))))
2087 (if (string-match "#" str)
2088 (let ((calc-hashes-used 0))
2089 (and (setq str (math-read-expr str))
2090 (if (eq (car-safe str) 'error)
2092 (append '(calcFunc-lambda)
2093 (calc-invent-args calc-hashes-used)
2095 (math-parse-date str)))))
2097 (throw 'syntax res))
2098 (if (eq (car-safe res) 'error)
2099 (throw 'syntax (nth 2 res)))
2100 (setq math-exp-pos (1+ last))
2104 (provide 'calc-forms)
2106 ;;; calc-forms.el ends here