1 ;;; calc-forms.el --- data format conversion functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;; Declare functions which are defined elsewhere.
32 (declare-function calendar-current-time-zone "cal-dst" ())
33 (declare-function calendar-absolute-from-gregorian "calendar" (date))
34 (declare-function dst-in-effect "cal-dst" (date))
40 (let ((time (current-time-string)))
41 (calc-enter-result 0 "time"
44 (string-to-number (substring time 11 13))
45 (string-to-number (substring time 14 16))
46 (string-to-number (substring time 17 19)))
47 (list 'hms 24 0 0))))))
49 (defun calc-to-hms (arg)
53 (if (eq calc-angle-mode 'rad)
54 (calc-unary-op ">rad" 'calcFunc-rad arg)
55 (calc-unary-op ">deg" 'calcFunc-deg arg))
56 (calc-unary-op ">hms" 'calcFunc-hms arg))))
58 (defun calc-from-hms (arg)
64 (defun calc-hms-notation (fmt)
65 (interactive "sHours-minutes-seconds format (hms, @ \\=' \", etc.): ")
67 (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt)
69 (calc-change-mode 'calc-hms-format
70 (concat "%s" (math-match-substring fmt 1)
71 (math-match-substring fmt 2)
72 "%s" (math-match-substring fmt 3)
73 (math-match-substring fmt 4)
74 "%s" (math-match-substring fmt 5))
76 (setq-default calc-hms-format calc-hms-format)) ; for minibuffer
77 (error "Bad hours-minutes-seconds format"))))
79 (defun calc-date-notation (fmt arg)
80 (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP")
82 (if (string-match-p "\\`\\s-*\\'" fmt)
84 (if (string-match "\\` *\\([0-9]\\|10\\|11\\) *\\'" fmt)
85 (setq fmt (nth (string-to-number fmt) calc-standard-date-formats)))
86 (or (string-match "[a-zA-Z]" fmt)
87 (error "Bad date format specifier"))
89 (>= (setq arg (prefix-numeric-value arg)) 0)
91 (setq calc-standard-date-formats
92 (copy-sequence calc-standard-date-formats))
93 (setcar (nthcdr arg calc-standard-date-formats) fmt))
94 (let ((case-fold-search nil))
95 (and (not (string-match "<.*>" fmt))
96 ;; Find time part to put in <...>
97 (string-match "\\`[^hHspPT]*\\([^ac-gi-lnoqrt-zAC-GI-OQRU-Z]*\\(bs\\|bm\\|bh\\|BS\\|BH\\|[hHmpPsST]\\)+[^ac-gi-lnoqrt-zAC-GI-OQRU-Z]*\\)[^hHspPT]*\\'" fmt)
98 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
99 (regexp-quote (math-match-substring fmt 1))
100 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt)
101 (setq fmt (concat (substring fmt 0 (match-beginning 0))
103 (substring fmt (match-beginning 0) (match-end 0))
105 (substring fmt (match-end 0))))))
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]*[bBZI][a-zA-Z]" fmt)
129 (setq pos2 (1+ pos2)))
130 (while (and (< pos2 (length fmt))
131 (= (upcase (aref fmt pos2))
132 (upcase (aref fmt (1- pos2)))))
133 (setq pos2 (1+ pos2)))
134 (setq sym (intern (substring fmt pos pos2)))
135 (or (memq sym '(Y YY BY YYY YYYY
137 aa AA aaa AAA aaaa AAAA
138 bb BB bbb BBB bbbb BBBB
139 M MM BM mmm Mmm Mmmm MMM MMMM
141 W www Www Wwww WWW WWWW
144 m mm bm s ss bs SS BS C
146 (and (eq sym 'X) (not lfmt) (not fullfmt))
147 (error "Bad format code: %s" sym))
148 (and (memq sym '(bb BB bbb BBB bbbb BBBB))
149 (setq lfmt (cons 'b lfmt)))
150 (setq lfmt (cons sym lfmt))))
151 (setq fmt (substring fmt pos2)))
153 (setq lfmt (cons fmt lfmt)))
155 (setq fullfmt (cons (nreverse lfmt) fullfmt))
156 (setq fullfmt (nconc lfmt fullfmt))))
157 (calc-change-mode 'calc-date-format (nreverse fullfmt) t))))
160 (defun calc-hms-mode ()
163 (calc-change-mode 'calc-angle-mode 'hms)
164 (message "Angles measured in degrees-minutes-seconds")))
167 (defun calc-now (arg)
169 (calc-date-zero-args "now" 'calcFunc-now arg))
171 (defun calc-date-part (arg)
172 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
173 (if (or (< arg 1) (> arg 9))
174 (error "Part code out of range"))
177 (nth arg '(nil "year" "mnth" "day" "hour" "minu"
178 "sec" "wday" "yday" "hmst"))
179 (list (nth arg '(nil calcFunc-year calcFunc-month
180 calcFunc-day calcFunc-hour
181 calcFunc-minute calcFunc-second
182 calcFunc-weekday calcFunc-yearday
186 (defun calc-date (arg)
188 (if (or (< arg 1) (> arg 6))
189 (error "Between one and six arguments are allowed"))
191 (calc-enter-result arg "date" (cons 'calcFunc-date (calc-top-list-n arg)))))
193 (defun calc-julian (arg)
195 (calc-date-one-arg "juln" 'calcFunc-julian arg))
197 (defun calc-unix-time (arg)
199 (calc-date-one-arg "unix" 'calcFunc-unixtime arg))
201 (defun calc-time-zone (arg)
203 (calc-date-zero-args "zone" 'calcFunc-tzone arg))
205 (defun calc-convert-time-zones (old &optional new)
206 (interactive "sFrom time zone: ")
209 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv (calc-top-list-n 3)))
210 (if (equal old "") (setq old "local"))
212 (setq new (read-string (concat "From time zone: " old
214 (if (stringp old) (setq old (math-read-expr old)))
215 (if (eq (car-safe old) 'error)
216 (error "Error in expression: %S" (nth 1 old)))
217 (if (equal new "") (setq new "local"))
218 (if (stringp new) (setq new (math-read-expr new)))
219 (if (eq (car-safe new) 'error)
220 (error "Error in expression: %S" (nth 1 new)))
221 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
222 (calc-top-n 1) old new)))))
224 (defun calc-new-week (arg)
226 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg))
228 (defun calc-new-month (arg)
230 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg))
232 (defun calc-new-year (arg)
234 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg))
236 (defun calc-inc-month (arg)
238 (calc-date-one-arg "incm" 'calcFunc-incmonth arg))
240 (defun calc-business-days-plus (arg)
243 (calc-binary-op "bus+" 'calcFunc-badd arg)))
245 (defun calc-business-days-minus (arg)
248 (calc-binary-op "bus-" 'calcFunc-bsub arg)))
250 (defun calc-date-zero-args (prefix func arg)
253 (calc-enter-result 1 prefix (list func (calc-top-n 1)))
254 (calc-enter-result 0 prefix (if arg
255 (list func (prefix-numeric-value arg))
258 (defun calc-date-one-arg (prefix func arg)
261 (calc-enter-result 2 prefix (cons func (calc-top-list-n 2)))
262 (calc-enter-result 1 prefix (if arg
263 (list func (calc-top-n 1)
264 (prefix-numeric-value arg))
265 (list func (calc-top-n 1)))))))
268 ;;;; Hours-minutes-seconds forms.
270 (defun math-normalize-hms (a)
271 (let ((h (math-normalize (nth 1 a)))
272 (m (math-normalize (nth 2 a)))
273 (s (let ((calc-internal-prec (max (- calc-internal-prec 4) 3)))
274 (math-normalize (nth 3 a)))))
277 (and (= h 0) (math-negp m))
278 (and (= h 0) (= m 0) (math-negp s)))
281 (setq s (math-add s -60)
284 (setq m (math-add m -60)
286 (if (not (Math-lessp -60 s))
287 (setq s (math-add s 60)
289 (if (not (Math-lessp -60 m))
290 (setq m (math-add m 60)
293 (setq s (math-add s 60)
296 (setq m (math-add m 60)
298 (if (not (Math-lessp s 60))
299 (setq s (math-add s -60)
301 (if (not (Math-lessp m 60))
302 (setq m (math-add m -60)
304 (if (and (eq (car-safe s) 'float)
305 (<= (+ (math-numdigs (nth 1 s)) (nth 2 s))
306 (- 2 calc-internal-prec)))
310 ;;; Convert A from ANG or current angular mode to HMS format.
311 (defun math-to-hms (a &optional ang) ; [X R] [Public]
312 (cond ((eq (car-safe a) 'hms) a)
313 ((eq (car-safe a) 'sdev)
314 (math-make-sdev (math-to-hms (nth 1 a))
315 (math-to-hms (nth 2 a))))
316 ((not (Math-numberp a))
317 (list 'calcFunc-hms a))
319 (math-neg (math-to-hms (math-neg a) ang)))
320 ((eq (or ang calc-angle-mode) 'rad)
321 (math-to-hms (math-div a (math-pi-over-180)) 'deg))
322 ((memq (car-safe a) '(cplx polar)) a)
324 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
325 ; (math-normalize a)))
327 (let* ((b (math-mul a 3600))
328 (hm (math-trunc (math-div b 60)))
329 (hmd (math-idivmod hm 60)))
333 (math-sub b (math-mul hm 60))))))))
334 (defun calcFunc-hms (h &optional m s)
335 (or (Math-realp h) (math-reject-arg h 'realp))
337 (or (Math-realp m) (math-reject-arg m 'realp))
339 (or (Math-realp s) (math-reject-arg s 'realp))
340 (if (and (not (Math-lessp m 0)) (Math-lessp m 60)
341 (not (Math-lessp s 0)) (Math-lessp s 60))
342 (math-add (math-to-hms h)
344 (math-to-hms (math-add h
345 (math-add (math-div (or m 0) 60)
346 (math-div (or s 0) 3600)))
349 ;;; Convert A from HMS format to ANG or current angular mode.
350 (defun math-from-hms (a &optional ang) ; [R X] [Public]
351 (cond ((not (eq (car-safe a) 'hms))
354 (if (eq (car-safe a) 'sdev)
355 (math-make-sdev (math-from-hms (nth 1 a) ang)
356 (math-from-hms (nth 2 a) ang))
357 (if (eq (or ang calc-angle-mode) 'rad)
358 (list 'calcFunc-rad a)
359 (list 'calcFunc-deg a)))))
361 (math-neg (math-from-hms (math-neg a) ang)))
362 ((eq (or ang calc-angle-mode) 'rad)
363 (math-mul (math-from-hms a 'deg) (math-pi-over-180)))
365 (math-add (math-div (math-add (math-div (nth 3 a)
374 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
375 ;;; These versions are rewritten to use arbitrary-size integers.
377 ;;; A numerical date is the number of days since midnight on
378 ;;; the morning of December 31, 1 B.C. (Gregorian) or January 2, 1 A.D. (Julian).
379 ;;; Emacs's calendar refers to such a date as an absolute date, some Calc function
380 ;;; names also use that terminology. If the date is a non-integer, it represents
381 ;;; a specific date and time.
382 ;;; A "dt" is a list of the form, (year month day), corresponding to
383 ;;; an integer code, or (year month day hour minute second), corresponding
384 ;;; to a non-integer code.
386 (defun math-date-to-gregorian-dt (date)
387 "Return the day (YEAR MONTH DAY) in the Gregorian calendar.
388 DATE is the number of days since December 31, -1 in the Gregorian calendar."
391 (year (math-quotient (math-add date (if (Math-lessp date 711859)
392 365 ; for speed, we take
393 -108)) ; >1950 as a special case
394 (if (math-negp date) 366 365)))
395 ; this result may be an overestimate
397 (while (Math-lessp date (setq temp (math-absolute-from-gregorian-dt year 1 1)))
398 (setq year (math-add year -1)))
399 (if (eq year 0) (setq year -1))
400 (setq date (1+ (math-sub date temp)))
402 (if (math-leap-year-p year)
403 [1 32 61 92 122 153 183 214 245 275 306 336 999]
404 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
405 (while (>= date (aref temp month))
406 (setq month (1+ month)))
407 (setq day (1+ (- date (aref temp (1- month)))))
408 (list year month day)))
410 (defun math-date-to-julian-dt (date)
411 "Return the day (YEAR MONTH DAY) in the Julian calendar.
412 DATE is the number of days since December 31, -1 in the Gregorian calendar."
415 (year (math-quotient (math-add date (if (Math-lessp date 711859)
416 367 ; for speed, we take
417 -106)) ; >1950 as a special case
418 (if (math-negp date) 366 365)))
419 ; this result may be an overestimate
421 (while (Math-lessp date (setq temp (math-absolute-from-julian-dt year 1 1)))
422 (setq year (math-add year -1)))
423 (if (eq year 0) (setq year -1))
424 (setq date (1+ (math-sub date temp)))
426 (if (math-leap-year-p year t)
427 [1 32 61 92 122 153 183 214 245 275 306 336 999]
428 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
429 (while (>= date (aref temp month))
430 (setq month (1+ month)))
431 (setq day (1+ (- date (aref temp (1- month)))))
432 (list year month day)))
434 (defun math-date-to-dt (value)
435 "Return the day and time of VALUE.
436 The integer part of VALUE is the number of days since Dec 31, -1
437 in the Gregorian calendar and the remaining part determines the time."
438 (if (eq (car-safe value) 'date)
439 (setq value (nth 1 value)))
440 (or (math-realp value)
441 (math-reject-arg value 'datep))
442 (let* ((parts (math-date-parts value))
445 (dt (if (and calc-gregorian-switch
448 (nth 3 calc-gregorian-switch)
449 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))
451 (math-date-to-julian-dt date)
452 (math-date-to-gregorian-dt date))))
453 (if (math-integerp value)
459 (math-add (% time 60) (nth 2 parts)))))))
461 (defun math-date-to-iso-dt (date)
462 "Return the ISO8601 date (year week day) of DATE."
463 (unless (Math-integerp date)
464 (setq date (math-floor date)))
465 (let* ((approx (nth 0 (math-date-to-gregorian-dt (math-sub date 3))))
466 (year (math-add approx
469 (while (>= (math-compare date
470 (math-absolute-from-iso-dt (setq y (math-add y 1)) 1 1)) 0)
471 (setq sum (+ sum 1)))
475 (math-add (car (math-idivmod
476 (math-sub date (math-absolute-from-iso-dt year 1 1))
479 (let ((day (calcFunc-mod date 7)))
480 (if (= day 0) 7 day)))))
482 (defun math-dt-to-date (dt)
483 (or (integerp (nth 1 dt))
484 (math-reject-arg (nth 1 dt) 'fixnump))
485 (if (or (< (nth 1 dt) 1) (> (nth 1 dt) 12))
486 (math-reject-arg (nth 1 dt) "Month value is out of range"))
487 (or (integerp (nth 2 dt))
488 (math-reject-arg (nth 2 dt) 'fixnump))
489 (if (or (< (nth 2 dt) 1) (> (nth 2 dt) 31))
490 (math-reject-arg (nth 2 dt) "Day value is out of range"))
491 (let ((date (math-absolute-from-dt (car dt) (nth 1 dt) (nth 2 dt))))
493 (math-add (math-float date)
494 (math-div (math-add (+ (* (nth 3 dt) 3600)
500 (defun math-iso-dt-to-date (dt)
501 (let ((date (math-absolute-from-iso-dt (car dt) (nth 1 dt) (nth 2 dt))))
503 (math-add (math-float date)
504 (math-div (math-add (+ (* (nth 3 dt) 3600)
510 (defun math-date-parts (value &optional offset)
511 (let* ((date (math-floor value))
512 (time (math-round (math-mul (math-sub value (or offset date)) 86400)
513 (and (> calc-internal-prec 12)
514 (- calc-internal-prec 12))))
515 (ftime (math-floor time)))
518 (math-sub time ftime))))
521 (defun math-this-year ()
522 (nth 5 (decode-time)))
524 (defun math-leap-year-p (year &optional julian)
525 "Non-nil if YEAR is a leap year.
526 If JULIAN is non-nil, then use the criterion for leap years
527 in the Julian calendar, otherwise use the criterion in the
531 (= (math-imod (math-neg year) 4) 1)
532 (= (math-imod year 4) 0))
534 (setq year (math-sub -1 year)))
535 (setq year (math-imod year 400))
536 (or (and (= (% year 4) 0) (/= (% year 100) 0))
539 (defun math-days-in-month (year month)
540 (if (and (= month 2) (math-leap-year-p year))
542 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
544 (defun math-day-in-year (year month day &optional julian)
545 "Return the number of days of the year up to YEAR MONTH DAY.
546 The count includes the given date.
547 If JULIAN is non-nil, use the Julian calendar, otherwise
548 use the Gregorian calendar."
549 (let ((day-of-year (+ day (* 31 (1- month)))))
552 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
553 (if (math-leap-year-p year julian)
554 (setq day-of-year (1+ day-of-year)))))
557 (defun math-day-number (year month day)
558 "Return the number of days of the year up to YEAR MONTH DAY.
559 The count includes the given date."
560 (if calc-gregorian-switch
561 (cond ((eq year (nth 0 calc-gregorian-switch))
563 (- (math-absolute-from-dt year month day)
564 (math-absolute-from-dt year 1 1))))
565 ((Math-lessp year (nth 0 calc-gregorian-switch))
566 (math-day-in-year year month day t))
568 (math-day-in-year year month day)))
569 (math-day-in-year year month day)))
571 (defun math-dt-before-p (dt1 dt2)
572 "Non-nil if DT1 occurs before DT2.
573 A DT is a list of the form (YEAR MONTH DAY)."
574 (or (Math-lessp (nth 0 dt1) (nth 0 dt2))
575 (and (equal (nth 0 dt1) (nth 0 dt2))
576 (or (< (nth 1 dt1) (nth 1 dt2))
577 (and (= (nth 1 dt1) (nth 1 dt2))
578 (< (nth 2 dt1) (nth 2 dt2)))))))
580 (defun math-absolute-from-gregorian-dt (year month day)
581 "Return the DATE of the day given by the Gregorian day YEAR MONTH DAY.
582 Recall that DATE is the number of days since December 31, -1
583 in the Gregorian calendar."
584 (if (eq year 0) (setq year -1))
585 (let ((yearm1 (math-sub year 1)))
587 ;; Add the number of days of the year and the numbers of days
588 ;; in the previous years (leap year days to be added separately)
589 (math-add (math-day-in-year year month day)
590 (math-add (math-mul 365 yearm1)
591 ;; Add the number of Julian leap years
593 (math-quotient yearm1 4)
595 (math-quotient (math-sub 3 year)
597 ;; Subtract the number of Julian leap years which are not
598 ;; Gregorian leap years. In C=4N+r centuries, there will
599 ;; be 3N+r of these days. The following will compute
601 (let* ((correction (math-mul (math-quotient yearm1 100) 3))
602 (res (math-idivmod correction 4)))
603 (math-add (if (= (cdr res) 0)
608 (defun math-absolute-from-julian-dt (year month day)
609 "Return the DATE of the day given by the Julian day YEAR MONTH DAY.
610 Recall that DATE is the number of days since December 31, -1
611 in the Gregorian calendar."
612 (if (eq year 0) (setq year -1))
613 (let ((yearm1 (math-sub year 1)))
615 ;; Add the number of days of the year and the numbers of days
616 ;; in the previous years (leap year days to be added separately)
617 (math-add (math-day-in-year year month day)
618 (math-add (math-mul 365 yearm1)
619 ;; Add the number of Julian leap years
621 (math-quotient yearm1 4)
623 (math-quotient (math-sub 3 year)
625 ;; Adjustment, since January 1, 1 (Julian) is absolute day -1
628 ;; calc-gregorian-switch is a customizable variable defined in calc.el
629 (defvar calc-gregorian-switch)
631 (defun math-absolute-from-iso-dt (year week day)
632 "Return the DATE of the day given by the iso8601 day YEAR WEEK DAY."
633 (let* ((janfour (math-absolute-from-gregorian-dt year 1 4))
634 (prevmon (math-sub janfour
635 (cdr (math-idivmod (math-sub janfour 1) 7)))))
637 (math-add prevmon (* (1- week) 7))
638 (if (zerop day) 6 (1- day)))))
640 (defun math-absolute-from-dt (year month day)
641 "Return the DATE of the day given by the day YEAR MONTH DAY.
642 Recall that DATE is the number of days since December 31, -1
643 in the Gregorian calendar."
644 (if (and calc-gregorian-switch
645 ;; The next few lines determine if the given date
646 ;; occurs before the switch to the Gregorian calendar.
647 (math-dt-before-p (list year month day) calc-gregorian-switch))
648 (math-absolute-from-julian-dt year month day)
649 (math-absolute-from-gregorian-dt year month day)))
651 ;;; It is safe to redefine these in your init file to use a different
654 (defvar math-long-weekday-names '( "Sunday" "Monday" "Tuesday" "Wednesday"
655 "Thursday" "Friday" "Saturday" ))
656 (defvar math-short-weekday-names '( "Sun" "Mon" "Tue" "Wed"
659 (defvar math-long-month-names '( "January" "February" "March" "April"
660 "May" "June" "July" "August"
661 "September" "October" "November" "December" ))
662 (defvar math-short-month-names '( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
663 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
666 (defvar math-format-date-cache nil)
668 ;; The variables math-fd-date, math-fd-dt, math-fd-year,
669 ;; math-fd-month, math-fd-day, math-fd-weekday, math-fd-hour,
670 ;; math-fd-minute, math-fd-second, math-fd-bc-flag are local
671 ;; to math-format-date, but are used by math-format-date-part,
672 ;; which is called by math-format-date.
673 (defvar math-fd-date)
675 (defvar math-fd-year)
676 (defvar math-fd-month)
678 (defvar math-fd-weekday)
679 (defvar math-fd-hour)
680 (defvar math-fd-minute)
681 (defvar math-fd-second)
682 (defvar math-fd-bc-flag)
683 (defvar math-fd-iso-dt)
684 (defvar math-fd-isoyear)
685 (defvar math-fd-isoweek)
686 (defvar math-fd-isoweekday)
688 (defun math-format-date (math-fd-date)
689 (if (eq (car-safe math-fd-date) 'date)
690 (setq math-fd-date (nth 1 math-fd-date)))
691 (let ((entry (list math-fd-date calc-internal-prec calc-date-format)))
692 (or (cdr (assoc entry math-format-date-cache))
693 (let* ((math-fd-dt nil)
695 (calc-group-digits nil)
696 (calc-leading-zeros nil)
697 (calc-number-radix 10)
698 (calc-twos-complement-mode nil)
699 math-fd-year math-fd-month math-fd-day math-fd-weekday
700 math-fd-hour math-fd-minute math-fd-second
701 math-fd-isoyear math-fd-isoweek math-fd-isoweekday
702 (math-fd-bc-flag nil)
703 (fmt (apply 'concat (mapcar 'math-format-date-part
705 (setq math-format-date-cache (cons (cons entry fmt)
706 math-format-date-cache))
707 (and (setq math-fd-dt (nthcdr 10 math-format-date-cache))
708 (setcdr math-fd-dt nil))
711 (defconst math-julian-date-beginning '(float 17214225 -1)
712 "The beginning of the Julian date calendar,
713 as measured in the number of days before December 31, 1 BC (Gregorian).")
715 (defconst math-julian-date-beginning-int 1721423
716 "The beginning of the Julian date calendar,
717 as measured in the integer number of days before December 31, 1 BC (Gregorian).")
719 (defun math-format-date-part (x)
723 (if (math-integerp math-fd-date)
725 (apply 'concat (mapcar 'math-format-date-part x))))
729 (math-format-number math-fd-date))
731 (math-format-number (math-floor math-fd-date)))
734 (math-add math-fd-date math-julian-date-beginning)))
736 (math-format-number (math-add
737 (math-floor math-fd-date)
738 math-julian-date-beginning-int)))
740 (math-format-number (nth 1 (math-date-parts math-fd-date 719164))))
741 ((memq x '(IYYY Iww w))
744 (setq math-fd-iso-dt (math-date-to-iso-dt math-fd-date)
745 math-fd-isoyear (car math-fd-iso-dt)
746 math-fd-isoweek (nth 1 math-fd-iso-dt)
747 math-fd-isoweekday (nth 2 math-fd-iso-dt)))
749 (let* ((neg (Math-negp math-fd-isoyear))
750 (pyear (calcFunc-abs math-fd-isoyear)))
751 (if (and (natnump pyear) (< pyear 10000))
752 (concat (if neg "-" "") (format "%04d" pyear))
753 (concat (if neg "-" "+") (math-format-number pyear)))))
755 (concat "W" (format "%02d" math-fd-isoweek)))
757 (format "%d" math-fd-isoweekday)))))
761 (setq math-fd-dt (math-date-to-dt math-fd-date)
762 math-fd-year (car math-fd-dt)
763 math-fd-month (nth 1 math-fd-dt)
764 math-fd-day (nth 2 math-fd-dt)
765 math-fd-weekday (math-mod (math-floor math-fd-date) 7)
766 math-fd-hour (nth 3 math-fd-dt)
767 math-fd-minute (nth 4 math-fd-dt)
768 math-fd-second (nth 5 math-fd-dt))
769 (and (memq 'b calc-date-format)
770 (math-negp math-fd-year)
771 (setq math-fd-year (math-neg math-fd-year)
772 math-fd-bc-flag t))))
774 (if (and (integerp math-fd-year) (> math-fd-year 1940) (< math-fd-year 2040))
775 (format (cond ((eq x 'YY) "%02d")
778 (% math-fd-year 100))
779 (if (and (natnump math-fd-year) (< math-fd-year 100))
780 (format "+%d" math-fd-year)
781 (math-format-number math-fd-year))))
783 (math-format-number math-fd-year))
785 (if (and (natnump math-fd-year) (< math-fd-year 100))
786 (format "+%d" math-fd-year)
787 (math-format-number math-fd-year)))
789 (let* ((year (if (Math-negp math-fd-year)
790 (math-add math-fd-year 1)
792 (neg (Math-negp year))
793 (pyear (calcFunc-abs year)))
794 (if (and (natnump pyear) (< pyear 10000))
795 (concat (if neg "-" "") (format "%04d" pyear))
796 (concat (if neg "-" "+") (math-format-number pyear)))))
799 (and (not math-fd-bc-flag) "ad"))
801 (and (not math-fd-bc-flag) "AD"))
803 (and (not math-fd-bc-flag) "ad "))
805 (and (not math-fd-bc-flag) "AD "))
807 (and (not math-fd-bc-flag) "a.d."))
809 (and (not math-fd-bc-flag) "A.D."))
811 (and math-fd-bc-flag "bc"))
813 (and math-fd-bc-flag "BC"))
815 (and math-fd-bc-flag " bc"))
817 (and math-fd-bc-flag " BC"))
819 (and math-fd-bc-flag "b.c."))
821 (and math-fd-bc-flag "B.C."))
824 (format "%d" math-fd-month))
826 (format "%02d" math-fd-month))
828 (format "%2d" math-fd-month))
830 (downcase (nth (1- math-fd-month) math-short-month-names)))
832 (nth (1- math-fd-month) math-short-month-names))
834 (upcase (nth (1- math-fd-month) math-short-month-names)))
836 (nth (1- math-fd-month) math-long-month-names))
838 (upcase (nth (1- math-fd-month) math-long-month-names)))
840 (format "%d" math-fd-day))
842 (format "%02d" math-fd-day))
844 (format "%2d" math-fd-day))
846 (format "%d" math-fd-weekday))
848 (downcase (nth math-fd-weekday math-short-weekday-names)))
850 (nth math-fd-weekday math-short-weekday-names))
852 (upcase (nth math-fd-weekday math-short-weekday-names)))
854 (nth math-fd-weekday math-long-weekday-names))
856 (upcase (nth math-fd-weekday math-long-weekday-names)))
858 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day)))
860 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day)))
862 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day)))
864 (and math-fd-hour (format "%d" math-fd-hour)))
866 (and math-fd-hour (format "%02d" math-fd-hour)))
868 (and math-fd-hour (format "%2d" math-fd-hour)))
870 (and math-fd-hour (format "%d" (1+ (% (+ math-fd-hour 11) 12)))))
872 (and math-fd-hour (format "%02d" (1+ (% (+ math-fd-hour 11) 12)))))
874 (and math-fd-hour (format "%2d" (1+ (% (+ math-fd-hour 11) 12)))))
876 (and math-fd-hour (if (< math-fd-hour 12) "a" "p")))
878 (and math-fd-hour (if (< math-fd-hour 12) "A" "P")))
880 (and math-fd-hour (if (< math-fd-hour 12) "am" "pm")))
882 (and math-fd-hour (if (< math-fd-hour 12) "AM" "PM")))
884 (and math-fd-hour (if (< math-fd-hour 12) "a.m." "p.m.")))
886 (and math-fd-hour (if (< math-fd-hour 12) "A.M." "P.M.")))
888 (and math-fd-minute (format "%d" math-fd-minute)))
890 (and math-fd-minute (format "%02d" math-fd-minute)))
892 (and math-fd-minute (format "%2d" math-fd-minute)))
894 (and math-fd-second (not (math-zerop math-fd-second))
896 ((memq x '(s ss bs SS BS))
898 (not (and (memq x '(SS BS)) (math-zerop math-fd-second)))
899 (if (integerp math-fd-second)
900 (format (cond ((memq x '(ss SS)) "%02d")
901 ((memq x '(bs BS)) "%2d")
904 (concat (if (Math-lessp math-fd-second 10)
905 (cond ((memq x '(ss SS)) "0")
906 ((memq x '(bs BS)) " ")
909 (let ((calc-float-format
910 (list 'fix (min (- 12 calc-internal-prec)
912 (math-format-number math-fd-second))))))))
914 ;; The variable math-pd-str is local to math-parse-date and
915 ;; math-parse-standard-date, but is used by math-parse-date-word,
916 ;; which is called by math-parse-date and math-parse-standard-date.
919 (defun math-parse-date (math-pd-str)
921 (or (math-parse-standard-date math-pd-str t)
922 (math-parse-standard-date math-pd-str nil)
923 (and (string-match "W[0-9][0-9]" math-pd-str)
924 (math-parse-iso-date math-pd-str))
925 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str)
926 (list 'date (math-read-number (math-match-substring math-pd-str 1))))
927 (let ((case-fold-search t)
928 (year nil) (month nil) (day nil) (weekday nil)
929 (hour nil) (minute nil) (second nil) (bc-flag nil)
930 (a nil) (b nil) (c nil) (bigyear nil) temp)
932 ;; Extract the time, if any.
933 (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)
934 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str))
935 (let ((ampm (math-match-substring math-pd-str 6)))
936 (setq hour (string-to-number (math-match-substring math-pd-str 1))
937 minute (math-match-substring math-pd-str 2)
938 second (math-match-substring math-pd-str 4)
939 math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
940 (substring math-pd-str (match-end 0))))
941 (if (equal minute "")
943 (setq minute (string-to-number minute)))
944 (if (equal second "")
946 (setq second (math-read-number second)))
952 (not (eq second 0))))
953 (throw 'syntax "Hour value is out of range"))
954 (setq ampm (upcase (aref ampm 0)))
955 (if (memq ampm '(?N ?M))
956 (if (and (= hour 12) (= minute 0) (eq second 0))
957 (if (eq ampm ?M) (setq hour 0))
959 "Time must be 12:00:00 in this context"))
960 (if (or (= hour 0) (> hour 12))
961 (throw 'syntax "Hour value is out of range"))
962 (if (eq (= ampm ?A) (= hour 12))
963 (setq hour (% (+ hour 12) 24)))))))
965 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
966 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str)
968 (setq math-pd-str (copy-sequence math-pd-str))
969 (aset math-pd-str (match-beginning 1) ?\/)))
971 ;; Extract obvious month or weekday names.
972 (if (string-match "[a-zA-Z]" math-pd-str)
974 (setq month (math-parse-date-word math-long-month-names))
975 (setq weekday (math-parse-date-word math-long-weekday-names))
976 (or month (setq month
977 (math-parse-date-word math-short-month-names)))
978 (or weekday (math-parse-date-word math-short-weekday-names))
980 (if (setq temp (math-parse-date-word
981 '( "noon" "midnight" "mid" )))
982 (setq hour (if (= temp 1) 12 0) minute 0 second 0)))
983 (or (math-parse-date-word '( "ad" "a.d." ))
984 (if (math-parse-date-word '( "bc" "b.c." ))
986 (if (string-match "[a-zA-Z]+" math-pd-str)
987 (throw 'syntax (format "Bad word in date: \"%s\""
988 (math-match-substring math-pd-str 0))))))
990 ;; If there is a huge number other than the year, ignore it.
991 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str)
992 (setq temp (concat (substring math-pd-str 0 (match-beginning 0))
993 (substring math-pd-str (match-end 0))))
995 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp))
996 (setq math-pd-str temp))
998 ;; If there is a number with a sign or a large number, it is a year.
999 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str)
1000 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str))
1001 (setq year (math-match-substring math-pd-str 1)
1002 math-pd-str (concat (substring math-pd-str 0 (match-beginning 1))
1003 (substring math-pd-str (match-end 1)))
1004 year (math-read-number year)
1007 ;; Collect remaining numbers.
1009 (while (string-match "[0-9]+" math-pd-str temp)
1010 (and c (throw 'syntax "Too many numbers in date"))
1011 (setq c (string-to-number (math-match-substring math-pd-str 0)))
1012 (or b (setq b c c nil))
1013 (or a (setq a b b nil))
1014 (setq temp (match-end 0)))
1016 ;; Check that we have the right amount of information.
1017 (setq temp (+ (if year 1 0) (if month 1 0) (if day 1 0)
1018 (if a 1 0) (if b 1 0) (if c 1 0)))
1020 (throw 'syntax "Too many numbers in date")
1021 (if (or (< temp 2) (and year (= temp 2)))
1022 (throw 'syntax "Not enough numbers in date")
1023 (if (= temp 2) ; if year omitted, assume current year
1024 (setq year (math-this-year)))))
1026 ;; A large number must be a year.
1028 (if (and a (or (> a 31) (< a 1)))
1029 (setq year a a b b c c nil)
1030 (if (and b (or (> b 31) (< b 1)))
1031 (setq year b b c c nil)
1032 (if (and c (or (> c 31) (< c 1)))
1033 (setq year c c nil)))))
1035 ;; A medium-large number must be a day.
1037 (if (and a (> a 12))
1038 (setq day a a b b c c nil)
1039 (if (and b (> b 12))
1040 (setq day b b c c nil)
1041 (if (and c (> c 12))
1042 (setq day c c nil)))))
1044 ;; We may know enough to sort it out now.
1046 (or month (setq month a))
1047 (if (and year month)
1050 ;; Interpret order of numbers as same as for display format.
1051 (setq temp calc-date-format)
1053 (cond ((not (symbolp (car temp))))
1054 ((memq (car temp) '(Y YY BY YYY YYYY))
1055 (or year (setq year a a b b c)))
1056 ((memq (car temp) '(M MM BM mmm Mmm Mmmm MMM MMMM))
1057 (or month (setq month a a b b c)))
1058 ((memq (car temp) '(D DD BD))
1059 (or day (setq day a a b b c))))
1060 (setq temp (cdr temp)))
1062 ;; If display format was not complete, assume American style.
1063 (or month (setq month a a b b c))
1064 (or day (setq day a a b b c))
1065 (or year (setq year a a b b c))))
1068 (setq year (math-neg (math-abs year))))
1070 (math-parse-date-validate year bigyear month day
1071 hour minute second)))))
1073 (defun math-parse-date-validate (year bigyear month day hour minute second)
1074 (and (not bigyear) (natnump year) (< year 100)
1075 (setq year (+ year (if (< year 40) 2000 1900))))
1077 (throw 'syntax "Year value is out of range"))
1078 (if (or (< month 1) (> month 12))
1079 (throw 'syntax "Month value is out of range"))
1080 (if (or (< day 1) (> day (math-days-in-month year month)))
1081 (throw 'syntax "Day value is out of range"))
1088 (not (eq second 0))))
1089 (throw 'syntax "Hour value is out of range"))
1090 (if (or (< minute 0) (> minute 59))
1091 (throw 'syntax "Minute value is out of range"))
1092 (if (or (math-negp second) (not (Math-lessp second 60)))
1093 (throw 'syntax "Seconds value is out of range"))))
1094 (list 'date (math-dt-to-date (append (list year month day)
1095 (and hour (list hour minute second))))))
1097 (defun math-parse-iso-date-validate (isoyear isoweek isoweekday hour minute second)
1098 (if (or (< isoweek 1) (> isoweek 53))
1099 (throw 'syntax "Week value is out of range"))
1100 (if (or (< isoweekday 1) (> isoweekday 7))
1101 (throw 'syntax "Weekday value is out of range"))
1108 (not (eq second 0))))
1109 (throw 'syntax "Hour value is out of range"))
1110 (if (or (< minute 0) (> minute 59))
1111 (throw 'syntax "Minute value is out of range"))
1112 (if (or (math-negp second) (not (Math-lessp second 60)))
1113 (throw 'syntax "Seconds value is out of range"))))
1114 (list 'date (math-iso-dt-to-date (append (list isoyear isoweek isoweekday)
1115 (and hour (list hour minute second))))))
1117 (defun math-parse-date-word (names &optional front)
1119 (while (and names (not (string-match (if (equal (car names) "Sep")
1121 (regexp-quote (car names)))
1123 (setq names (cdr names)
1126 (or (not front) (= (match-beginning 0) 0))
1128 (setq math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
1130 (substring math-pd-str (match-end 0))))
1133 (defun math-parse-standard-date (math-pd-str with-time)
1134 (let ((case-fold-search t)
1136 (fmt calc-date-format) this next (gnext nil)
1137 (isoyear nil) (isoweek nil) (isoweekday nil)
1138 (year nil) (month nil) (day nil) (bigyear nil) (yearday nil)
1139 (hour nil) (minute nil) (second nil) (bc-flag nil))
1140 (while (and fmt okay)
1141 (setq this (car fmt)
1142 fmt (setq fmt (or (cdr fmt)
1147 (if (consp next) (setq next (car next)))
1148 (or (cond ((listp this)
1154 (if (and (<= (length this) (length math-pd-str))
1156 (substring math-pd-str 0 (length this))))
1157 (setq math-pd-str (substring math-pd-str (length this)))))
1160 ((memq this '(n N j J))
1161 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str)
1162 (setq num (math-match-substring math-pd-str 0)
1163 math-pd-str (substring math-pd-str (match-end 0))
1164 num (math-date-to-dt (math-read-number num))
1166 (if (memq this '(n N))
1168 (if (or (eq this 'j)
1169 (math-integerp num))
1170 math-julian-date-beginning-int
1171 math-julian-date-beginning)))
1172 hour (or (nth 3 num) hour)
1173 minute (or (nth 4 num) minute)
1174 second (or (nth 5 num) second)
1179 (and (string-match "\\`[-+]?[0-9]+" math-pd-str)
1180 (setq num (math-match-substring math-pd-str 0)
1181 math-pd-str (substring math-pd-str (match-end 0))
1182 num (math-date-to-dt
1184 (math-div (math-read-number num)
1192 ((memq this '(mmm Mmm MMM))
1193 (setq month (math-parse-date-word math-short-month-names t)))
1194 ((memq this '(Mmmm MMMM))
1195 (setq month (math-parse-date-word math-long-month-names t)))
1196 ((memq this '(www Www WWW))
1197 (math-parse-date-word math-short-weekday-names t))
1198 ((memq this '(Wwww WWWW))
1199 (math-parse-date-word math-long-weekday-names t))
1201 (if (string-match "\\`a" math-pd-str)
1202 (setq hour (if (= hour 12) 0 hour)
1203 math-pd-str (substring math-pd-str 1))
1204 (if (string-match "\\`p" math-pd-str)
1205 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
1206 math-pd-str (substring math-pd-str 1)))))
1207 ((memq this '(pp PP pppp PPPP))
1208 (if (string-match "\\`am\\|a\\.m\\." math-pd-str)
1209 (setq hour (if (= hour 12) 0 hour)
1210 math-pd-str (substring math-pd-str (match-end 0)))
1211 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str)
1212 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
1213 math-pd-str (substring math-pd-str (match-end 0))))))
1214 ((memq this '(Y YY BY YYY YYYY ZYYY))
1215 (and (if (memq next '(MM DD ddd hh HH mm ss SS))
1216 (if (memq this '(Y YY BYY))
1217 (string-match "\\` *[0-9][0-9]" math-pd-str)
1218 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str))
1219 (string-match "\\`[-+]?[0-9]+" math-pd-str))
1220 (setq year (math-match-substring math-pd-str 0)
1221 bigyear (or (eq this 'YYY)
1222 (memq (aref math-pd-str 0) '(?\+ ?\-)))
1223 math-pd-str (substring math-pd-str (match-end 0))
1224 year (math-read-number year))
1225 (if (and (eq this 'ZYYY) (eq year 0))
1226 (setq year (math-sub year 1)
1230 (if (string-match "\\`[-+]?[0-9]+" math-pd-str)
1231 (setq isoyear (string-to-number (math-match-substring math-pd-str 0))
1232 math-pd-str (substring math-pd-str (match-end 0)))))
1234 (if (string-match "W\\([0-9][0-9]\\)" math-pd-str)
1235 (setq isoweek (string-to-number (math-match-substring math-pd-str 1))
1236 math-pd-str (substring math-pd-str 3))))
1240 (if (eq (aref math-pd-str 0) ?T)
1241 (setq math-pd-str (substring math-pd-str 1))
1243 ((memq this '(aa AA aaaa AAAA))
1244 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str)
1245 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1246 ((memq this '(aaa AAA))
1247 (if (string-match "\\` *ad *" math-pd-str)
1248 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1249 ((memq this '(bb BB bbb BBB bbbb BBBB))
1250 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str)
1251 (setq math-pd-str (substring math-pd-str (match-end 0))
1253 ((memq this '(s ss bs SS BS))
1254 (and (if (memq next '(YY YYYY MM DD hh HH mm))
1255 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str)
1256 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str))
1257 (setq second (math-match-substring math-pd-str 0)
1258 math-pd-str (substring math-pd-str (match-end 0))
1259 second (math-read-number second))))
1261 (if (string-match "\\`:[0-9][0-9]" math-pd-str)
1262 (setq math-pd-str (substring math-pd-str 1))
1264 ((or (not (if (and (memq this '(ddd MM DD hh HH mm))
1265 (memq next '(YY YYYY MM DD ddd
1268 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str)
1269 (string-match "\\` *[0-9][0-9]" math-pd-str))
1270 (string-match "\\` *[0-9]+" math-pd-str)))
1271 (and (setq num (string-to-number
1272 (math-match-substring math-pd-str 0))
1273 math-pd-str (substring math-pd-str (match-end 0)))
1277 (and (>= num 0) (< num 7)))
1279 (setq isoweekday num))
1280 ((memq this '(d ddd bdd))
1282 ((memq this '(M MM BM))
1284 ((memq this '(D DD BD))
1286 ((memq this '(h hh bh H HH BH))
1288 ((memq this '(m mm bm))
1294 (setq month 1 day 1)))
1295 (if (and okay (equal math-pd-str ""))
1297 (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second)
1298 (and month day (or (not (or hour minute second))
1301 (or year (setq year (math-this-year)))
1302 (or second (setq second 0))
1304 (setq year (math-neg (math-abs year))))
1305 (setq day (math-parse-date-validate year bigyear month day
1306 hour minute second))
1308 (setq day (math-add day (1- yearday))))
1311 (defun math-parse-iso-date (math-pd-str)
1312 "Parse MATH-PD-STR as an ISO week date, or return nil."
1313 (let ((case-fold-search t)
1314 (isoyear nil) (isoweek nil) (isoweekday nil)
1315 (hour nil) (minute nil) (second nil))
1316 ;; Extract the time, if any.
1317 (if (string-match "T[^0-9]*\\([0-9][0-9]\\)[^0-9]*\\([0-9][0-9]\\)?[^0-9]*\\([0-9][0-9]\\(\\.[0-9]+\\)?\\)?" math-pd-str)
1319 (setq hour (string-to-number (math-match-substring math-pd-str 1))
1320 minute (math-match-substring math-pd-str 2)
1321 second (math-match-substring math-pd-str 3)
1322 math-pd-str (substring math-pd-str 0 (match-beginning 0)))
1323 (if (equal minute "")
1325 (setq minute (string-to-number minute)))
1326 (if (equal second "")
1328 (setq second (math-read-number second)))))
1329 ;; Next, the year, week and weekday
1330 (if (string-match "\\(-?[0-9]*\\)[^0-9]*W\\([0-9][0-9]\\)[^0-9]*\\([0-9]\\)[^0-9]*\\'" math-pd-str)
1332 (setq isoyear (string-to-number (math-match-substring math-pd-str 1))
1333 isoweek (string-to-number (math-match-substring math-pd-str 2))
1334 isoweekday (string-to-number (math-match-substring math-pd-str 3)))
1335 (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second)))))
1337 (defun calcFunc-now (&optional zone)
1338 (let ((date (let ((calc-date-format nil))
1339 (math-parse-date (current-time-string)))))
1342 (math-add date (math-div (math-sub (calcFunc-tzone nil date)
1343 (calcFunc-tzone zone date))
1346 (calc-record-why "*Unable to interpret current date from system")
1347 (append (list 'calcFunc-now) (and zone (list zone))))))
1349 (defun calcFunc-year (date)
1350 (car (math-date-to-dt date)))
1352 (defun calcFunc-month (date)
1353 (nth 1 (math-date-to-dt date)))
1355 (defun calcFunc-day (date)
1356 (nth 2 (math-date-to-dt date)))
1358 (defun calcFunc-weekday (date)
1359 (if (eq (car-safe date) 'date)
1360 (setq date (nth 1 date)))
1361 (or (math-realp date)
1362 (math-reject-arg date 'datep))
1363 (math-mod (math-floor date) 7))
1365 (defun calcFunc-yearday (date)
1366 (let ((dt (math-date-to-dt date)))
1367 (math-day-number (car dt) (nth 1 dt) (nth 2 dt))))
1369 (defun calcFunc-hour (date)
1370 (if (eq (car-safe date) 'hms)
1372 (or (nth 3 (math-date-to-dt date)) 0)))
1374 (defun calcFunc-minute (date)
1375 (if (eq (car-safe date) 'hms)
1377 (or (nth 4 (math-date-to-dt date)) 0)))
1379 (defun calcFunc-second (date)
1380 (if (eq (car-safe date) 'hms)
1382 (or (nth 5 (math-date-to-dt date)) 0)))
1384 (defun calcFunc-time (date)
1385 (let ((dt (math-date-to-dt date)))
1387 (cons 'hms (nthcdr 3 dt))
1388 (list 'hms 0 0 0))))
1390 (defun calcFunc-date (date &optional month day hour minute second)
1391 (and (math-messy-integerp month) (setq month (math-trunc month)))
1392 (and month (not (integerp month)) (math-reject-arg month 'fixnump))
1393 (and (math-messy-integerp day) (setq day (math-trunc day)))
1394 (and day (not (integerp day)) (math-reject-arg day 'fixnump))
1395 (if (and (eq (car-safe hour) 'hms) (not minute))
1396 (setq second (nth 3 hour)
1399 (and (math-messy-integerp hour) (setq hour (math-trunc hour)))
1400 (and hour (not (integerp hour)) (math-reject-arg hour 'fixnump))
1401 (and (math-messy-integerp minute) (setq minute (math-trunc minute)))
1402 (and minute (not (integerp minute)) (math-reject-arg minute 'fixnump))
1403 (and (math-messy-integerp second) (setq second (math-trunc second)))
1404 (and second (not (math-realp second)) (math-reject-arg second 'realp))
1407 (and (math-messy-integerp date) (setq date (math-trunc date)))
1408 (and date (not (math-integerp date)) (math-reject-arg date 'integerp))
1411 (list 'date (math-dt-to-date (list date month day hour
1414 (list 'date (math-dt-to-date (list date month day))))
1415 (list 'date (math-dt-to-date (list (math-this-year) date month)))))
1416 (if (math-realp date)
1418 (if (eq (car date) 'date)
1420 (math-reject-arg date 'datep)))))
1422 (defun calcFunc-julian (date &optional zone)
1423 (if (math-realp date)
1424 (list 'date (if (math-integerp date)
1425 (math-sub date math-julian-date-beginning-int)
1426 (setq date (math-sub date math-julian-date-beginning))
1427 (math-sub date (math-div (calcFunc-tzone zone date)
1429 (if (eq (car date) 'date)
1430 (math-add (nth 1 date) (if (math-integerp (nth 1 date))
1431 math-julian-date-beginning-int
1432 (math-add math-julian-date-beginning
1433 (math-div (calcFunc-tzone zone date)
1435 (math-reject-arg date 'datep))))
1437 (defun calcFunc-unixtime (date &optional zone)
1438 (if (math-realp date)
1440 (setq date (math-add 719163 (math-div date '(float 864 2))))
1441 (list 'date (math-sub date (math-div (calcFunc-tzone zone date)
1443 (if (eq (car date) 'date)
1444 (math-add (nth 1 (math-date-parts (nth 1 date) 719163))
1445 (calcFunc-tzone zone date))
1446 (math-reject-arg date 'datep))))
1449 ;;; Note: Longer names must appear before shorter names which are
1450 ;;; substrings of them.
1451 (defvar math-tzone-names
1453 ( "MEGT" -1 "MET" "METDST" ) ; Middle Europe
1454 ( "METDST" -1 -1 ) ( "MET" -1 0 )
1455 ( "MEGZ" -1 "MEZ" "MESZ" ) ( "MEZ" -1 0 ) ( "MESZ" -1 -1 )
1456 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1457 ( "WETDST" 0 -1 ) ( "WET" 0 0 )
1458 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -1 ) ; Britain
1459 ( "NGT" (float 35 -1) "NST" "NDT" ) ; Newfoundland
1460 ( "NST" (float 35 -1) 0 ) ( "NDT" (float 35 -1) -1 )
1461 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -1 ) ; Atlantic
1462 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -1 ) ; Eastern
1463 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -1 ) ; Central
1464 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -1 ) ; Mountain
1465 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -1 ) ; Pacific
1466 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -1 ) ; Yukon
1468 "No doc yet. See calc manual for now. ")
1470 (defvar var-TimeZone nil)
1473 (defvar calendar-current-time-zone-cache)
1475 (defvar math-calendar-tzinfo
1477 "Information about the timezone, retrieved from the calendar.")
1479 (defun math-get-calendar-tzinfo ()
1480 "Get information about the timezone from the calendar.
1481 The result should be a list of two items about the current time zone:
1482 first, the number of seconds difference from GMT
1483 second, the number of seconds offset for daylight savings."
1484 (if math-calendar-tzinfo
1485 math-calendar-tzinfo
1487 (let ((tzinfo (progn
1488 (calendar-current-time-zone)
1489 calendar-current-time-zone-cache)))
1490 (setq math-calendar-tzinfo
1491 (list (* 60 (abs (nth 0 tzinfo)))
1492 (* 60 (nth 1 tzinfo)))))))
1494 (defun calcFunc-tzone (&optional zone date)
1496 (cond ((math-realp zone)
1497 (math-round (math-mul zone 3600)))
1498 ((eq (car zone) 'hms)
1499 (math-round (math-mul (math-from-hms zone 'deg) 3600)))
1501 (math-add (calcFunc-tzone (nth 1 zone) date)
1502 (calcFunc-tzone (nth 2 zone) date)))
1504 (math-sub (calcFunc-tzone (nth 1 zone) date)
1505 (calcFunc-tzone (nth 2 zone) date)))
1506 ((eq (car zone) 'var)
1507 (let ((name (upcase (symbol-name (nth 1 zone))))
1509 (if (setq found (assoc name math-tzone-names))
1510 (calcFunc-tzone (math-add (nth 1 found)
1511 (if (integerp (nth 2 found))
1514 (math-daylight-savings-adjust
1518 (if (equal name "LOCAL")
1519 (calcFunc-tzone nil date)
1520 (math-reject-arg zone "*Unrecognized time zone name")))))
1521 (t (math-reject-arg zone "*Expected a time zone")))
1522 (if (calc-var-value 'var-TimeZone)
1523 (calcFunc-tzone (calc-var-value 'var-TimeZone) date)
1524 (let ((tzinfo (math-get-calendar-tzinfo)))
1526 (* (math-cal-daylight-savings-adjust date) (nth 1 tzinfo)))))))
1528 (defvar math-daylight-savings-hook 'math-std-daylight-savings)
1530 (defun math-daylight-savings-adjust (date zone &optional dt)
1531 (or date (setq date (nth 1 (calcFunc-now))))
1533 (if (eq (car-safe date) 'date)
1536 (if (and date (math-realp date))
1537 (let ((zadj (assoc zone math-tzone-names)))
1538 (if zadj (setq bump -1
1539 date (math-sub date (math-div (nth 1 zadj)
1541 (math-reject-arg date 'datep)))
1542 (setq date (math-float date))
1543 (or dt (setq dt (math-date-to-dt date)))
1544 (and math-daylight-savings-hook
1545 (funcall math-daylight-savings-hook date dt zone bump))))
1547 ;;; Based on part of dst-adjust-time in cal-dst.el
1548 ;;; For calcFunc-dst, when zone=nil
1549 (defun math-cal-daylight-savings-adjust (date)
1550 "Return -1 if DATE is using daylight saving, 0 otherwise."
1552 (unless date (setq date (calcFunc-now)))
1553 (let* ((dt (math-date-to-dt date))
1558 (+ (nth 3 dt) (/ (nth 4 dt) 60.0)))
1563 (calendar-absolute-from-gregorian
1564 (list (nth 1 dt) (nth 2 dt) (nth 0 dt)))
1565 (/ (round (* 60 time)) 60.0 24.0))))
1566 (if (dst-in-effect rounded-abs-date)
1570 (defun calcFunc-dsadj (date &optional zone)
1572 (or (eq (car-safe zone) 'var)
1573 (math-reject-arg zone "*Time zone variable expected"))
1574 (setq zone (calc-var-value 'var-TimeZone)))
1577 (setq zone (and (eq (car-safe zone) 'var)
1578 (upcase (symbol-name (nth 1 zone)))))
1579 (let ((zadj (assoc zone math-tzone-names)))
1580 (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1581 (if (integerp (nth 2 zadj))
1583 (math-daylight-savings-adjust date zone))))
1584 (math-cal-daylight-savings-adjust date)))
1586 ;; (defun calcFunc-dsadj (date &optional zone)
1588 ;; (or (eq (car-safe zone) 'var)
1589 ;; (math-reject-arg zone "*Time zone variable expected"))
1590 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1593 ;; (calc-var-value 'var-TimeZone)))))
1594 ;; (setq zone (and (eq (car-safe zone) 'var)
1595 ;; (upcase (symbol-name (nth 1 zone)))))
1596 ;; (let ((zadj (assoc zone math-tzone-names)))
1597 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1598 ;; (if (integerp (nth 2 zadj))
1600 ;; (math-daylight-savings-adjust date zone))))
1602 (defun calcFunc-tzconv (date z1 z2)
1603 (if (math-realp date)
1604 (nth 1 (calcFunc-tzconv (list 'date date) z1 z2))
1605 (calcFunc-unixtime (calcFunc-unixtime date z1) z2)))
1607 (defun math-std-daylight-savings (date dt zone bump)
1608 "Standard North American daylight saving algorithm.
1609 Before 2007, this uses `math-std-daylight-savings-old', where
1610 daylight saving began on the first Sunday of April at 2 a.m.,
1611 and ended on the last Sunday of October at 2 a.m.
1612 As of 2007, this uses `math-std-daylight-savings-new', where
1613 daylight saving begins on the second Sunday of March at 2 a.m.,
1614 and ends on the first Sunday of November at 2 a.m."
1615 (if (< (car dt) 2007)
1616 (math-std-daylight-savings-old date dt zone bump)
1617 (math-std-daylight-savings-new date dt zone bump)))
1619 (defun math-std-daylight-savings-new (date dt zone bump)
1620 "Standard North American daylight saving algorithm as of 2007.
1621 This implements the rules for the U.S. and Canada.
1622 Daylight saving begins on the second Sunday of March at 2 a.m.,
1623 and ends on the first Sunday of November at 2 a.m."
1624 (cond ((< (nth 1 dt) 3) 0)
1626 (let ((sunday (math-prev-weekday-in-month date dt 14 0)))
1627 (cond ((< (nth 2 dt) sunday) 0)
1628 ((= (nth 2 dt) sunday)
1629 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1631 ((< (nth 1 dt) 11) -1)
1633 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1634 (cond ((< (nth 2 dt) sunday) -1)
1635 ((= (nth 2 dt) sunday)
1636 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1640 (defun math-std-daylight-savings-old (date dt zone bump)
1641 "Standard North American daylight saving algorithm before 2007.
1642 This implements the rules for the U.S. and Canada.
1643 Daylight saving begins on the first Sunday of April at 2 a.m.,
1644 and ends on the last Sunday of October at 2 a.m."
1645 (cond ((< (nth 1 dt) 4) 0)
1647 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1648 (cond ((< (nth 2 dt) sunday) 0)
1649 ((= (nth 2 dt) sunday)
1650 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1652 ((< (nth 1 dt) 10) -1)
1654 (let ((sunday (math-prev-weekday-in-month date dt 31 0)))
1655 (cond ((< (nth 2 dt) sunday) -1)
1656 ((= (nth 2 dt) sunday)
1657 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1661 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1662 ;;; day of the given month.
1663 (defun math-prev-weekday-in-month (date dt day wday)
1664 (or day (setq day (nth 2 dt)))
1665 (if (> day (math-days-in-month (car dt) (nth 1 dt)))
1666 (setq day (math-days-in-month (car dt) (nth 1 dt))))
1667 (let ((zeroth (math-sub (math-floor date) (nth 2 dt))))
1668 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day))) zeroth)))
1670 (defun calcFunc-pwday (date &optional day weekday)
1671 (if (eq (car-safe date) 'date)
1672 (setq date (nth 1 date)))
1673 (or (math-realp date)
1674 (math-reject-arg date 'datep))
1675 (if (math-messy-integerp day) (setq day (math-trunc day)))
1676 (or (integerp day) (math-reject-arg day 'fixnump))
1677 (if (= day 0) (setq day 31))
1678 (and (or (< day 7) (> day 31)) (math-reject-arg day 'range))
1679 (math-prev-weekday-in-month date (math-date-to-dt date) day (or weekday 0)))
1682 (defun calcFunc-newweek (date &optional weekday)
1683 (if (eq (car-safe date) 'date)
1684 (setq date (nth 1 date)))
1685 (or (math-realp date)
1686 (math-reject-arg date 'datep))
1687 (or weekday (setq weekday 0))
1688 (and (math-messy-integerp weekday) (setq weekday (math-trunc weekday)))
1689 (or (integerp weekday) (math-reject-arg weekday 'fixnump))
1690 (and (or (< weekday 0) (> weekday 6)) (math-reject-arg weekday 'range))
1691 (setq date (math-floor date))
1692 (list 'date (math-sub date (calcFunc-weekday (math-sub date weekday)))))
1694 (defun calcFunc-newmonth (date &optional day)
1695 (or day (setq day 1))
1696 (and (math-messy-integerp day) (setq day (math-trunc day)))
1697 (or (integerp day) (math-reject-arg day 'fixnump))
1698 (and (or (< day 0) (> day 31)) (math-reject-arg day 'range))
1699 (let* ((dt (math-date-to-dt date))
1700 (dim (math-days-in-month (car dt) (nth 1 dt)))
1701 (julian (if calc-gregorian-switch
1702 (math-date-to-dt (math-sub
1703 (or (nth 3 calc-gregorian-switch)
1704 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))
1706 (if (or (= day 0) (> day dim))
1708 (setq day (1- day)))
1709 ;; Adjust if this occurs near the switch to the Gregorian calendar
1710 (if calc-gregorian-switch
1712 ((and (math-dt-before-p (list (car dt) (nth 1 dt) 1) calc-gregorian-switch)
1713 (math-dt-before-p julian (list (car dt) (nth 1 dt) 1)))
1714 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the month
1716 (math-dt-to-date (list (car calc-gregorian-switch)
1717 (nth 1 calc-gregorian-switch)
1718 (if (> (+ (nth 2 calc-gregorian-switch) day) dim)
1720 (+ (nth 2 calc-gregorian-switch) day))))))
1721 ((and (eq (car dt) (car calc-gregorian-switch))
1722 (= (nth 1 dt) (nth 1 calc-gregorian-switch)))
1723 ;; In this case, the switch to the Gregorian calendar occurs in the given month
1724 (if (< (+ (nth 2 julian) day) (nth 2 calc-gregorian-switch))
1725 ;; If the DAYth day occurs before the switch, use it
1726 (list 'date (math-dt-to-date (list (car dt) (nth 1 dt) (1+ day))))
1727 ;; Otherwise do some computations
1728 (let ((tm (+ day (- (nth 2 calc-gregorian-switch) (nth 2 julian)))))
1729 (list 'date (math-dt-to-date
1733 (if (> tm dim) dim tm)))))))
1734 ((and (eq (car dt) (car julian))
1735 (= (nth 1 dt) (nth 1 julian)))
1736 ;; In this case, the current month is truncated because of the switch
1737 ;; to the Gregorian calendar
1738 (list 'date (math-dt-to-date
1741 (if (>= day (nth 2 julian))
1746 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) day))))
1747 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) day)))))
1749 (defun calcFunc-newyear (date &optional day)
1750 (if (eq (car-safe date) 'date) (setq date (nth 1 date)))
1751 (or day (setq day 1))
1752 (and (math-messy-integerp day) (setq day (math-trunc day)))
1753 (or (integerp day) (math-reject-arg day 'fixnump))
1754 (let* ((dt (math-date-to-dt date))
1755 (gregbeg (if calc-gregorian-switch
1756 (or (nth 3 calc-gregorian-switch)
1757 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))))
1758 (julianend (if calc-gregorian-switch (math-sub gregbeg 1)))
1759 (julian (if calc-gregorian-switch
1760 (math-date-to-dt julianend))))
1761 (if (and (>= day 0) (<= day 366))
1762 (let ((max (if (math-leap-year-p (car dt)) 366 365)))
1763 (if (or (= day 0) (> day max)) (setq day max))
1764 (if calc-gregorian-switch
1765 ;; Now to break this down into cases
1767 ((and (math-dt-before-p (list (car dt) 1 1) calc-gregorian-switch)
1768 (math-dt-before-p julian (list (car dt) 1 1)))
1769 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the year
1770 (list 'date (math-min (math-add gregbeg (1- day))
1771 (math-dt-to-date (list (car calc-gregorian-switch) 12 31)))))
1772 ((eq (car dt) (car julian))
1773 ;; In this case, the switch to the Gregorian calendar occurs in the given year
1774 (if (Math-lessp (car julian) (car calc-gregorian-switch))
1775 ;; Here, the last Julian day is the last day of the year.
1776 (list 'date (math-min (math-add (math-dt-to-date (list (car dt) 1 1)) (1- day))
1778 ;; Otherwise, just make sure the date doesn't go past the end of the year
1779 (list 'date (math-min (math-add (math-dt-to-date (list (car dt) 1 1)) (1- day))
1780 (math-dt-to-date (list (car dt) 12 31))))))
1782 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1784 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1786 (if (and (>= day -12) (<= day -1))
1787 (if (and calc-gregorian-switch
1788 (math-dt-before-p (list (car dt) (- day) 1) calc-gregorian-switch)
1789 (math-dt-before-p julian (list (car dt) (- day) 1)))
1790 (list 'date gregbeg)
1791 (list 'date (math-dt-to-date (list (car dt) (- day) 1))))
1792 (math-reject-arg day 'range)))))
1794 (defun calcFunc-incmonth (date &optional step)
1795 (or step (setq step 1))
1796 (and (math-messy-integerp step) (setq step (math-trunc step)))
1797 (or (math-integerp step) (math-reject-arg step 'integerp))
1798 (let* ((dt (math-date-to-dt date))
1800 (month (math-add (1- (nth 1 dt)) step))
1801 (extra (calcFunc-idiv month 12))
1803 (setq month (1+ (math-sub month (math-mul extra 12)))
1804 year (math-add year extra)
1805 day (min day (math-days-in-month year month)))
1806 (and (math-posp (car dt)) (not (math-posp year))
1807 (setq year (math-sub year 1))) ; did we go past the year zero?
1808 (and (math-negp (car dt)) (not (math-negp year))
1809 (setq year (math-add year 1)))
1810 (list 'date (math-dt-to-date
1811 (cons year (cons month (cons day (cdr (cdr (cdr dt))))))))))
1813 (defun calcFunc-incyear (date &optional step)
1814 (calcFunc-incmonth date (math-mul (or step 1) 12)))
1818 (defun calcFunc-bsub (a b)
1819 (or (eq (car-safe a) 'date)
1820 (math-reject-arg a 'datep))
1821 (if (eq (car-safe b) 'date)
1822 (if (math-lessp (nth 1 a) (nth 1 b))
1823 (math-neg (calcFunc-bsub b a))
1824 (math-setup-holidays b)
1825 (let* ((da (math-to-business-day a))
1826 (db (math-to-business-day b)))
1827 (math-add (math-sub (car da) (car db))
1828 (if (and (cdr db) (not (cdr da))) 1 0))))
1829 (calcFunc-badd a (math-neg b))))
1831 (defvar math-holidays-cache nil)
1832 (defvar math-holidays-cache-tag t)
1833 (defun calcFunc-badd (a b)
1834 (if (eq (car-safe b) 'date)
1835 (if (eq (car-safe a) 'date)
1836 (math-reject-arg nil "*Invalid combination in date arithmetic")
1837 (calcFunc-badd b a))
1838 (if (eq (car-safe a) 'date)
1842 (let* ((d (math-to-business-day a))
1843 (bb (math-add (car d)
1844 (if (and (cdr d) (Math-posp b))
1845 (math-sub b 1) b))))
1846 (or (math-from-business-day bb)
1847 (calcFunc-badd a b))))
1848 (if (eq (car-safe b) 'hms)
1849 (let ((hours (nth 7 math-holidays-cache)))
1850 (setq b (math-div (math-from-hms b 'deg) 24))
1852 (setq b (math-div b (cdr hours))))
1853 (calcFunc-badd a b))
1854 (math-reject-arg nil "*Invalid combination in date arithmetic")))
1855 (math-reject-arg a 'datep))))
1857 (defun calcFunc-holiday (a)
1858 (if (cdr (math-to-business-day a)) 1 0))
1860 ;;; Compute the number of business days since Jan 1, 1 AD.
1862 (defun math-to-business-day (date &optional need-year)
1863 (if (eq (car-safe date) 'date)
1864 (setq date (nth 1 date)))
1865 (or (Math-realp date)
1866 (math-reject-arg date 'datep))
1867 (let* ((day (math-floor date))
1868 (time (math-sub date day))
1869 (dt (math-date-to-dt day))
1872 (or (not need-year) (eq (car dt) need-year)
1873 (math-reject-arg (list 'date day) "*Generated holiday has wrong year"))
1874 (math-setup-holidays date)
1875 (let ((days (car math-holidays-cache)))
1876 (while (and (setq days (cdr days)) (< (car days) day))
1877 (setq delta (1+ delta)))
1878 (and days (= day (car days))
1880 (let* ((weekdays (nth 3 math-holidays-cache))
1881 (weeks (1- (/ (+ day 6) 7)))
1882 (wkday (- day 1 (* weeks 7))))
1883 (setq delta (+ delta (* weeks (length weekdays))))
1884 (while (and weekdays (< (car weekdays) wkday))
1885 (setq weekdays (cdr weekdays)
1887 (and weekdays (eq wkday (car weekdays))
1889 (let ((hours (nth 7 math-holidays-cache)))
1892 (setq time (math-div (math-sub time (car hours)) (cdr hours)))
1893 (if (Math-lessp time 0) (setq time 0))
1894 (or (Math-lessp time 1)
1897 (math-div 1 (math-mul 86400 (cdr hours)))))))))
1898 (cons (math-add (math-sub day delta) time) holiday)))
1901 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1902 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1904 (defun math-from-business-day (num)
1905 (let* ((day (math-floor num))
1906 (time (math-sub num day)))
1908 (math-reject-arg nil "*Date is outside valid range"))
1909 (math-setup-holidays)
1910 (let ((days (nth 1 math-holidays-cache))
1912 (while (and (setq days (cdr days)) (< (car days) day))
1913 (setq delta (1+ delta)))
1914 (setq day (+ day delta)))
1915 (let* ((weekdays (nth 3 math-holidays-cache))
1916 (bweek (- 7 (length weekdays)))
1917 (weeks (1- (/ (+ day (1- bweek)) bweek)))
1918 (wkday (- day 1 (* weeks bweek)))
1920 (setq day (+ day (* weeks (length weekdays))))
1921 (while (if (memq w weekdays)
1923 (> (setq wkday (1- wkday)) 0))
1925 (let ((hours (nth 7 math-holidays-cache)))
1927 (setq time (math-add (math-mul time (cdr hours)) (car hours)))))
1928 (and (not (math-setup-holidays day))
1929 (list 'date (math-add day time))))))
1931 ;; The variable math-sh-year is local to math-setup-holidays
1932 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1933 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1934 (defvar math-sh-year)
1936 (defun math-setup-holidays (&optional date)
1937 (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag)
1938 (let ((h (calc-var-value 'var-Holidays))
1939 (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3)
1940 (thu . 4) (fri . 5) (sat . 6) ))
1941 (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil))
1942 (or (math-vectorp h)
1943 (math-reject-arg h "*Holidays variable must be a vector"))
1944 (while (setq h (cdr h))
1945 (cond ((or (and (eq (car-safe (car h)) 'date)
1946 (integerp (nth 1 (car h))))
1947 (and (eq (car-safe (car h)) 'intv)
1948 (eq (car-safe (nth 2 (car h))) 'date))
1949 (eq (car-safe (car h)) 'vec))
1950 (setq days (cons (car h) days)))
1951 ((and (eq (car-safe (car h)) 'var)
1952 (assq (nth 1 (car h)) wdnames))
1953 (setq weekdays (cons (cdr (assq (nth 1 (car h)) wdnames))
1955 ((and (eq (car-safe (car h)) 'intv)
1956 (eq (car-safe (nth 2 (car h))) 'hms)
1957 (eq (car-safe (nth 3 (car h))) 'hms))
1960 (car h) "*Only one hours interval allowed in Holidays"))
1961 (setq hours (math-div (car h) '(hms 24 0 0)))
1962 (if (or (Math-lessp (nth 2 hours) 0)
1963 (Math-lessp 1 (nth 3 hours)))
1965 (car h) "*Hours interval out of range"))
1966 (setq hours (cons (nth 2 hours)
1967 (math-sub (nth 3 hours) (nth 2 hours))))
1968 (if (Math-zerop (cdr hours))
1970 (car h) "*Degenerate hours interval")))
1971 ((or (and (eq (car-safe (car h)) 'intv)
1972 (Math-integerp (nth 2 (car h)))
1973 (Math-integerp (nth 3 (car h))))
1974 (and (integerp (car h))
1975 (> (car h) 1900) (< (car h) 2100)))
1978 (car h) "*Only one limit allowed in Holidays"))
1979 (setq limit (calcFunc-vint (car h) '(intv 3 1 2737)))
1980 (if (equal limit '(vec))
1981 (math-reject-arg (car h) "*Limit is out of range")))
1982 ((or (math-expr-contains (car h) '(var y var-y))
1983 (math-expr-contains (car h) '(var m var-m)))
1984 (setq exprs (cons (car h) exprs)))
1986 (car h) "*Holidays must contain a vector of holidays"))))
1987 (if (= (length weekdays) 7)
1988 (math-reject-arg nil "*Too many weekend days"))
1989 (setq math-holidays-cache (list (list -1) ; 0: days list
1990 (list -1) ; 1: inverse-days list
1993 (or limit '(intv 3 1 2737))
1994 nil ; 5: (lo.hi) expanded years
1996 hours) ; 7: business hours
1997 math-holidays-cache-tag (calc-var-value 'var-Holidays))))
1999 (let ((year (calcFunc-year date))
2000 (limits (nth 5 math-holidays-cache))
2002 (or (eq (calcFunc-in year (nth 4 math-holidays-cache)) 1)
2004 (or (eq (car-safe date) 'date) (setq date (list 'date date)))
2005 (math-reject-arg date "*Date is outside valid range")))
2007 (let ((days (nth 6 math-holidays-cache)))
2009 (let ((math-sh-year nil)) ; see below
2010 (setcar (nthcdr 6 math-holidays-cache) nil)
2011 (math-setup-add-holidays (cons 'vec (cdr days)))
2012 (setcar (nthcdr 2 math-holidays-cache) (car days))))
2013 (cond ((not (nth 2 math-holidays-cache))
2017 (setcar (nthcdr 5 math-holidays-cache) (cons year year))
2018 (math-setup-year-holidays year)
2020 ((< year (car limits))
2021 (message "Computing holidays, %d .. %d"
2022 year (1- (car limits)))
2023 (calc-set-command-flag 'clear-message)
2024 (while (< year (car limits))
2025 (setcar limits (1- (car limits)))
2026 (math-setup-year-holidays (car limits)))
2028 ((> year (cdr limits))
2029 (message "Computing holidays, %d .. %d"
2030 (1+ (cdr limits)) year)
2031 (calc-set-command-flag 'clear-message)
2032 (while (> year (cdr limits))
2033 (setcdr limits (1+ (cdr limits)))
2034 (math-setup-year-holidays (cdr limits)))
2039 (or done (setq math-holidays-cache-tag t))))))
2041 (defun math-setup-year-holidays (math-sh-year)
2042 (let ((exprs (nth 2 math-holidays-cache)))
2044 (let* ((var-y math-sh-year)
2046 (expr (math-evaluate-expr (car exprs))))
2047 (if (math-expr-contains expr '(var m var-m))
2049 (while (<= (setq var-m (1+ var-m)) 12)
2050 (math-setup-add-holidays (math-evaluate-expr expr))))
2051 (math-setup-add-holidays expr)))
2052 (setq exprs (cdr exprs)))))
2054 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
2055 (cond ((eq (car-safe days) 'vec)
2056 (while (setq days (cdr days))
2057 (math-setup-add-holidays (car days))))
2058 ((eq (car-safe days) 'intv)
2059 (let ((day (math-ceiling (nth 2 days))))
2060 (or (eq (calcFunc-in day days) 1)
2061 (setq day (math-add day 1)))
2062 (while (eq (calcFunc-in day days) 1)
2063 (math-setup-add-holidays day)
2064 (setq day (math-add day 1)))))
2065 ((eq (car-safe days) 'date)
2066 (math-setup-add-holidays (nth 1 days)))
2069 (let ((b (math-to-business-day days math-sh-year)))
2070 (or (cdr b) ; don't register holidays twice!
2071 (let ((prev (car math-holidays-cache))
2072 (iprev (nth 1 math-holidays-cache)))
2073 (while (and (cdr prev) (< (nth 1 prev) days))
2074 (setq prev (cdr prev) iprev (cdr iprev)))
2075 (setcdr prev (cons days (cdr prev)))
2076 (setcdr iprev (cons (car b) (cdr iprev)))
2077 (while (setq iprev (cdr iprev))
2078 (setcar iprev (1- (car iprev))))))))
2080 (math-reject-arg (list 'date days) "*Invalid holiday value"))
2082 (math-reject-arg days "*Holiday formula failed to evaluate"))))
2089 ;;; Build a standard deviation form. [X X X]
2090 (defun math-make-sdev (x sigma)
2091 (if (memq (car-safe x) '(date mod sdev intv vec))
2092 (math-reject-arg x 'realp))
2093 (if (memq (car-safe sigma) '(date mod sdev intv vec))
2094 (math-reject-arg sigma 'realp))
2095 (if (or (Math-negp sigma) (memq (car-safe sigma) '(cplx polar)))
2096 (setq sigma (math-abs sigma)))
2097 (if (and (Math-zerop sigma) (Math-scalarp x))
2099 (list 'sdev x sigma)))
2100 (defun calcFunc-sdev (x sigma)
2101 (math-make-sdev x sigma))
2107 (defun math-normalize-mod (a)
2108 (let ((n (math-normalize (nth 1 a)))
2109 (m (math-normalize (nth 2 a))))
2110 (if (and (math-anglep n) (math-anglep m) (math-posp m))
2112 (math-normalize (list 'calcFunc-makemod n m)))))
2114 ;;; Build a modulo form. [N R R]
2115 (defun math-make-mod (n m)
2116 (setq calc-previous-modulo m)
2118 (cond ((not (Math-anglep m))
2119 (math-reject-arg m 'anglep))
2120 ((not (math-posp m))
2121 (math-reject-arg m 'posp))
2123 (if (or (Math-negp n)
2124 (not (Math-lessp n m)))
2125 (list 'mod (math-mod n m) m)
2127 ((memq (car n) '(+ - / vec neg))
2130 (mapcar (function (lambda (x) (math-make-mod x m)))
2132 ((and (eq (car n) '*) (Math-anglep (nth 1 n)))
2133 (math-mul (math-make-mod (nth 1 n) m) (nth 2 n)))
2134 ((memq (car n) '(* ^ var calcFunc-subscr))
2135 (math-mul (math-make-mod 1 m) n))
2136 (t (math-reject-arg n 'anglep)))))
2137 (defun calcFunc-makemod (n m)
2138 (math-make-mod n m))
2142 ;;;; Interval forms.
2144 ;;; Build an interval form. [X S X X]
2145 (defun math-make-intv (mask lo hi)
2146 (if (memq (car-safe lo) '(cplx polar mod sdev intv vec))
2147 (math-reject-arg lo 'realp))
2148 (if (memq (car-safe hi) '(cplx polar mod sdev intv vec))
2149 (math-reject-arg hi 'realp))
2150 (or (eq (eq (car-safe lo) 'date) (eq (car-safe hi) 'date))
2151 (math-reject-arg (if (eq (car-safe lo) 'date) hi lo) 'datep))
2152 (if (and (or (Math-realp lo) (eq (car lo) 'date))
2153 (or (Math-realp hi) (eq (car hi) 'date)))
2154 (let ((cmp (math-compare lo hi)))
2158 (list 'intv mask lo hi))
2161 (list 'intv 2 lo lo)
2162 (list 'intv mask lo lo))
2163 (list 'intv mask lo hi))))
2164 (list 'intv mask lo hi)))
2165 (defun calcFunc-intv (mask lo hi)
2166 (if (math-messy-integerp mask) (setq mask (math-trunc mask)))
2167 (or (natnump mask) (math-reject-arg mask 'fixnatnump))
2168 (or (<= mask 3) (math-reject-arg mask 'range))
2169 (math-make-intv mask lo hi))
2171 (defun math-sort-intv (mask lo hi)
2172 (if (Math-lessp hi lo)
2173 (math-make-intv (aref [0 2 1 3] mask) hi lo)
2174 (math-make-intv mask lo hi)))
2179 (defun math-combine-intervals (a am b bm c cm d dm)
2181 (if (= (setq res (math-compare a c)) 1)
2184 (setq am (or am cm))))
2185 (if (= (setq res (math-compare b d)) -1)
2188 (setq bm (or bm dm))))
2189 (math-make-intv (+ (if am 2 0) (if bm 1 0)) a b)))
2192 (defun math-div-mod (a b m) ; [R R R R] (Returns nil if no solution)
2193 (and (Math-integerp a) (Math-integerp b) (Math-integerp m)
2194 (let ((u1 1) (u3 b) (v1 0) (v3 m))
2195 (while (not (eq v3 0)) ; See Knuth sec 4.5.2, exercise 15
2196 (let* ((q (math-idivmod u3 v3))
2197 (t1 (math-sub u1 (math-mul v1 (car q)))))
2198 (setq u1 v1 u3 v3 v1 t1 v3 (cdr q))))
2199 (let ((q (math-idivmod a u3)))
2201 (math-mod (math-mul (car q) u1) m))))))
2203 (defun math-mod-intv (a b)
2204 (let* ((q1 (math-floor (math-div (nth 2 a) b)))
2205 (q2 (math-floor (math-div (nth 3 a) b)))
2206 (m1 (math-sub (nth 2 a) (math-mul q1 b)))
2207 (m2 (math-sub (nth 3 a) (math-mul q2 b))))
2208 (cond ((equal q1 q2)
2209 (math-sort-intv (nth 1 a) m1 m2))
2210 ((and (math-equal-int (math-sub q2 q1) 1)
2212 (memq (nth 1 a) '(0 2)))
2213 (math-make-intv (nth 1 a) m1 b))
2215 (math-make-intv 2 0 b)))))
2217 ;; The variables math-exp-str and math-exp-pos are local to
2218 ;; math-read-exprs in math-aent.el, but are used by
2219 ;; math-read-angle-brackets, which is called (indirectly) by
2221 (defvar math-exp-str)
2222 (defvar math-exp-pos)
2224 (defun math-read-angle-brackets ()
2225 (let* ((last (or (math-check-for-commas t) (length math-exp-str)))
2226 (str (substring math-exp-str math-exp-pos last))
2228 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str)
2229 (let ((str1 (substring str 0 (1- (match-end 0))))
2230 (str2 (substring str (match-end 0)))
2231 (calc-hashes-used 0))
2232 (setq str1 (math-read-expr (concat "[" str1 "]")))
2233 (if (eq (car-safe str1) 'error)
2235 (setq str2 (math-read-expr str2))
2236 (if (eq (car-safe str2) 'error)
2238 (append '(calcFunc-lambda) (cdr str1) (list str2)))))
2239 (if (string-match "#" str)
2240 (let ((calc-hashes-used 0))
2241 (and (setq str (math-read-expr str))
2242 (if (eq (car-safe str) 'error)
2244 (append '(calcFunc-lambda)
2245 (calc-invent-args calc-hashes-used)
2247 (math-parse-date str)))))
2249 (throw 'syntax res))
2250 (if (eq (car-safe res) 'error)
2251 (throw 'syntax (nth 2 res)))
2252 (setq math-exp-pos (1+ last))
2256 (provide 'calc-forms)
2258 ;;; calc-forms.el ends here