Function: math-to-business-day

math-to-business-day is an autoloaded and byte-compiled function defined in calc-forms.el.gz.

Signature

(math-to-business-day DATE &optional NEED-YEAR)

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calc-forms.el.gz
;;; Compute the number of business days since Jan 1, 1 AD.

(defun math-to-business-day (date &optional need-year)
  (if (eq (car-safe date) 'date)
      (setq date (nth 1 date)))
  (or (Math-realp date)
      (math-reject-arg date 'datep))
  (let* ((day (math-floor date))
	 (time (math-sub date day))
	 (dt (math-date-to-dt day))
	 (delta 0)
	 (holiday nil))
    (or (not need-year) (eq (car dt) need-year)
	(math-reject-arg (list 'date day) "*Generated holiday has wrong year"))
    (math-setup-holidays date)
    (let ((days (car math-holidays-cache)))
      (while (and (setq days (cdr days)) (< (car days) day))
	(setq delta (1+ delta)))
      (and days (= day (car days))
	   (setq holiday t)))
    (let* ((weekdays (nth 3 math-holidays-cache))
           (weeks (/ day 7))
           (wkday (mod day 7)))         ; Day of week: 0=Sunday, 6=Saturday
      (setq delta (+ delta (* weeks (length weekdays))))
      (while (and weekdays (< (car weekdays) wkday))
	(setq weekdays (cdr weekdays)
	      delta (1+ delta)))
      (and weekdays (eq wkday (car weekdays))
	   (setq holiday t)))
    (let ((hours (nth 7 math-holidays-cache)))
      (if hours
	  (progn
	    (setq time (math-div (math-sub time (car hours)) (cdr hours)))
	    (if (Math-lessp time 0) (setq time 0))
	    (or (Math-lessp time 1)
		(setq time
		      (math-sub 1
				(math-div 1 (math-mul 86400 (cdr hours)))))))))
    (cons (math-add (math-sub day delta) time) holiday)))