1 ;;; calc-forms.el --- data format conversion functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
35 ;; Declare functions which are defined elsewhere.
36 (declare-function calendar-current-time-zone
"cal-dst" ())
37 (declare-function calendar-absolute-from-gregorian
"calendar" (date))
38 (declare-function dst-in-effect
"cal-dst" (date))
44 (let ((time (current-time-string)))
45 (calc-enter-result 0 "time"
48 (string-to-number (substring time
11 13))
49 (string-to-number (substring time
14 16))
50 (string-to-number (substring time
17 19)))
51 (list 'hms
24 0 0))))))
53 (defun calc-to-hms (arg)
57 (if (eq calc-angle-mode
'rad
)
58 (calc-unary-op ">rad" 'calcFunc-rad arg
)
59 (calc-unary-op ">deg" 'calcFunc-deg arg
))
60 (calc-unary-op ">hms" 'calcFunc-hms arg
))))
62 (defun calc-from-hms (arg)
68 (defun calc-hms-notation (fmt)
69 (interactive "sHours-minutes-seconds format (hms, @ ' \", etc.): ")
71 (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt
)
73 (calc-change-mode 'calc-hms-format
74 (concat "%s" (math-match-substring fmt
1)
75 (math-match-substring fmt
2)
76 "%s" (math-match-substring fmt
3)
77 (math-match-substring fmt
4)
78 "%s" (math-match-substring fmt
5))
80 (setq-default calc-hms-format calc-hms-format
)) ; for minibuffer
81 (error "Bad hours-minutes-seconds format"))))
83 (defun calc-date-notation (fmt arg
)
84 (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP")
88 (if (string-match "\\` *[0-9] *\\'" fmt
)
89 (setq fmt
(nth (string-to-number fmt
) calc-standard-date-formats
)))
90 (or (string-match "[a-zA-Z]" fmt
)
91 (error "Bad date format specifier"))
93 (>= (setq arg
(prefix-numeric-value arg
)) 0)
95 (setq calc-standard-date-formats
96 (copy-sequence calc-standard-date-formats
))
97 (setcar (nthcdr arg calc-standard-date-formats
) fmt
))
98 (let ((case-fold-search nil
))
99 (and (not (string-match "<.*>" fmt
))
100 (string-match "\\`[^hHspP]*\\([^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*[bBhHmpPsS]+[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*\\)[^hHspP]*\\'" fmt
)
101 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
102 (regexp-quote (math-match-substring fmt
1))
103 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt
)
104 (setq fmt
(concat (substring fmt
0 (match-beginning 0))
106 (substring fmt
(match-beginning 0) (match-end 0))
108 (substring fmt
(match-end 0))))))
113 (let ((case-fold-search nil
))
114 (and (setq temp
(string-match ":[BS]S" fmt
))
116 (while (setq pos
(string-match "[<>a-zA-Z]" fmt
))
118 (setq lfmt
(cons (substring fmt
0 pos
) lfmt
)))
120 (cond ((= (aref fmt pos
) ?\
<)
121 (and time
(error "Nested <'s not allowed"))
122 (and lfmt
(setq fullfmt
(nconc lfmt fullfmt
)
125 ((= (aref fmt pos
) ?\
>)
126 (or time
(error "Misplaced > in format"))
127 (and lfmt
(setq fullfmt
(cons (nreverse lfmt
) fullfmt
)
131 (if (string-match "\\`[^a-zA-Z]*[bB][a-zA-Z]" fmt
)
132 (setq pos2
(1+ pos2
)))
133 (while (and (< pos2
(length fmt
))
134 (= (upcase (aref fmt pos2
))
135 (upcase (aref fmt
(1- pos2
)))))
136 (setq pos2
(1+ pos2
)))
137 (setq sym
(intern (substring fmt pos pos2
)))
138 (or (memq sym
'(Y YY BY YYY YYYY
139 aa AA aaa AAA aaaa AAAA
140 bb BB bbb BBB bbbb BBBB
141 M MM BM mmm Mmm Mmmm MMM MMMM
143 W www Www Wwww WWW WWWW
146 m mm bm s ss bss SS BS C
148 (and (eq sym
'X
) (not lfmt
) (not fullfmt
))
149 (error "Bad format code: %s" sym
))
150 (and (memq sym
'(bb BB bbb BBB bbbb BBBB
))
151 (setq lfmt
(cons 'b lfmt
)))
152 (setq lfmt
(cons sym lfmt
))))
153 (setq fmt
(substring fmt pos2
)))
155 (setq lfmt
(cons fmt lfmt
)))
157 (setq fullfmt
(cons (nreverse lfmt
) fullfmt
))
158 (setq fullfmt
(nconc lfmt fullfmt
))))
159 (calc-change-mode 'calc-date-format
(nreverse fullfmt
) t
))))
162 (defun calc-hms-mode ()
165 (calc-change-mode 'calc-angle-mode
'hms
)
166 (message "Angles measured in degrees-minutes-seconds")))
169 (defun calc-now (arg)
171 (calc-date-zero-args "now" 'calcFunc-now arg
))
173 (defun calc-date-part (arg)
174 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
175 (if (or (< arg
1) (> arg
9))
176 (error "Part code out of range"))
179 (nth arg
'(nil "year" "mnth" "day" "hour" "minu"
180 "sec" "wday" "yday" "hmst"))
181 (list (nth arg
'(nil calcFunc-year calcFunc-month
182 calcFunc-day calcFunc-hour
183 calcFunc-minute calcFunc-second
184 calcFunc-weekday calcFunc-yearday
188 (defun calc-date (arg)
190 (if (or (< arg
1) (> arg
6))
191 (error "Between one and six arguments are allowed"))
193 (calc-enter-result arg
"date" (cons 'calcFunc-date
(calc-top-list-n arg
)))))
195 (defun calc-julian (arg)
197 (calc-date-one-arg "juln" 'calcFunc-julian arg
))
199 (defun calc-unix-time (arg)
201 (calc-date-one-arg "unix" 'calcFunc-unixtime arg
))
203 (defun calc-time-zone (arg)
205 (calc-date-zero-args "zone" 'calcFunc-tzone arg
))
207 (defun calc-convert-time-zones (old &optional new
)
208 (interactive "sFrom time zone: ")
211 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv
(calc-top-list-n 3)))
212 (if (equal old
"") (setq old
"local"))
214 (setq new
(read-string (concat "From time zone: " old
216 (if (stringp old
) (setq old
(math-read-expr old
)))
217 (if (eq (car-safe old
) 'error
)
218 (error "Error in expression: %S" (nth 1 old
)))
219 (if (equal new
"") (setq new
"local"))
220 (if (stringp new
) (setq new
(math-read-expr new
)))
221 (if (eq (car-safe new
) 'error
)
222 (error "Error in expression: %S" (nth 1 new
)))
223 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
224 (calc-top-n 1) old new
)))))
226 (defun calc-new-week (arg)
228 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg
))
230 (defun calc-new-month (arg)
232 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg
))
234 (defun calc-new-year (arg)
236 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg
))
238 (defun calc-inc-month (arg)
240 (calc-date-one-arg "incm" 'calcFunc-incmonth arg
))
242 (defun calc-business-days-plus (arg)
245 (calc-binary-op "bus+" 'calcFunc-badd arg
)))
247 (defun calc-business-days-minus (arg)
250 (calc-binary-op "bus-" 'calcFunc-bsub arg
)))
252 (defun calc-date-zero-args (prefix func arg
)
255 (calc-enter-result 1 prefix
(list func
(calc-top-n 1)))
256 (calc-enter-result 0 prefix
(if arg
257 (list func
(prefix-numeric-value arg
))
260 (defun calc-date-one-arg (prefix func arg
)
263 (calc-enter-result 2 prefix
(cons func
(calc-top-list-n 2)))
264 (calc-enter-result 1 prefix
(if arg
265 (list func
(calc-top-n 1)
266 (prefix-numeric-value arg
))
267 (list func
(calc-top-n 1)))))))
270 ;;;; Hours-minutes-seconds forms.
272 (defun math-normalize-hms (a)
273 (let ((h (math-normalize (nth 1 a
)))
274 (m (math-normalize (nth 2 a
)))
275 (s (let ((calc-internal-prec (max (- calc-internal-prec
4) 3)))
276 (math-normalize (nth 3 a
)))))
280 (setq s
(math-add s -
60)
283 (setq m
(math-add m -
60)
285 (if (not (Math-lessp -
60 s
))
286 (setq s
(math-add s
60)
288 (if (not (Math-lessp -
60 m
))
289 (setq m
(math-add m
60)
292 (setq s
(math-add s
60)
295 (setq m
(math-add m
60)
297 (if (not (Math-lessp s
60))
298 (setq s
(math-add s -
60)
300 (if (not (Math-lessp m
60))
301 (setq m
(math-add m -
60)
303 (if (and (eq (car-safe s
) 'float
)
304 (<= (+ (math-numdigs (nth 1 s
)) (nth 2 s
))
305 (- 2 calc-internal-prec
)))
309 ;;; Convert A from ANG or current angular mode to HMS format.
310 (defun math-to-hms (a &optional ang
) ; [X R] [Public]
311 (cond ((eq (car-safe a
) 'hms
) a
)
312 ((eq (car-safe a
) 'sdev
)
313 (math-make-sdev (math-to-hms (nth 1 a
))
314 (math-to-hms (nth 2 a
))))
315 ((not (Math-numberp a
))
316 (list 'calcFunc-hms a
))
318 (math-neg (math-to-hms (math-neg a
) ang
)))
319 ((eq (or ang calc-angle-mode
) 'rad
)
320 (math-to-hms (math-div a
(math-pi-over-180)) 'deg
))
321 ((memq (car-safe a
) '(cplx polar
)) a
)
323 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
324 ; (math-normalize a)))
326 (let* ((b (math-mul a
3600))
327 (hm (math-trunc (math-div b
60)))
328 (hmd (math-idivmod hm
60)))
332 (math-sub b
(math-mul hm
60))))))))
333 (defun calcFunc-hms (h &optional m s
)
334 (or (Math-realp h
) (math-reject-arg h
'realp
))
336 (or (Math-realp m
) (math-reject-arg m
'realp
))
338 (or (Math-realp s
) (math-reject-arg s
'realp
))
339 (if (and (not (Math-lessp m
0)) (Math-lessp m
60)
340 (not (Math-lessp s
0)) (Math-lessp s
60))
341 (math-add (math-to-hms h
)
343 (math-to-hms (math-add h
344 (math-add (math-div (or m
0) 60)
345 (math-div (or s
0) 3600)))
348 ;;; Convert A from HMS format to ANG or current angular mode.
349 (defun math-from-hms (a &optional ang
) ; [R X] [Public]
350 (cond ((not (eq (car-safe a
) 'hms
))
353 (if (eq (car-safe a
) 'sdev
)
354 (math-make-sdev (math-from-hms (nth 1 a
) ang
)
355 (math-from-hms (nth 2 a
) ang
))
356 (if (eq (or ang calc-angle-mode
) 'rad
)
357 (list 'calcFunc-rad a
)
358 (list 'calcFunc-deg a
)))))
360 (math-neg (math-from-hms (math-neg a
) ang
)))
361 ((eq (or ang calc-angle-mode
) 'rad
)
362 (math-mul (math-from-hms a
'deg
) (math-pi-over-180)))
364 (math-add (math-div (math-add (math-div (nth 3 a
)
373 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
374 ;;; These versions are rewritten to use arbitrary-size integers.
375 ;;; The Julian calendar is used up to 9/2/1752, after which the Gregorian
376 ;;; calendar is used; the first day after 9/2/1752 is 9/14/1752.
378 ;;; A numerical date is the number of days since midnight on
379 ;;; the morning of January 1, 1 A.D. If the date is a non-integer,
380 ;;; it represents a specific date and time.
381 ;;; A "dt" is a list of the form, (year month day), corresponding to
382 ;;; an integer code, or (year month day hour minute second), corresponding
383 ;;; to a non-integer code.
385 (defun math-date-to-dt (value)
386 (if (eq (car-safe value
) 'date
)
387 (setq value
(nth 1 value
)))
388 (or (math-realp value
)
389 (math-reject-arg value
'datep
))
390 (let* ((parts (math-date-parts value
))
395 (year (math-quotient (math-add date
(if (Math-lessp date
711859)
396 365 ; for speed, we take
397 -
108)) ; >1950 as a special case
398 (if (math-negp value
) 366 365)))
399 ; this result may be an overestimate
401 (while (Math-lessp date
(setq temp
(math-absolute-from-date year
1 1)))
402 (setq year
(math-add year -
1)))
403 (if (eq year
0) (setq year -
1))
404 (setq date
(1+ (math-sub date temp
)))
405 (and (eq year
1752) (>= date
247)
406 (setq date
(+ date
11)))
407 (setq temp
(if (math-leap-year-p year
)
408 [1 32 61 92 122 153 183 214 245 275 306 336 999]
409 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
410 (while (>= date
(aref temp month
))
411 (setq month
(1+ month
)))
412 (setq day
(1+ (- date
(aref temp
(1- month
)))))
413 (if (math-integerp value
)
414 (list year month day
)
418 (math-add (% time
60) (nth 2 parts
))))))
420 (defun math-dt-to-date (dt)
421 (or (integerp (nth 1 dt
))
422 (math-reject-arg (nth 1 dt
) 'fixnump
))
423 (if (or (< (nth 1 dt
) 1) (> (nth 1 dt
) 12))
424 (math-reject-arg (nth 1 dt
) "Month value is out of range"))
425 (or (integerp (nth 2 dt
))
426 (math-reject-arg (nth 2 dt
) 'fixnump
))
427 (if (or (< (nth 2 dt
) 1) (> (nth 2 dt
) 31))
428 (math-reject-arg (nth 2 dt
) "Day value is out of range"))
429 (let ((date (math-absolute-from-date (car dt
) (nth 1 dt
) (nth 2 dt
))))
431 (math-add (math-float date
)
432 (math-div (math-add (+ (* (nth 3 dt
) 3600)
438 (defun math-date-parts (value &optional offset
)
439 (let* ((date (math-floor value
))
440 (time (math-round (math-mul (math-sub value
(or offset date
)) 86400)
441 (and (> calc-internal-prec
12)
442 (- calc-internal-prec
12))))
443 (ftime (math-floor time
)))
446 (math-sub time ftime
))))
449 (defun math-this-year ()
450 (string-to-number (substring (current-time-string) -
4)))
452 (defun math-leap-year-p (year)
453 (if (Math-lessp year
1752)
455 (= (math-imod (math-neg year
) 4) 1)
456 (= (math-imod year
4) 0))
457 (setq year
(math-imod year
400))
458 (or (and (= (% year
4) 0) (/= (% year
100) 0))
461 (defun math-days-in-month (year month
)
462 (if (and (= month
2) (math-leap-year-p year
))
464 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month
))))
466 (defun math-day-number (year month day
)
467 (let ((day-of-year (+ day
(* 31 (1- month
)))))
470 (setq day-of-year
(- day-of-year
(/ (+ 23 (* 4 month
)) 10)))
471 (if (math-leap-year-p year
)
472 (setq day-of-year
(1+ day-of-year
)))))
475 (and (= month
9) (>= day
14)))
476 (setq day-of-year
(- day-of-year
11)))
479 (defun math-absolute-from-date (year month day
)
480 (if (eq year
0) (setq year -
1))
481 (let ((yearm1 (math-sub year
1)))
482 (math-sub (math-add (math-day-number year month day
)
483 (math-add (math-mul 365 yearm1
)
485 (math-quotient yearm1
4)
487 (math-quotient (math-sub 3 year
)
489 (if (or (Math-lessp year
1753)
490 (and (eq year
1752) (<= month
9)))
492 (let ((correction (math-mul (math-quotient yearm1
100) 3)))
493 (let ((res (math-idivmod correction
4)))
494 (math-add (if (= (cdr res
) 0)
500 ;;; It is safe to redefine these in your .emacs file to use a different
503 (defvar math-long-weekday-names
'( "Sunday" "Monday" "Tuesday" "Wednesday"
504 "Thursday" "Friday" "Saturday" ))
505 (defvar math-short-weekday-names
'( "Sun" "Mon" "Tue" "Wed"
508 (defvar math-long-month-names
'( "January" "February" "March" "April"
509 "May" "June" "July" "August"
510 "September" "October" "November" "December" ))
511 (defvar math-short-month-names
'( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
512 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
515 (defvar math-format-date-cache nil
)
517 ;; The variables math-fd-date, math-fd-dt, math-fd-year,
518 ;; math-fd-month, math-fd-day, math-fd-weekday, math-fd-hour,
519 ;; math-fd-minute, math-fd-second, math-fd-bc-flag are local
520 ;; to math-format-date, but are used by math-format-date-part,
521 ;; which is called by math-format-date.
522 (defvar math-fd-date
)
524 (defvar math-fd-year
)
525 (defvar math-fd-month
)
527 (defvar math-fd-weekday
)
528 (defvar math-fd-hour
)
529 (defvar math-fd-minute
)
530 (defvar math-fd-second
)
531 (defvar math-fd-bc-flag
)
533 (defun math-format-date (math-fd-date)
534 (if (eq (car-safe math-fd-date
) 'date
)
535 (setq math-fd-date
(nth 1 math-fd-date
)))
536 (let ((entry (list math-fd-date calc-internal-prec calc-date-format
)))
537 (or (cdr (assoc entry math-format-date-cache
))
538 (let* ((math-fd-dt nil
)
539 (calc-group-digits nil
)
540 (calc-leading-zeros nil
)
541 (calc-number-radix 10)
542 math-fd-year math-fd-month math-fd-day math-fd-weekday
543 math-fd-hour math-fd-minute math-fd-second
544 (math-fd-bc-flag nil
)
545 (fmt (apply 'concat
(mapcar 'math-format-date-part
547 (setq math-format-date-cache
(cons (cons entry fmt
)
548 math-format-date-cache
))
549 (and (setq math-fd-dt
(nthcdr 10 math-format-date-cache
))
550 (setcdr math-fd-dt nil
))
553 (defconst math-julian-date-beginning
'(float 17214235 -
1)
554 "The beginning of the Julian calendar,
555 as measured in the number of days before January 1 of the year 1AD.")
557 (defconst math-julian-date-beginning-int
1721424
558 "The beginning of the Julian calendar,
559 as measured in the integer number of days before January 1 of the year 1AD.")
561 (defun math-format-date-part (x)
565 (if (math-integerp math-fd-date
)
567 (apply 'concat
(mapcar 'math-format-date-part x
))))
571 (math-format-number math-fd-date
))
573 (math-format-number (math-floor math-fd-date
)))
576 (math-add math-fd-date math-julian-date-beginning
)))
578 (math-format-number (math-add
579 (math-floor math-fd-date
)
580 math-julian-date-beginning-int
)))
582 (math-format-number (nth 1 (math-date-parts math-fd-date
719164))))
586 (setq math-fd-dt
(math-date-to-dt math-fd-date
)
587 math-fd-year
(car math-fd-dt
)
588 math-fd-month
(nth 1 math-fd-dt
)
589 math-fd-day
(nth 2 math-fd-dt
)
590 math-fd-weekday
(math-mod
591 (math-add (math-floor math-fd-date
) 6) 7)
592 math-fd-hour
(nth 3 math-fd-dt
)
593 math-fd-minute
(nth 4 math-fd-dt
)
594 math-fd-second
(nth 5 math-fd-dt
))
595 (and (memq 'b calc-date-format
)
596 (math-negp math-fd-year
)
597 (setq math-fd-year
(math-neg math-fd-year
)
598 math-fd-bc-flag t
))))
600 (if (and (integerp math-fd-year
) (> math-fd-year
1940) (< math-fd-year
2040))
601 (format (cond ((eq x
'YY
) "%02d")
604 (% math-fd-year
100))
605 (if (and (natnump math-fd-year
) (< math-fd-year
100))
606 (format "+%d" math-fd-year
)
607 (math-format-number math-fd-year
))))
609 (math-format-number math-fd-year
))
611 (if (and (natnump math-fd-year
) (< math-fd-year
100))
612 (format "+%d" math-fd-year
)
613 (math-format-number math-fd-year
)))
616 (and (not math-fd-bc-flag
) "ad"))
618 (and (not math-fd-bc-flag
) "AD"))
620 (and (not math-fd-bc-flag
) "ad "))
622 (and (not math-fd-bc-flag
) "AD "))
624 (and (not math-fd-bc-flag
) "a.d."))
626 (and (not math-fd-bc-flag
) "A.D."))
628 (and math-fd-bc-flag
"bc"))
630 (and math-fd-bc-flag
"BC"))
632 (and math-fd-bc-flag
" bc"))
634 (and math-fd-bc-flag
" BC"))
636 (and math-fd-bc-flag
"b.c."))
638 (and math-fd-bc-flag
"B.C."))
640 (format "%d" math-fd-month
))
642 (format "%02d" math-fd-month
))
644 (format "%2d" math-fd-month
))
646 (downcase (nth (1- math-fd-month
) math-short-month-names
)))
648 (nth (1- math-fd-month
) math-short-month-names
))
650 (upcase (nth (1- math-fd-month
) math-short-month-names
)))
652 (nth (1- math-fd-month
) math-long-month-names
))
654 (upcase (nth (1- math-fd-month
) math-long-month-names
)))
656 (format "%d" math-fd-day
))
658 (format "%02d" math-fd-day
))
660 (format "%2d" math-fd-day
))
662 (format "%d" math-fd-weekday
))
664 (downcase (nth math-fd-weekday math-short-weekday-names
)))
666 (nth math-fd-weekday math-short-weekday-names
))
668 (upcase (nth math-fd-weekday math-short-weekday-names
)))
670 (nth math-fd-weekday math-long-weekday-names
))
672 (upcase (nth math-fd-weekday math-long-weekday-names
)))
674 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
676 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
678 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
680 (and math-fd-hour
(format "%d" math-fd-hour
)))
682 (and math-fd-hour
(format "%02d" math-fd-hour
)))
684 (and math-fd-hour
(format "%2d" math-fd-hour
)))
686 (and math-fd-hour
(format "%d" (1+ (%
(+ math-fd-hour
11) 12)))))
688 (and math-fd-hour
(format "%02d" (1+ (%
(+ math-fd-hour
11) 12)))))
690 (and math-fd-hour
(format "%2d" (1+ (%
(+ math-fd-hour
11) 12)))))
692 (and math-fd-hour
(if (< math-fd-hour
12) "a" "p")))
694 (and math-fd-hour
(if (< math-fd-hour
12) "A" "P")))
696 (and math-fd-hour
(if (< math-fd-hour
12) "am" "pm")))
698 (and math-fd-hour
(if (< math-fd-hour
12) "AM" "PM")))
700 (and math-fd-hour
(if (< math-fd-hour
12) "a.m." "p.m.")))
702 (and math-fd-hour
(if (< math-fd-hour
12) "A.M." "P.M.")))
704 (and math-fd-minute
(format "%d" math-fd-minute
)))
706 (and math-fd-minute
(format "%02d" math-fd-minute
)))
708 (and math-fd-minute
(format "%2d" math-fd-minute
)))
710 (and math-fd-second
(not (math-zerop math-fd-second
))
712 ((memq x
'(s ss bs SS BS
))
714 (not (and (memq x
'(SS BS
)) (math-zerop math-fd-second
)))
715 (if (integerp math-fd-second
)
716 (format (cond ((memq x
'(ss SS
)) "%02d")
717 ((memq x
'(bs BS
)) "%2d")
720 (concat (if (Math-lessp math-fd-second
10)
721 (cond ((memq x
'(ss SS
)) "0")
722 ((memq x
'(bs BS
)) " ")
725 (let ((calc-float-format
726 (list 'fix
(min (- 12 calc-internal-prec
)
728 (math-format-number math-fd-second
))))))))
730 ;; The variable math-pd-str is local to math-parse-date and
731 ;; math-parse-standard-date, but is used by math-parse-date-word,
732 ;; which is called by math-parse-date and math-parse-standard-date.
735 (defun math-parse-date (math-pd-str)
737 (or (math-parse-standard-date math-pd-str t
)
738 (math-parse-standard-date math-pd-str nil
)
739 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str
)
740 (list 'date
(math-read-number (math-match-substring math-pd-str
1))))
741 (let ((case-fold-search t
)
742 (year nil
) (month nil
) (day nil
) (weekday nil
)
743 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
)
744 (a nil
) (b nil
) (c nil
) (bigyear nil
) temp
)
746 ;; Extract the time, if any.
747 (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
)
748 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str
))
749 (let ((ampm (math-match-substring math-pd-str
6)))
750 (setq hour
(string-to-number (math-match-substring math-pd-str
1))
751 minute
(math-match-substring math-pd-str
2)
752 second
(math-match-substring math-pd-str
4)
753 math-pd-str
(concat (substring math-pd-str
0 (match-beginning 0))
754 (substring math-pd-str
(match-end 0))))
755 (if (equal minute
"")
757 (setq minute
(string-to-number minute
)))
758 (if (equal second
"")
760 (setq second
(math-read-number second
)))
763 (throw 'syntax
"Hour value out of range"))
764 (setq ampm
(upcase (aref ampm
0)))
765 (if (memq ampm
'(?N ?M
))
766 (if (and (= hour
12) (= minute
0) (eq second
0))
767 (if (eq ampm ?M
) (setq hour
0))
769 "Time must be 12:00:00 in this context"))
770 (if (or (= hour
0) (> hour
12))
771 (throw 'syntax
"Hour value out of range"))
772 (if (eq (= ampm ?A
) (= hour
12))
773 (setq hour
(%
(+ hour
12) 24)))))))
775 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
776 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str
)
778 (setq math-pd-str
(copy-sequence math-pd-str
))
779 (aset math-pd-str
(match-beginning 1) ?\
/)))
781 ;; Extract obvious month or weekday names.
782 (if (string-match "[a-zA-Z]" math-pd-str
)
784 (setq month
(math-parse-date-word math-long-month-names
))
785 (setq weekday
(math-parse-date-word math-long-weekday-names
))
786 (or month
(setq month
787 (math-parse-date-word math-short-month-names
)))
788 (or weekday
(math-parse-date-word math-short-weekday-names
))
790 (if (setq temp
(math-parse-date-word
791 '( "noon" "midnight" "mid" )))
792 (setq hour
(if (= temp
1) 12 0) minute
0 second
0)))
793 (or (math-parse-date-word '( "ad" "a.d." ))
794 (if (math-parse-date-word '( "bc" "b.c." ))
796 (if (string-match "[a-zA-Z]+" math-pd-str
)
797 (throw 'syntax
(format "Bad word in date: \"%s\""
798 (math-match-substring math-pd-str
0))))))
800 ;; If there is a huge number other than the year, ignore it.
801 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str
)
802 (setq temp
(concat (substring math-pd-str
0 (match-beginning 0))
803 (substring math-pd-str
(match-end 0))))
805 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp
))
806 (setq math-pd-str temp
))
808 ;; If there is a number with a sign or a large number, it is a year.
809 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str
)
810 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str
))
811 (setq year
(math-match-substring math-pd-str
1)
812 math-pd-str
(concat (substring math-pd-str
0 (match-beginning 1))
813 (substring math-pd-str
(match-end 1)))
814 year
(math-read-number year
)
817 ;; Collect remaining numbers.
819 (while (string-match "[0-9]+" math-pd-str temp
)
820 (and c
(throw 'syntax
"Too many numbers in date"))
821 (setq c
(string-to-number (math-match-substring math-pd-str
0)))
822 (or b
(setq b c c nil
))
823 (or a
(setq a b b nil
))
824 (setq temp
(match-end 0)))
826 ;; Check that we have the right amount of information.
827 (setq temp
(+ (if year
1 0) (if month
1 0) (if day
1 0)
828 (if a
1 0) (if b
1 0) (if c
1 0)))
830 (throw 'syntax
"Too many numbers in date")
831 (if (or (< temp
2) (and year
(= temp
2)))
832 (throw 'syntax
"Not enough numbers in date")
833 (if (= temp
2) ; if year omitted, assume current year
834 (setq year
(math-this-year)))))
836 ;; A large number must be a year.
838 (if (and a
(or (> a
31) (< a
1)))
839 (setq year a a b b c c nil
)
840 (if (and b
(or (> b
31) (< b
1)))
841 (setq year b b c c nil
)
842 (if (and c
(or (> c
31) (< c
1)))
843 (setq year c c nil
)))))
845 ;; A medium-large number must be a day.
848 (setq day a a b b c c nil
)
850 (setq day b b c c nil
)
852 (setq day c c nil
)))))
854 ;; We may know enough to sort it out now.
856 (or month
(setq month a
))
860 ;; Interpret order of numbers as same as for display format.
861 (setq temp calc-date-format
)
863 (cond ((not (symbolp (car temp
))))
864 ((memq (car temp
) '(Y YY BY YYY YYYY
))
865 (or year
(setq year a a b b c
)))
866 ((memq (car temp
) '(M MM BM mmm Mmm Mmmm MMM MMMM
))
867 (or month
(setq month a a b b c
)))
868 ((memq (car temp
) '(D DD BD
))
869 (or day
(setq day a a b b c
))))
870 (setq temp
(cdr temp
)))
872 ;; If display format was not complete, assume American style.
873 (or month
(setq month a a b b c
))
874 (or day
(setq day a a b b c
))
875 (or year
(setq year a a b b c
))))
878 (setq year
(math-neg (math-abs year
))))
880 (math-parse-date-validate year bigyear month day
881 hour minute second
)))))
883 (defun math-parse-date-validate (year bigyear month day hour minute second
)
884 (and (not bigyear
) (natnump year
) (< year
100)
885 (setq year
(+ year
(if (< year
40) 2000 1900))))
887 (throw 'syntax
"Year value is out of range"))
888 (if (or (< month
1) (> month
12))
889 (throw 'syntax
"Month value is out of range"))
890 (if (or (< day
1) (> day
(math-days-in-month year month
)))
891 (throw 'syntax
"Day value is out of range"))
894 (if (or (< hour
0) (> hour
23))
895 (throw 'syntax
"Hour value is out of range"))
896 (if (or (< minute
0) (> minute
59))
897 (throw 'syntax
"Minute value is out of range"))
898 (if (or (math-negp second
) (not (Math-lessp second
60)))
899 (throw 'syntax
"Seconds value is out of range"))))
900 (list 'date
(math-dt-to-date (append (list year month day
)
901 (and hour
(list hour minute second
))))))
903 (defun math-parse-date-word (names &optional front
)
905 (while (and names
(not (string-match (if (equal (car names
) "Sep")
907 (regexp-quote (car names
)))
909 (setq names
(cdr names
)
912 (or (not front
) (= (match-beginning 0) 0))
914 (setq math-pd-str
(concat (substring math-pd-str
0 (match-beginning 0))
916 (substring math-pd-str
(match-end 0))))
919 (defun math-parse-standard-date (math-pd-str with-time
)
920 (let ((case-fold-search t
)
922 (fmt calc-date-format
) this next
(gnext nil
)
923 (year nil
) (month nil
) (day nil
) (bigyear nil
) (yearday nil
)
924 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
))
925 (while (and fmt okay
)
927 fmt
(setq fmt
(or (cdr fmt
)
932 (if (consp next
) (setq next
(car next
)))
933 (or (cond ((listp this
)
939 (if (and (<= (length this
) (length math-pd-str
))
941 (substring math-pd-str
0 (length this
))))
942 (setq math-pd-str
(substring math-pd-str
(length this
)))))
945 ((memq this
'(n N j J
))
946 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str
)
947 (setq num
(math-match-substring math-pd-str
0)
948 math-pd-str
(substring math-pd-str
(match-end 0))
949 num
(math-date-to-dt (math-read-number num
))
951 (if (memq this
'(n N
))
955 math-julian-date-beginning-int
956 math-julian-date-beginning
)))
957 hour
(or (nth 3 num
) hour
)
958 minute
(or (nth 4 num
) minute
)
959 second
(or (nth 5 num
) second
)
964 (and (string-match "\\`[-+]?[0-9]+" math-pd-str
)
965 (setq num
(math-match-substring math-pd-str
0)
966 math-pd-str
(substring math-pd-str
(match-end 0))
969 (math-div (math-read-number num
)
977 ((memq this
'(mmm Mmm MMM
))
978 (setq month
(math-parse-date-word math-short-month-names t
)))
979 ((memq this
'(Mmmm MMMM
))
980 (setq month
(math-parse-date-word math-long-month-names t
)))
981 ((memq this
'(www Www WWW
))
982 (math-parse-date-word math-short-weekday-names t
))
983 ((memq this
'(Wwww WWWW
))
984 (math-parse-date-word math-long-weekday-names t
))
986 (if (string-match "\\`a" math-pd-str
)
987 (setq hour
(if (= hour
12) 0 hour
)
988 math-pd-str
(substring math-pd-str
1))
989 (if (string-match "\\`p" math-pd-str
)
990 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
991 math-pd-str
(substring math-pd-str
1)))))
992 ((memq this
'(pp PP pppp PPPP
))
993 (if (string-match "\\`am\\|a\\.m\\." math-pd-str
)
994 (setq hour
(if (= hour
12) 0 hour
)
995 math-pd-str
(substring math-pd-str
(match-end 0)))
996 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str
)
997 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
998 math-pd-str
(substring math-pd-str
(match-end 0))))))
999 ((memq this
'(Y YY BY YYY YYYY
))
1000 (and (if (memq next
'(MM DD ddd hh HH mm ss SS
))
1001 (if (memq this
'(Y YY BYY
))
1002 (string-match "\\` *[0-9][0-9]" math-pd-str
)
1003 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str
))
1004 (string-match "\\`[-+]?[0-9]+" math-pd-str
))
1005 (setq year
(math-match-substring math-pd-str
0)
1006 bigyear
(or (eq this
'YYY
)
1007 (memq (aref math-pd-str
0) '(?\
+ ?\-
)))
1008 math-pd-str
(substring math-pd-str
(match-end 0))
1009 year
(math-read-number year
))))
1012 ((memq this
'(aa AA aaaa AAAA
))
1013 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str
)
1014 (setq math-pd-str
(substring math-pd-str
(match-end 0)))))
1015 ((memq this
'(aaa AAA
))
1016 (if (string-match "\\` *ad *" math-pd-str
)
1017 (setq math-pd-str
(substring math-pd-str
(match-end 0)))))
1018 ((memq this
'(bb BB bbb BBB bbbb BBBB
))
1019 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str
)
1020 (setq math-pd-str
(substring math-pd-str
(match-end 0))
1022 ((memq this
'(s ss bs SS BS
))
1023 (and (if (memq next
'(YY YYYY MM DD hh HH mm
))
1024 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str
)
1025 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str
))
1026 (setq second
(math-match-substring math-pd-str
0)
1027 math-pd-str
(substring math-pd-str
(match-end 0))
1028 second
(math-read-number second
))))
1030 (if (string-match "\\`:[0-9][0-9]" math-pd-str
)
1031 (setq math-pd-str
(substring math-pd-str
1))
1033 ((or (not (if (and (memq this
'(ddd MM DD hh HH mm
))
1034 (memq next
'(YY YYYY MM DD ddd
1037 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str
)
1038 (string-match "\\` *[0-9][0-9]" math-pd-str
))
1039 (string-match "\\` *[0-9]+" math-pd-str
)))
1040 (and (setq num
(string-to-number
1041 (math-match-substring math-pd-str
0))
1042 math-pd-str
(substring math-pd-str
(match-end 0)))
1046 (and (>= num
0) (< num
7)))
1047 ((memq this
'(d ddd bdd
))
1049 ((memq this
'(M MM BM
))
1051 ((memq this
'(D DD BD
))
1053 ((memq this
'(h hh bh H HH BH
))
1055 ((memq this
'(m mm bm
))
1061 (setq month
1 day
1)))
1062 (if (and okay
(equal math-pd-str
""))
1063 (and month day
(or (not (or hour minute second
))
1066 (or year
(setq year
(math-this-year)))
1067 (or second
(setq second
0))
1069 (setq year
(math-neg (math-abs year
))))
1070 (setq day
(math-parse-date-validate year bigyear month day
1071 hour minute second
))
1073 (setq day
(math-add day
(1- yearday
))))
1077 (defun calcFunc-now (&optional zone
)
1078 (let ((date (let ((calc-date-format nil
))
1079 (math-parse-date (current-time-string)))))
1082 (math-add date
(math-div (math-sub (calcFunc-tzone nil date
)
1083 (calcFunc-tzone zone date
))
1086 (calc-record-why "*Unable to interpret current date from system")
1087 (append (list 'calcFunc-now
) (and zone
(list zone
))))))
1089 (defun calcFunc-year (date)
1090 (car (math-date-to-dt date
)))
1092 (defun calcFunc-month (date)
1093 (nth 1 (math-date-to-dt date
)))
1095 (defun calcFunc-day (date)
1096 (nth 2 (math-date-to-dt date
)))
1098 (defun calcFunc-weekday (date)
1099 (if (eq (car-safe date
) 'date
)
1100 (setq date
(nth 1 date
)))
1101 (or (math-realp date
)
1102 (math-reject-arg date
'datep
))
1103 (math-mod (math-add (math-floor date
) 6) 7))
1105 (defun calcFunc-yearday (date)
1106 (let ((dt (math-date-to-dt date
)))
1107 (math-day-number (car dt
) (nth 1 dt
) (nth 2 dt
))))
1109 (defun calcFunc-hour (date)
1110 (if (eq (car-safe date
) 'hms
)
1112 (or (nth 3 (math-date-to-dt date
)) 0)))
1114 (defun calcFunc-minute (date)
1115 (if (eq (car-safe date
) 'hms
)
1117 (or (nth 4 (math-date-to-dt date
)) 0)))
1119 (defun calcFunc-second (date)
1120 (if (eq (car-safe date
) 'hms
)
1122 (or (nth 5 (math-date-to-dt date
)) 0)))
1124 (defun calcFunc-time (date)
1125 (let ((dt (math-date-to-dt date
)))
1127 (cons 'hms
(nthcdr 3 dt
))
1128 (list 'hms
0 0 0))))
1130 (defun calcFunc-date (date &optional month day hour minute second
)
1131 (and (math-messy-integerp month
) (setq month
(math-trunc month
)))
1132 (and month
(not (integerp month
)) (math-reject-arg month
'fixnump
))
1133 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1134 (and day
(not (integerp day
)) (math-reject-arg day
'fixnump
))
1135 (if (and (eq (car-safe hour
) 'hms
) (not minute
))
1136 (setq second
(nth 3 hour
)
1139 (and (math-messy-integerp hour
) (setq hour
(math-trunc hour
)))
1140 (and hour
(not (integerp hour
)) (math-reject-arg hour
'fixnump
))
1141 (and (math-messy-integerp minute
) (setq minute
(math-trunc minute
)))
1142 (and minute
(not (integerp minute
)) (math-reject-arg minute
'fixnump
))
1143 (and (math-messy-integerp second
) (setq second
(math-trunc second
)))
1144 (and second
(not (math-realp second
)) (math-reject-arg second
'realp
))
1147 (and (math-messy-integerp date
) (setq date
(math-trunc date
)))
1148 (and date
(not (math-integerp date
)) (math-reject-arg date
'integerp
))
1151 (list 'date
(math-dt-to-date (list date month day hour
1154 (list 'date
(math-dt-to-date (list date month day
))))
1155 (list 'date
(math-dt-to-date (list (math-this-year) date month
)))))
1156 (if (math-realp date
)
1158 (if (eq (car date
) 'date
)
1160 (math-reject-arg date
'datep
)))))
1162 (defun calcFunc-julian (date &optional zone
)
1163 (if (math-realp date
)
1164 (list 'date
(if (math-integerp date
)
1165 (math-sub date math-julian-date-beginning-int
)
1166 (setq date
(math-sub date math-julian-date-beginning
))
1167 (math-sub date
(math-div (calcFunc-tzone zone date
)
1169 (if (eq (car date
) 'date
)
1170 (math-add (nth 1 date
) (if (math-integerp (nth 1 date
))
1171 math-julian-date-beginning-int
1172 (math-add math-julian-date-beginning
1173 (math-div (calcFunc-tzone zone date
)
1175 (math-reject-arg date
'datep
))))
1177 (defun calcFunc-unixtime (date &optional zone
)
1178 (if (math-realp date
)
1180 (setq date
(math-add 719164 (math-div date
'(float 864 2))))
1181 (list 'date
(math-sub date
(math-div (calcFunc-tzone zone date
)
1183 (if (eq (car date
) 'date
)
1184 (math-add (nth 1 (math-date-parts (nth 1 date
) 719164))
1185 (calcFunc-tzone zone date
))
1186 (math-reject-arg date
'datep
))))
1189 ;;; Note: Longer names must appear before shorter names which are
1190 ;;; substrings of them.
1191 (defvar math-tzone-names
1193 ( "MEGT" -
1 "MET" "METDST" ) ; Middle Europe
1194 ( "METDST" -
1 -
1 ) ( "MET" -
1 0 )
1195 ( "MEGZ" -
1 "MEZ" "MESZ" ) ( "MEZ" -
1 0 ) ( "MESZ" -
1 -
1 )
1196 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1197 ( "WETDST" 0 -
1 ) ( "WET" 0 0 )
1198 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -
1 ) ; Britain
1199 ( "NGT" (float 35 -
1) "NST" "NDT" ) ; Newfoundland
1200 ( "NST" (float 35 -
1) 0 ) ( "NDT" (float 35 -
1) -
1 )
1201 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -
1 ) ; Atlantic
1202 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -
1 ) ; Eastern
1203 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -
1 ) ; Central
1204 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -
1 ) ; Mountain
1205 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -
1 ) ; Pacific
1206 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -
1 ) ; Yukon
1208 "No doc yet. See calc manual for now. ")
1210 (defvar var-TimeZone nil
)
1213 (defvar calendar-current-time-zone-cache
)
1215 (defvar math-calendar-tzinfo
1217 "Information about the timezone, retrieved from the calendar.")
1219 (defun math-get-calendar-tzinfo ()
1220 "Get information about the timezone from the calendar.
1221 The result should be a list of two items about the current time zone:
1222 first, the number of seconds difference from GMT
1223 second, the number of seconds offset for daylight savings."
1224 (if math-calendar-tzinfo
1225 math-calendar-tzinfo
1227 (let ((tzinfo (progn
1228 (calendar-current-time-zone)
1229 calendar-current-time-zone-cache
)))
1230 (setq math-calendar-tzinfo
1231 (list (* 60 (abs (nth 0 tzinfo
)))
1232 (* 60 (nth 1 tzinfo
)))))))
1234 (defun calcFunc-tzone (&optional zone date
)
1236 (cond ((math-realp zone
)
1237 (math-round (math-mul zone
3600)))
1238 ((eq (car zone
) 'hms
)
1239 (math-round (math-mul (math-from-hms zone
'deg
) 3600)))
1241 (math-add (calcFunc-tzone (nth 1 zone
) date
)
1242 (calcFunc-tzone (nth 2 zone
) date
)))
1244 (math-sub (calcFunc-tzone (nth 1 zone
) date
)
1245 (calcFunc-tzone (nth 2 zone
) date
)))
1246 ((eq (car zone
) 'var
)
1247 (let ((name (upcase (symbol-name (nth 1 zone
))))
1249 (if (setq found
(assoc name math-tzone-names
))
1250 (calcFunc-tzone (math-add (nth 1 found
)
1251 (if (integerp (nth 2 found
))
1254 (math-daylight-savings-adjust
1258 (if (equal name
"LOCAL")
1259 (calcFunc-tzone nil date
)
1260 (math-reject-arg zone
"*Unrecognized time zone name")))))
1261 (t (math-reject-arg zone
"*Expected a time zone")))
1262 (if (calc-var-value 'var-TimeZone
)
1263 (calcFunc-tzone (calc-var-value 'var-TimeZone
) date
)
1264 (let ((tzinfo (math-get-calendar-tzinfo)))
1266 (* (math-cal-daylight-savings-adjust date
) (nth 1 tzinfo
)))))))
1268 (defvar math-daylight-savings-hook
'math-std-daylight-savings
)
1270 (defun math-daylight-savings-adjust (date zone
&optional dt
)
1271 (or date
(setq date
(nth 1 (calcFunc-now))))
1273 (if (eq (car-safe date
) 'date
)
1276 (if (and date
(math-realp date
))
1277 (let ((zadj (assoc zone math-tzone-names
)))
1278 (if zadj
(setq bump -
1
1279 date
(math-sub date
(math-div (nth 1 zadj
)
1281 (math-reject-arg date
'datep
)))
1282 (setq date
(math-float date
))
1283 (or dt
(setq dt
(math-date-to-dt date
)))
1284 (and math-daylight-savings-hook
1285 (funcall math-daylight-savings-hook date dt zone bump
))))
1287 ;;; Based on part of dst-adjust-time in cal-dst.el
1288 ;;; For calcFunc-dst, when zone=nil
1289 (defun math-cal-daylight-savings-adjust (date)
1290 "Return -1 if DATE is using daylight saving, 0 otherwise."
1292 (unless date
(setq date
(calcFunc-now)))
1293 (let* ((dt (math-date-to-dt date
))
1298 (+ (nth 3 dt
) (/ (nth 4 dt
) 60.0)))
1303 (calendar-absolute-from-gregorian
1304 (list (nth 1 dt
) (nth 2 dt
) (nth 0 dt
)))
1305 (/ (round (* 60 time
)) 60.0 24.0))))
1306 (if (dst-in-effect rounded-abs-date
)
1310 (defun calcFunc-dsadj (date &optional zone
)
1312 (or (eq (car-safe zone
) 'var
)
1313 (math-reject-arg zone
"*Time zone variable expected"))
1314 (setq zone
(calc-var-value 'var-TimeZone
)))
1317 (setq zone
(and (eq (car-safe zone
) 'var
)
1318 (upcase (symbol-name (nth 1 zone
)))))
1319 (let ((zadj (assoc zone math-tzone-names
)))
1320 (or zadj
(math-reject-arg zone
"*Unrecognized time zone name"))
1321 (if (integerp (nth 2 zadj
))
1323 (math-daylight-savings-adjust date zone
))))
1324 (math-cal-daylight-savings-adjust date
)))
1326 ;; (defun calcFunc-dsadj (date &optional zone)
1328 ;; (or (eq (car-safe zone) 'var)
1329 ;; (math-reject-arg zone "*Time zone variable expected"))
1330 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1333 ;; (calc-var-value 'var-TimeZone)))))
1334 ;; (setq zone (and (eq (car-safe zone) 'var)
1335 ;; (upcase (symbol-name (nth 1 zone)))))
1336 ;; (let ((zadj (assoc zone math-tzone-names)))
1337 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1338 ;; (if (integerp (nth 2 zadj))
1340 ;; (math-daylight-savings-adjust date zone))))
1342 (defun calcFunc-tzconv (date z1 z2
)
1343 (if (math-realp date
)
1344 (nth 1 (calcFunc-tzconv (list 'date date
) z1 z2
))
1345 (calcFunc-unixtime (calcFunc-unixtime date z1
) z2
)))
1347 (defun math-std-daylight-savings (date dt zone bump
)
1348 "Standard North American daylight saving algorithm.
1349 Before 2007, this uses `math-std-daylight-savings-old', where
1350 daylight saving began on the first Sunday of April at 2 a.m.,
1351 and ended on the last Sunday of October at 2 a.m.
1352 As of 2007, this uses `math-std-daylight-savings-new', where
1353 daylight saving begins on the second Sunday of March at 2 a.m.,
1354 and ends on the first Sunday of November at 2 a.m."
1355 (if (< (car dt
) 2007)
1356 (math-std-daylight-savings-old date dt zone bump
)
1357 (math-std-daylight-savings-new date dt zone bump
)))
1359 (defun math-std-daylight-savings-new (date dt zone bump
)
1360 "Standard North American daylight saving algorithm as of 2007.
1361 This implements the rules for the U.S. and Canada.
1362 Daylight saving begins on the second Sunday of March at 2 a.m.,
1363 and ends on the first Sunday of November at 2 a.m."
1364 (cond ((< (nth 1 dt
) 3) 0)
1366 (let ((sunday (math-prev-weekday-in-month date dt
14 0)))
1367 (cond ((< (nth 2 dt
) sunday
) 0)
1368 ((= (nth 2 dt
) sunday
)
1369 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1371 ((< (nth 1 dt
) 11) -
1)
1373 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1374 (cond ((< (nth 2 dt
) sunday
) -
1)
1375 ((= (nth 2 dt
) sunday
)
1376 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1380 (defun math-std-daylight-savings-old (date dt zone bump
)
1381 "Standard North American daylight saving algorithm before 2007.
1382 This implements the rules for the U.S. and Canada.
1383 Daylight saving begins on the first Sunday of April at 2 a.m.,
1384 and ends on the last Sunday of October at 2 a.m."
1385 (cond ((< (nth 1 dt
) 4) 0)
1387 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1388 (cond ((< (nth 2 dt
) sunday
) 0)
1389 ((= (nth 2 dt
) sunday
)
1390 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1392 ((< (nth 1 dt
) 10) -
1)
1394 (let ((sunday (math-prev-weekday-in-month date dt
31 0)))
1395 (cond ((< (nth 2 dt
) sunday
) -
1)
1396 ((= (nth 2 dt
) sunday
)
1397 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1401 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1402 ;;; day of the given month.
1403 (defun math-prev-weekday-in-month (date dt day wday
)
1404 (or day
(setq day
(nth 2 dt
)))
1405 (if (> day
(math-days-in-month (car dt
) (nth 1 dt
)))
1406 (setq day
(math-days-in-month (car dt
) (nth 1 dt
))))
1407 (let ((zeroth (math-sub (math-floor date
) (nth 2 dt
))))
1408 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day
))) zeroth
)))
1410 (defun calcFunc-pwday (date &optional day weekday
)
1411 (if (eq (car-safe date
) 'date
)
1412 (setq date
(nth 1 date
)))
1413 (or (math-realp date
)
1414 (math-reject-arg date
'datep
))
1415 (if (math-messy-integerp day
) (setq day
(math-trunc day
)))
1416 (or (integerp day
) (math-reject-arg day
'fixnump
))
1417 (if (= day
0) (setq day
31))
1418 (and (or (< day
7) (> day
31)) (math-reject-arg day
'range
))
1419 (math-prev-weekday-in-month date
(math-date-to-dt date
) day
(or weekday
0)))
1422 (defun calcFunc-newweek (date &optional weekday
)
1423 (if (eq (car-safe date
) 'date
)
1424 (setq date
(nth 1 date
)))
1425 (or (math-realp date
)
1426 (math-reject-arg date
'datep
))
1427 (or weekday
(setq weekday
0))
1428 (and (math-messy-integerp weekday
) (setq weekday
(math-trunc weekday
)))
1429 (or (integerp weekday
) (math-reject-arg weekday
'fixnump
))
1430 (and (or (< weekday
0) (> weekday
6)) (math-reject-arg weekday
'range
))
1431 (setq date
(math-floor date
))
1432 (list 'date
(math-sub date
(calcFunc-weekday (math-sub date weekday
)))))
1434 (defun calcFunc-newmonth (date &optional day
)
1435 (or day
(setq day
1))
1436 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1437 (or (integerp day
) (math-reject-arg day
'fixnump
))
1438 (and (or (< day
0) (> day
31)) (math-reject-arg day
'range
))
1439 (let ((dt (math-date-to-dt date
)))
1440 (if (or (= day
0) (> day
(math-days-in-month (car dt
) (nth 1 dt
))))
1441 (setq day
(math-days-in-month (car dt
) (nth 1 dt
))))
1442 (and (eq (car dt
) 1752) (= (nth 1 dt
) 9)
1443 (if (>= day
14) (setq day
(- day
11))))
1444 (list 'date
(math-add (math-dt-to-date (list (car dt
) (nth 1 dt
) 1))
1447 (defun calcFunc-newyear (date &optional day
)
1448 (or day
(setq day
1))
1449 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1450 (or (integerp day
) (math-reject-arg day
'fixnump
))
1451 (let ((dt (math-date-to-dt date
)))
1452 (if (and (>= day
0) (<= day
366))
1453 (let ((max (if (eq (car dt
) 1752) 355
1454 (if (math-leap-year-p (car dt
)) 366 365))))
1455 (if (or (= day
0) (> day max
)) (setq day max
))
1456 (list 'date
(math-add (math-dt-to-date (list (car dt
) 1 1))
1458 (if (and (>= day -
12) (<= day -
1))
1459 (list 'date
(math-dt-to-date (list (car dt
) (- day
) 1)))
1460 (math-reject-arg day
'range
)))))
1462 (defun calcFunc-incmonth (date &optional step
)
1463 (or step
(setq step
1))
1464 (and (math-messy-integerp step
) (setq step
(math-trunc step
)))
1465 (or (math-integerp step
) (math-reject-arg step
'integerp
))
1466 (let* ((dt (math-date-to-dt date
))
1468 (month (math-add (1- (nth 1 dt
)) step
))
1469 (extra (calcFunc-idiv month
12))
1471 (setq month
(1+ (math-sub month
(math-mul extra
12)))
1472 year
(math-add year extra
)
1473 day
(min day
(math-days-in-month year month
)))
1474 (and (math-posp (car dt
)) (not (math-posp year
))
1475 (setq year
(math-sub year
1))) ; did we go past the year zero?
1476 (and (math-negp (car dt
)) (not (math-negp year
))
1477 (setq year
(math-add year
1)))
1478 (list 'date
(math-dt-to-date
1479 (cons year
(cons month
(cons day
(cdr (cdr (cdr dt
))))))))))
1481 (defun calcFunc-incyear (date &optional step
)
1482 (calcFunc-incmonth date
(math-mul (or step
1) 12)))
1486 (defun calcFunc-bsub (a b
)
1487 (or (eq (car-safe a
) 'date
)
1488 (math-reject-arg a
'datep
))
1489 (if (eq (car-safe b
) 'date
)
1490 (if (math-lessp (nth 1 a
) (nth 1 b
))
1491 (math-neg (calcFunc-bsub b a
))
1492 (math-setup-holidays b
)
1493 (let* ((da (math-to-business-day a
))
1494 (db (math-to-business-day b
)))
1495 (math-add (math-sub (car da
) (car db
))
1496 (if (and (cdr db
) (not (cdr da
))) 1 0))))
1497 (calcFunc-badd a
(math-neg b
))))
1499 (defvar math-holidays-cache nil
)
1500 (defvar math-holidays-cache-tag t
)
1501 (defun calcFunc-badd (a b
)
1502 (if (eq (car-safe b
) 'date
)
1503 (if (eq (car-safe a
) 'date
)
1504 (math-reject-arg nil
"*Invalid combination in date arithmetic")
1505 (calcFunc-badd b a
))
1506 (if (eq (car-safe a
) 'date
)
1510 (let* ((d (math-to-business-day a
))
1511 (bb (math-add (car d
)
1512 (if (and (cdr d
) (Math-posp b
))
1513 (math-sub b
1) b
))))
1514 (or (math-from-business-day bb
)
1515 (calcFunc-badd a b
))))
1516 (if (eq (car-safe b
) 'hms
)
1517 (let ((hours (nth 7 math-holidays-cache
)))
1518 (setq b
(math-div (math-from-hms b
'deg
) 24))
1520 (setq b
(math-div b
(cdr hours
))))
1521 (calcFunc-badd a b
))
1522 (math-reject-arg nil
"*Invalid combination in date arithmetic")))
1523 (math-reject-arg a
'datep
))))
1525 (defun calcFunc-holiday (a)
1526 (if (cdr (math-to-business-day a
)) 1 0))
1528 ;;; Compute the number of business days since Jan 1, 1 AD.
1530 (defun math-to-business-day (date &optional need-year
)
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 (let* ((day (math-floor date
))
1536 (time (math-sub date day
))
1537 (dt (math-date-to-dt day
))
1540 (or (not need-year
) (eq (car dt
) need-year
)
1541 (math-reject-arg (list 'date day
) "*Generated holiday has wrong year"))
1542 (math-setup-holidays date
)
1543 (let ((days (car math-holidays-cache
)))
1544 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1545 (setq delta
(1+ delta
)))
1546 (and days
(= day
(car days
))
1548 (let* ((weekdays (nth 3 math-holidays-cache
))
1549 (weeks (1- (/ (+ day
6) 7)))
1550 (wkday (- day
1 (* weeks
7))))
1551 (setq delta
(+ delta
(* weeks
(length weekdays
))))
1552 (while (and weekdays
(< (car weekdays
) wkday
))
1553 (setq weekdays
(cdr weekdays
)
1555 (and weekdays
(eq wkday
(car weekdays
))
1557 (let ((hours (nth 7 math-holidays-cache
)))
1560 (setq time
(math-div (math-sub time
(car hours
)) (cdr hours
)))
1561 (if (Math-lessp time
0) (setq time
0))
1562 (or (Math-lessp time
1)
1565 (math-div 1 (math-mul 86400 (cdr hours
)))))))))
1566 (cons (math-add (math-sub day delta
) time
) holiday
)))
1569 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1570 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1572 (defun math-from-business-day (num)
1573 (let* ((day (math-floor num
))
1574 (time (math-sub num day
)))
1576 (math-reject-arg nil
"*Date is outside valid range"))
1577 (math-setup-holidays)
1578 (let ((days (nth 1 math-holidays-cache
))
1580 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1581 (setq delta
(1+ delta
)))
1582 (setq day
(+ day delta
)))
1583 (let* ((weekdays (nth 3 math-holidays-cache
))
1584 (bweek (- 7 (length weekdays
)))
1585 (weeks (1- (/ (+ day
(1- bweek
)) bweek
)))
1586 (wkday (- day
1 (* weeks bweek
)))
1588 (setq day
(+ day
(* weeks
(length weekdays
))))
1589 (while (if (memq w weekdays
)
1591 (> (setq wkday
(1- wkday
)) 0))
1593 (let ((hours (nth 7 math-holidays-cache
)))
1595 (setq time
(math-add (math-mul time
(cdr hours
)) (car hours
)))))
1596 (and (not (math-setup-holidays day
))
1597 (list 'date
(math-add day time
))))))
1599 ;; The variable math-sh-year is local to math-setup-holidays
1600 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1601 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1602 (defvar math-sh-year
)
1604 (defun math-setup-holidays (&optional date
)
1605 (or (eq (calc-var-value 'var-Holidays
) math-holidays-cache-tag
)
1606 (let ((h (calc-var-value 'var-Holidays
))
1607 (wdnames '( (sun .
0) (mon .
1) (tue .
2) (wed .
3)
1608 (thu .
4) (fri .
5) (sat .
6) ))
1609 (days nil
) (weekdays nil
) (exprs nil
) (limit nil
) (hours nil
))
1610 (or (math-vectorp h
)
1611 (math-reject-arg h
"*Holidays variable must be a vector"))
1612 (while (setq h
(cdr h
))
1613 (cond ((or (and (eq (car-safe (car h
)) 'date
)
1614 (integerp (nth 1 (car h
))))
1615 (and (eq (car-safe (car h
)) 'intv
)
1616 (eq (car-safe (nth 2 (car h
))) 'date
))
1617 (eq (car-safe (car h
)) 'vec
))
1618 (setq days
(cons (car h
) days
)))
1619 ((and (eq (car-safe (car h
)) 'var
)
1620 (assq (nth 1 (car h
)) wdnames
))
1621 (setq weekdays
(cons (cdr (assq (nth 1 (car h
)) wdnames
))
1623 ((and (eq (car-safe (car h
)) 'intv
)
1624 (eq (car-safe (nth 2 (car h
))) 'hms
)
1625 (eq (car-safe (nth 3 (car h
))) 'hms
))
1628 (car h
) "*Only one hours interval allowed in Holidays"))
1629 (setq hours
(math-div (car h
) '(hms 24 0 0)))
1630 (if (or (Math-lessp (nth 2 hours
) 0)
1631 (Math-lessp 1 (nth 3 hours
)))
1633 (car h
) "*Hours interval out of range"))
1634 (setq hours
(cons (nth 2 hours
)
1635 (math-sub (nth 3 hours
) (nth 2 hours
))))
1636 (if (Math-zerop (cdr hours
))
1638 (car h
) "*Degenerate hours interval")))
1639 ((or (and (eq (car-safe (car h
)) 'intv
)
1640 (Math-integerp (nth 2 (car h
)))
1641 (Math-integerp (nth 3 (car h
))))
1642 (and (integerp (car h
))
1643 (> (car h
) 1900) (< (car h
) 2100)))
1646 (car h
) "*Only one limit allowed in Holidays"))
1647 (setq limit
(calcFunc-vint (car h
) '(intv 3 1 2737)))
1648 (if (equal limit
'(vec))
1649 (math-reject-arg (car h
) "*Limit is out of range")))
1650 ((or (math-expr-contains (car h
) '(var y var-y
))
1651 (math-expr-contains (car h
) '(var m var-m
)))
1652 (setq exprs
(cons (car h
) exprs
)))
1654 (car h
) "*Holidays must contain a vector of holidays"))))
1655 (if (= (length weekdays
) 7)
1656 (math-reject-arg nil
"*Too many weekend days"))
1657 (setq math-holidays-cache
(list (list -
1) ; 0: days list
1658 (list -
1) ; 1: inverse-days list
1661 (or limit
'(intv 3 1 2737))
1662 nil
; 5: (lo.hi) expanded years
1664 hours
) ; 7: business hours
1665 math-holidays-cache-tag
(calc-var-value 'var-Holidays
))))
1667 (let ((year (calcFunc-year date
))
1668 (limits (nth 5 math-holidays-cache
))
1670 (or (eq (calcFunc-in year
(nth 4 math-holidays-cache
)) 1)
1672 (or (eq (car-safe date
) 'date
) (setq date
(list 'date date
)))
1673 (math-reject-arg date
"*Date is outside valid range")))
1675 (let ((days (nth 6 math-holidays-cache
)))
1677 (let ((math-sh-year nil
)) ; see below
1678 (setcar (nthcdr 6 math-holidays-cache
) nil
)
1679 (math-setup-add-holidays (cons 'vec
(cdr days
)))
1680 (setcar (nthcdr 2 math-holidays-cache
) (car days
))))
1681 (cond ((not (nth 2 math-holidays-cache
))
1685 (setcar (nthcdr 5 math-holidays-cache
) (cons year year
))
1686 (math-setup-year-holidays year
)
1688 ((< year
(car limits
))
1689 (message "Computing holidays, %d .. %d"
1690 year
(1- (car limits
)))
1691 (calc-set-command-flag 'clear-message
)
1692 (while (< year
(car limits
))
1693 (setcar limits
(1- (car limits
)))
1694 (math-setup-year-holidays (car limits
)))
1696 ((> year
(cdr limits
))
1697 (message "Computing holidays, %d .. %d"
1698 (1+ (cdr limits
)) year
)
1699 (calc-set-command-flag 'clear-message
)
1700 (while (> year
(cdr limits
))
1701 (setcdr limits
(1+ (cdr limits
)))
1702 (math-setup-year-holidays (cdr limits
)))
1707 (or done
(setq math-holidays-cache-tag t
))))))
1709 (defun math-setup-year-holidays (math-sh-year)
1710 (let ((exprs (nth 2 math-holidays-cache
)))
1712 (let* ((var-y math-sh-year
)
1714 (expr (math-evaluate-expr (car exprs
))))
1715 (if (math-expr-contains expr
'(var m var-m
))
1717 (while (<= (setq var-m
(1+ var-m
)) 12)
1718 (math-setup-add-holidays (math-evaluate-expr expr
))))
1719 (math-setup-add-holidays expr
)))
1720 (setq exprs
(cdr exprs
)))))
1722 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
1723 (cond ((eq (car-safe days
) 'vec
)
1724 (while (setq days
(cdr days
))
1725 (math-setup-add-holidays (car days
))))
1726 ((eq (car-safe days
) 'intv
)
1727 (let ((day (math-ceiling (nth 2 days
))))
1728 (or (eq (calcFunc-in day days
) 1)
1729 (setq day
(math-add day
1)))
1730 (while (eq (calcFunc-in day days
) 1)
1731 (math-setup-add-holidays day
)
1732 (setq day
(math-add day
1)))))
1733 ((eq (car-safe days
) 'date
)
1734 (math-setup-add-holidays (nth 1 days
)))
1737 (let ((b (math-to-business-day days math-sh-year
)))
1738 (or (cdr b
) ; don't register holidays twice!
1739 (let ((prev (car math-holidays-cache
))
1740 (iprev (nth 1 math-holidays-cache
)))
1741 (while (and (cdr prev
) (< (nth 1 prev
) days
))
1742 (setq prev
(cdr prev
) iprev
(cdr iprev
)))
1743 (setcdr prev
(cons days
(cdr prev
)))
1744 (setcdr iprev
(cons (car b
) (cdr iprev
)))
1745 (while (setq iprev
(cdr iprev
))
1746 (setcar iprev
(1- (car iprev
))))))))
1748 (math-reject-arg (list 'date days
) "*Invalid holiday value"))
1750 (math-reject-arg days
"*Holiday formula failed to evaluate"))))
1757 ;;; Build a standard deviation form. [X X X]
1758 (defun math-make-sdev (x sigma
)
1759 (if (memq (car-safe x
) '(date mod sdev intv vec
))
1760 (math-reject-arg x
'realp
))
1761 (if (memq (car-safe sigma
) '(date mod sdev intv vec
))
1762 (math-reject-arg sigma
'realp
))
1763 (if (or (Math-negp sigma
) (memq (car-safe sigma
) '(cplx polar
)))
1764 (setq sigma
(math-abs sigma
)))
1765 (if (and (Math-zerop sigma
) (Math-scalarp x
))
1767 (list 'sdev x sigma
)))
1768 (defun calcFunc-sdev (x sigma
)
1769 (math-make-sdev x sigma
))
1775 (defun math-normalize-mod (a)
1776 (let ((n (math-normalize (nth 1 a
)))
1777 (m (math-normalize (nth 2 a
))))
1778 (if (and (math-anglep n
) (math-anglep m
) (math-posp m
))
1780 (math-normalize (list 'calcFunc-makemod n m
)))))
1782 ;;; Build a modulo form. [N R R]
1783 (defun math-make-mod (n m
)
1784 (setq calc-previous-modulo m
)
1786 (cond ((not (Math-anglep m
))
1787 (math-reject-arg m
'anglep
))
1788 ((not (math-posp m
))
1789 (math-reject-arg m
'posp
))
1791 (if (or (Math-negp n
)
1792 (not (Math-lessp n m
)))
1793 (list 'mod
(math-mod n m
) m
)
1795 ((memq (car n
) '(+ -
/ vec neg
))
1798 (mapcar (function (lambda (x) (math-make-mod x m
)))
1800 ((and (eq (car n
) '*) (Math-anglep (nth 1 n
)))
1801 (math-mul (math-make-mod (nth 1 n
) m
) (nth 2 n
)))
1802 ((memq (car n
) '(* ^ var calcFunc-subscr
))
1803 (math-mul (math-make-mod 1 m
) n
))
1804 (t (math-reject-arg n
'anglep
)))))
1805 (defun calcFunc-makemod (n m
)
1806 (math-make-mod n m
))
1810 ;;;; Interval forms.
1812 ;;; Build an interval form. [X S X X]
1813 (defun math-make-intv (mask lo hi
)
1814 (if (memq (car-safe lo
) '(cplx polar mod sdev intv vec
))
1815 (math-reject-arg lo
'realp
))
1816 (if (memq (car-safe hi
) '(cplx polar mod sdev intv vec
))
1817 (math-reject-arg hi
'realp
))
1818 (or (eq (eq (car-safe lo
) 'date
) (eq (car-safe hi
) 'date
))
1819 (math-reject-arg (if (eq (car-safe lo
) 'date
) hi lo
) 'datep
))
1820 (if (and (or (Math-realp lo
) (eq (car lo
) 'date
))
1821 (or (Math-realp hi
) (eq (car hi
) 'date
)))
1822 (let ((cmp (math-compare lo hi
)))
1826 (list 'intv mask lo hi
))
1829 (list 'intv
2 lo lo
)
1830 (list 'intv mask lo lo
))
1831 (list 'intv mask lo hi
))))
1832 (list 'intv mask lo hi
)))
1833 (defun calcFunc-intv (mask lo hi
)
1834 (if (math-messy-integerp mask
) (setq mask
(math-trunc mask
)))
1835 (or (natnump mask
) (math-reject-arg mask
'fixnatnump
))
1836 (or (<= mask
3) (math-reject-arg mask
'range
))
1837 (math-make-intv mask lo hi
))
1839 (defun math-sort-intv (mask lo hi
)
1840 (if (Math-lessp hi lo
)
1841 (math-make-intv (aref [0 2 1 3] mask
) hi lo
)
1842 (math-make-intv mask lo hi
)))
1847 (defun math-combine-intervals (a am b bm c cm d dm
)
1849 (if (= (setq res
(math-compare a c
)) 1)
1852 (setq am
(or am cm
))))
1853 (if (= (setq res
(math-compare b d
)) -
1)
1856 (setq bm
(or bm dm
))))
1857 (math-make-intv (+ (if am
2 0) (if bm
1 0)) a b
)))
1860 (defun math-div-mod (a b m
) ; [R R R R] (Returns nil if no solution)
1861 (and (Math-integerp a
) (Math-integerp b
) (Math-integerp m
)
1862 (let ((u1 1) (u3 b
) (v1 0) (v3 m
))
1863 (while (not (eq v3
0)) ; See Knuth sec 4.5.2, exercise 15
1864 (let* ((q (math-idivmod u3 v3
))
1865 (t1 (math-sub u1
(math-mul v1
(car q
)))))
1866 (setq u1 v1 u3 v3 v1 t1 v3
(cdr q
))))
1867 (let ((q (math-idivmod a u3
)))
1869 (math-mod (math-mul (car q
) u1
) m
))))))
1871 (defun math-mod-intv (a b
)
1872 (let* ((q1 (math-floor (math-div (nth 2 a
) b
)))
1873 (q2 (math-floor (math-div (nth 3 a
) b
)))
1874 (m1 (math-sub (nth 2 a
) (math-mul q1 b
)))
1875 (m2 (math-sub (nth 3 a
) (math-mul q2 b
))))
1876 (cond ((equal q1 q2
)
1877 (math-sort-intv (nth 1 a
) m1 m2
))
1878 ((and (math-equal-int (math-sub q2 q1
) 1)
1880 (memq (nth 1 a
) '(0 2)))
1881 (math-make-intv (nth 1 a
) m1 b
))
1883 (math-make-intv 2 0 b
)))))
1885 ;; The variables math-exp-str and math-exp-pos are local to
1886 ;; math-read-exprs in math-aent.el, but are used by
1887 ;; math-read-angle-brackets, which is called (indirectly) by
1889 (defvar math-exp-str
)
1890 (defvar math-exp-pos
)
1892 (defun math-read-angle-brackets ()
1893 (let* ((last (or (math-check-for-commas t
) (length math-exp-str
)))
1894 (str (substring math-exp-str math-exp-pos last
))
1896 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str
)
1897 (let ((str1 (substring str
0 (1- (match-end 0))))
1898 (str2 (substring str
(match-end 0)))
1899 (calc-hashes-used 0))
1900 (setq str1
(math-read-expr (concat "[" str1
"]")))
1901 (if (eq (car-safe str1
) 'error
)
1903 (setq str2
(math-read-expr str2
))
1904 (if (eq (car-safe str2
) 'error
)
1906 (append '(calcFunc-lambda) (cdr str1
) (list str2
)))))
1907 (if (string-match "#" str
)
1908 (let ((calc-hashes-used 0))
1909 (and (setq str
(math-read-expr str
))
1910 (if (eq (car-safe str
) 'error
)
1912 (append '(calcFunc-lambda)
1913 (calc-invent-args calc-hashes-used
)
1915 (math-parse-date str
)))))
1917 (throw 'syntax res
))
1918 (if (eq (car-safe res
) 'error
)
1919 (throw 'syntax
(nth 2 res
)))
1920 (setq math-exp-pos
(1+ last
))
1924 (provide 'calc-forms
)
1926 ;; arch-tag: a3d8f33b-9508-4043-8060-d02b8c9c750c
1927 ;;; calc-forms.el ends here