1 ;;; calc-forms.el --- data format conversion functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2004 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-forms () nil
)
41 (let ((time (current-time-string)))
42 (calc-enter-result 0 "time"
45 (string-to-int (substring time
11 13))
46 (string-to-int (substring time
14 16))
47 (string-to-int (substring time
17 19)))
48 (list 'hms
24 0 0))))))
50 (defun calc-to-hms (arg)
54 (if (eq calc-angle-mode
'rad
)
55 (calc-unary-op ">rad" 'calcFunc-rad arg
)
56 (calc-unary-op ">deg" 'calcFunc-deg arg
))
57 (calc-unary-op ">hms" 'calcFunc-hms arg
))))
59 (defun calc-from-hms (arg)
65 (defun calc-hms-notation (fmt)
66 (interactive "sHours-minutes-seconds format (hms, @ ' \", etc.): ")
68 (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt
)
70 (calc-change-mode 'calc-hms-format
71 (concat "%s" (math-match-substring fmt
1)
72 (math-match-substring fmt
2)
73 "%s" (math-match-substring fmt
3)
74 (math-match-substring fmt
4)
75 "%s" (math-match-substring fmt
5))
77 (setq-default calc-hms-format calc-hms-format
)) ; for minibuffer
78 (error "Bad hours-minutes-seconds format"))))
80 (defun calc-date-notation (fmt arg
)
81 (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP")
85 (if (string-match "\\` *[0-9] *\\'" fmt
)
86 (setq fmt
(nth (string-to-int fmt
) calc-standard-date-formats
)))
87 (or (string-match "[a-zA-Z]" fmt
)
88 (error "Bad date format specifier"))
90 (>= (setq arg
(prefix-numeric-value arg
)) 0)
92 (setq calc-standard-date-formats
93 (copy-sequence calc-standard-date-formats
))
94 (setcar (nthcdr arg calc-standard-date-formats
) fmt
))
95 (let ((case-fold-search nil
))
96 (and (not (string-match "<.*>" fmt
))
97 (string-match "\\`[^hHspP]*\\([^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*[bBhHmpPsS]+[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*\\)[^hHspP]*\\'" fmt
)
98 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
99 (regexp-quote (math-match-substring fmt
1))
100 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt
)
101 (setq fmt
(concat (substring fmt
0 (match-beginning 0))
103 (substring fmt
(match-beginning 0) (match-end 0))
105 (substring fmt
(match-end 0))))))
110 (let ((case-fold-search nil
))
111 (and (setq temp
(string-match ":[BS]S" fmt
))
113 (while (setq pos
(string-match "[<>a-zA-Z]" fmt
))
115 (setq lfmt
(cons (substring fmt
0 pos
) lfmt
)))
117 (cond ((= (aref fmt pos
) ?\
<)
118 (and time
(error "Nested <'s not allowed"))
119 (and lfmt
(setq fullfmt
(nconc lfmt fullfmt
)
122 ((= (aref fmt pos
) ?\
>)
123 (or time
(error "Misplaced > in format"))
124 (and lfmt
(setq fullfmt
(cons (nreverse lfmt
) fullfmt
)
128 (if (string-match "\\`[^a-zA-Z]*[bB][a-zA-Z]" fmt
)
129 (setq pos2
(1+ pos2
)))
130 (while (and (< pos2
(length fmt
))
131 (= (upcase (aref fmt pos2
))
132 (upcase (aref fmt
(1- pos2
)))))
133 (setq pos2
(1+ pos2
)))
134 (setq sym
(intern (substring fmt pos pos2
)))
135 (or (memq sym
'(Y YY BY YYY YYYY
136 aa AA aaa AAA aaaa AAAA
137 bb BB bbb BBB bbbb BBBB
138 M MM BM mmm Mmm Mmmm MMM MMMM
140 W www Www Wwww WWW WWWW
143 m mm bm s ss bss SS BS C
145 (and (eq sym
'X
) (not lfmt
) (not fullfmt
))
146 (error "Bad format code: %s" sym
))
147 (and (memq sym
'(bb BB bbb BBB bbbb BBBB
))
148 (setq lfmt
(cons 'b lfmt
)))
149 (setq lfmt
(cons sym lfmt
))))
150 (setq fmt
(substring fmt pos2
)))
152 (setq lfmt
(cons fmt lfmt
)))
154 (setq fullfmt
(cons (nreverse lfmt
) fullfmt
))
155 (setq fullfmt
(nconc lfmt fullfmt
))))
156 (calc-change-mode 'calc-date-format
(nreverse fullfmt
) t
))))
159 (defun calc-hms-mode ()
162 (calc-change-mode 'calc-angle-mode
'hms
)
163 (message "Angles measured in degrees-minutes-seconds")))
166 (defun calc-now (arg)
168 (calc-date-zero-args "now" 'calcFunc-now arg
))
170 (defun calc-date-part (arg)
171 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
172 (if (or (< arg
1) (> arg
9))
173 (error "Part code out of range"))
176 (nth arg
'(nil "year" "mnth" "day" "hour" "minu"
177 "sec" "wday" "yday" "hmst"))
178 (list (nth arg
'(nil calcFunc-year calcFunc-month
179 calcFunc-day calcFunc-hour
180 calcFunc-minute calcFunc-second
181 calcFunc-weekday calcFunc-yearday
185 (defun calc-date (arg)
187 (if (or (< arg
1) (> arg
6))
188 (error "Between one and six arguments are allowed"))
190 (calc-enter-result arg
"date" (cons 'calcFunc-date
(calc-top-list-n arg
)))))
192 (defun calc-julian (arg)
194 (calc-date-one-arg "juln" 'calcFunc-julian arg
))
196 (defun calc-unix-time (arg)
198 (calc-date-one-arg "unix" 'calcFunc-unixtime arg
))
200 (defun calc-time-zone (arg)
202 (calc-date-zero-args "zone" 'calcFunc-tzone arg
))
204 (defun calc-convert-time-zones (old &optional new
)
205 (interactive "sFrom time zone: ")
208 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv
(calc-top-list-n 3)))
209 (if (equal old
"") (setq old
"local"))
211 (setq new
(read-string (concat "From time zone: " old
213 (if (stringp old
) (setq old
(math-read-expr old
)))
214 (if (eq (car-safe old
) 'error
)
215 (error "Error in expression: %S" (nth 1 old
)))
216 (if (equal new
"") (setq new
"local"))
217 (if (stringp new
) (setq new
(math-read-expr new
)))
218 (if (eq (car-safe new
) 'error
)
219 (error "Error in expression: %S" (nth 1 new
)))
220 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
221 (calc-top-n 1) old new
)))))
223 (defun calc-new-week (arg)
225 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg
))
227 (defun calc-new-month (arg)
229 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg
))
231 (defun calc-new-year (arg)
233 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg
))
235 (defun calc-inc-month (arg)
237 (calc-date-one-arg "incm" 'calcFunc-incmonth arg
))
239 (defun calc-business-days-plus (arg)
242 (calc-binary-op "bus+" 'calcFunc-badd arg
)))
244 (defun calc-business-days-minus (arg)
247 (calc-binary-op "bus-" 'calcFunc-bsub arg
)))
249 (defun calc-date-zero-args (prefix func arg
)
252 (calc-enter-result 1 prefix
(list func
(calc-top-n 1)))
253 (calc-enter-result 0 prefix
(if arg
254 (list func
(prefix-numeric-value arg
))
257 (defun calc-date-one-arg (prefix func arg
)
260 (calc-enter-result 2 prefix
(cons func
(calc-top-list-n 2)))
261 (calc-enter-result 1 prefix
(if arg
262 (list func
(calc-top-n 1)
263 (prefix-numeric-value arg
))
264 (list func
(calc-top-n 1)))))))
267 ;;;; Hours-minutes-seconds forms.
269 (defun math-normalize-hms (a)
270 (let ((h (math-normalize (nth 1 a
)))
271 (m (math-normalize (nth 2 a
)))
272 (s (let ((calc-internal-prec (max (- calc-internal-prec
4) 3)))
273 (math-normalize (nth 3 a
)))))
277 (setq s
(math-add s -
60)
280 (setq m
(math-add m -
60)
282 (if (not (Math-lessp -
60 s
))
283 (setq s
(math-add s
60)
285 (if (not (Math-lessp -
60 m
))
286 (setq m
(math-add m
60)
289 (setq s
(math-add s
60)
292 (setq m
(math-add m
60)
294 (if (not (Math-lessp s
60))
295 (setq s
(math-add s -
60)
297 (if (not (Math-lessp m
60))
298 (setq m
(math-add m -
60)
300 (if (and (eq (car-safe s
) 'float
)
301 (<= (+ (math-numdigs (nth 1 s
)) (nth 2 s
))
302 (- 2 calc-internal-prec
)))
306 ;;; Convert A from ANG or current angular mode to HMS format.
307 (defun math-to-hms (a &optional ang
) ; [X R] [Public]
308 (cond ((eq (car-safe a
) 'hms
) a
)
309 ((eq (car-safe a
) 'sdev
)
310 (math-make-sdev (math-to-hms (nth 1 a
))
311 (math-to-hms (nth 2 a
))))
312 ((not (Math-numberp a
))
313 (list 'calcFunc-hms a
))
315 (math-neg (math-to-hms (math-neg a
) ang
)))
316 ((eq (or ang calc-angle-mode
) 'rad
)
317 (math-to-hms (math-div a
(math-pi-over-180)) 'deg
))
318 ((memq (car-safe a
) '(cplx polar
)) a
)
320 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
321 ; (math-normalize a)))
323 (let* ((b (math-mul a
3600))
324 (hm (math-trunc (math-div b
60)))
325 (hmd (math-idivmod hm
60)))
329 (math-sub b
(math-mul hm
60))))))))
330 (defun calcFunc-hms (h &optional m s
)
331 (or (Math-realp h
) (math-reject-arg h
'realp
))
333 (or (Math-realp m
) (math-reject-arg m
'realp
))
335 (or (Math-realp s
) (math-reject-arg s
'realp
))
336 (if (and (not (Math-lessp m
0)) (Math-lessp m
60)
337 (not (Math-lessp s
0)) (Math-lessp s
60))
338 (math-add (math-to-hms h
)
340 (math-to-hms (math-add h
341 (math-add (math-div (or m
0) 60)
342 (math-div (or s
0) 3600)))
345 ;;; Convert A from HMS format to ANG or current angular mode.
346 (defun math-from-hms (a &optional ang
) ; [R X] [Public]
347 (cond ((not (eq (car-safe a
) 'hms
))
350 (if (eq (car-safe a
) 'sdev
)
351 (math-make-sdev (math-from-hms (nth 1 a
) ang
)
352 (math-from-hms (nth 2 a
) ang
))
353 (if (eq (or ang calc-angle-mode
) 'rad
)
354 (list 'calcFunc-rad a
)
355 (list 'calcFunc-deg a
)))))
357 (math-neg (math-from-hms (math-neg a
) ang
)))
358 ((eq (or ang calc-angle-mode
) 'rad
)
359 (math-mul (math-from-hms a
'deg
) (math-pi-over-180)))
361 (math-add (math-div (math-add (math-div (nth 3 a
)
370 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
371 ;;; These versions are rewritten to use arbitrary-size integers.
372 ;;; The Julian calendar is used up to 9/2/1752, after which the Gregorian
373 ;;; calendar is used; the first day after 9/2/1752 is 9/14/1752.
375 ;;; A numerical date is the number of days since midnight on
376 ;;; the morning of January 1, 1 A.D. If the date is a non-integer,
377 ;;; it represents a specific date and time.
378 ;;; A "dt" is a list of the form, (year month day), corresponding to
379 ;;; an integer code, or (year month day hour minute second), corresponding
380 ;;; to a non-integer code.
382 (defun math-date-to-dt (value)
383 (if (eq (car-safe value
) 'date
)
384 (setq value
(nth 1 value
)))
385 (or (math-realp value
)
386 (math-reject-arg value
'datep
))
387 (let* ((parts (math-date-parts value
))
392 (year (math-quotient (math-add date
(if (Math-lessp date
711859)
393 365 ; for speed, we take
394 -
108)) ; >1950 as a special case
395 (if (math-negp value
) 366 365)))
396 ; this result may be an overestimate
398 (while (Math-lessp date
(setq temp
(math-absolute-from-date year
1 1)))
399 (setq year
(math-add year -
1)))
400 (if (eq year
0) (setq year -
1))
401 (setq date
(1+ (math-sub date temp
)))
402 (and (eq year
1752) (>= date
247)
403 (setq date
(+ date
11)))
404 (setq temp
(if (math-leap-year-p year
)
405 [1 32 61 92 122 153 183 214 245 275 306 336 999]
406 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
407 (while (>= date
(aref temp month
))
408 (setq month
(1+ month
)))
409 (setq day
(1+ (- date
(aref temp
(1- month
)))))
410 (if (math-integerp value
)
411 (list year month day
)
415 (math-add (% time
60) (nth 2 parts
))))))
417 (defun math-dt-to-date (dt)
418 (or (integerp (nth 1 dt
))
419 (math-reject-arg (nth 1 dt
) 'fixnump
))
420 (if (or (< (nth 1 dt
) 1) (> (nth 1 dt
) 12))
421 (math-reject-arg (nth 1 dt
) "Month value is out of range"))
422 (or (integerp (nth 2 dt
))
423 (math-reject-arg (nth 2 dt
) 'fixnump
))
424 (if (or (< (nth 2 dt
) 1) (> (nth 2 dt
) 31))
425 (math-reject-arg (nth 2 dt
) "Day value is out of range"))
426 (let ((date (math-absolute-from-date (car dt
) (nth 1 dt
) (nth 2 dt
))))
428 (math-add (math-float date
)
429 (math-div (math-add (+ (* (nth 3 dt
) 3600)
435 (defun math-date-parts (value &optional offset
)
436 (let* ((date (math-floor value
))
437 (time (math-round (math-mul (math-sub value
(or offset date
)) 86400)
438 (and (> calc-internal-prec
12)
439 (- calc-internal-prec
12))))
440 (ftime (math-floor time
)))
443 (math-sub time ftime
))))
446 (defun math-this-year ()
447 (string-to-int (substring (current-time-string) -
4)))
449 (defun math-leap-year-p (year)
450 (if (Math-lessp year
1752)
452 (= (math-imod (math-neg year
) 4) 1)
453 (= (math-imod year
4) 0))
454 (setq year
(math-imod year
400))
455 (or (and (= (% year
4) 0) (/= (% year
100) 0))
458 (defun math-days-in-month (year month
)
459 (if (and (= month
2) (math-leap-year-p year
))
461 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month
))))
463 (defun math-day-number (year month day
)
464 (let ((day-of-year (+ day
(* 31 (1- month
)))))
467 (setq day-of-year
(- day-of-year
(/ (+ 23 (* 4 month
)) 10)))
468 (if (math-leap-year-p year
)
469 (setq day-of-year
(1+ day-of-year
)))))
472 (and (= month
9) (>= day
14)))
473 (setq day-of-year
(- day-of-year
11)))
476 (defun math-absolute-from-date (year month day
)
477 (if (eq year
0) (setq year -
1))
478 (let ((yearm1 (math-sub year
1)))
479 (math-sub (math-add (math-day-number year month day
)
480 (math-add (math-mul 365 yearm1
)
482 (math-quotient yearm1
4)
484 (math-quotient (math-sub 3 year
)
486 (if (or (Math-lessp year
1753)
487 (and (eq year
1752) (<= month
9)))
489 (let ((correction (math-mul (math-quotient yearm1
100) 3)))
490 (let ((res (math-idivmod correction
4)))
491 (math-add (if (= (cdr res
) 0)
497 ;;; It is safe to redefine these in your .emacs file to use a different
500 (defvar math-long-weekday-names
'( "Sunday" "Monday" "Tuesday" "Wednesday"
501 "Thursday" "Friday" "Saturday" ))
502 (defvar math-short-weekday-names
'( "Sun" "Mon" "Tue" "Wed"
505 (defvar math-long-month-names
'( "January" "February" "March" "April"
506 "May" "June" "July" "August"
507 "September" "October" "November" "December" ))
508 (defvar math-short-month-names
'( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
509 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
512 (defvar math-format-date-cache nil
)
513 (defun math-format-date (date)
514 (if (eq (car-safe date
) 'date
)
515 (setq date
(nth 1 date
)))
516 (let ((entry (list date calc-internal-prec calc-date-format
)))
517 (or (cdr (assoc entry math-format-date-cache
))
519 (calc-group-digits nil
)
520 (calc-leading-zeros nil
)
521 (calc-number-radix 10)
522 year month day weekday hour minute second
524 (fmt (apply 'concat
(mapcar 'math-format-date-part
526 (setq math-format-date-cache
(cons (cons entry fmt
)
527 math-format-date-cache
))
528 (and (setq dt
(nthcdr 10 math-format-date-cache
))
532 (defun math-format-date-part (x)
536 (if (math-integerp date
)
538 (apply 'concat
(mapcar 'math-format-date-part x
))))
542 (math-format-number date
))
544 (math-format-number (math-floor date
)))
546 (math-format-number (math-add date
'(float (bigpos 235 214 17) -
1))))
548 (math-format-number (math-add (math-floor date
) '(bigpos 424 721 1))))
550 (math-format-number (nth 1 (math-date-parts date
719164))))
554 (setq dt
(math-date-to-dt date
)
558 weekday
(math-mod (math-add (math-floor date
) 6) 7)
562 (and (memq 'b calc-date-format
)
564 (setq year
(math-neg year
)
567 (if (and (integerp year
) (> year
1940) (< year
2040))
568 (format (cond ((eq x
'YY
) "%02d")
572 (if (and (natnump year
) (< year
100))
574 (math-format-number year
))))
576 (math-format-number year
))
578 (if (and (natnump year
) (< year
100))
580 (math-format-number year
)))
583 (and (not bc-flag
) "ad"))
585 (and (not bc-flag
) "AD"))
587 (and (not bc-flag
) "ad "))
589 (and (not bc-flag
) "AD "))
591 (and (not bc-flag
) "a.d."))
593 (and (not bc-flag
) "A.D."))
603 (and bc-flag
"b.c."))
605 (and bc-flag
"B.C."))
609 (format "%02d" month
))
611 (format "%2d" month
))
613 (downcase (nth (1- month
) math-short-month-names
)))
615 (nth (1- month
) math-short-month-names
))
617 (upcase (nth (1- month
) math-short-month-names
)))
619 (nth (1- month
) math-long-month-names
))
621 (upcase (nth (1- month
) math-long-month-names
)))
629 (format "%d" weekday
))
631 (downcase (nth weekday math-short-weekday-names
)))
633 (nth weekday math-short-weekday-names
))
635 (upcase (nth weekday math-short-weekday-names
)))
637 (nth weekday math-long-weekday-names
))
639 (upcase (nth weekday math-long-weekday-names
)))
641 (format "%d" (math-day-number year month day
)))
643 (format "%03d" (math-day-number year month day
)))
645 (format "%3d" (math-day-number year month day
)))
647 (and hour
(format "%d" hour
)))
649 (and hour
(format "%02d" hour
)))
651 (and hour
(format "%2d" hour
)))
653 (and hour
(format "%d" (1+ (%
(+ hour
11) 12)))))
655 (and hour
(format "%02d" (1+ (%
(+ hour
11) 12)))))
657 (and hour
(format "%2d" (1+ (%
(+ hour
11) 12)))))
659 (and hour
(if (< hour
12) "a" "p")))
661 (and hour
(if (< hour
12) "A" "P")))
663 (and hour
(if (< hour
12) "am" "pm")))
665 (and hour
(if (< hour
12) "AM" "PM")))
667 (and hour
(if (< hour
12) "a.m." "p.m.")))
669 (and hour
(if (< hour
12) "A.M." "P.M.")))
671 (and minute
(format "%d" minute
)))
673 (and minute
(format "%02d" minute
)))
675 (and minute
(format "%2d" minute
)))
677 (and second
(not (math-zerop second
))
679 ((memq x
'(s ss bs SS BS
))
681 (not (and (memq x
'(SS BS
)) (math-zerop second
)))
682 (if (integerp second
)
683 (format (cond ((memq x
'(ss SS
)) "%02d")
684 ((memq x
'(bs BS
)) "%2d")
687 (concat (if (Math-lessp second
10)
688 (cond ((memq x
'(ss SS
)) "0")
689 ((memq x
'(bs BS
)) " ")
692 (let ((calc-float-format
693 (list 'fix
(min (- 12 calc-internal-prec
)
695 (math-format-number second
))))))))
698 (defun math-parse-date (str)
700 (or (math-parse-standard-date str t
)
701 (math-parse-standard-date str nil
)
702 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" str
)
703 (list 'date
(math-read-number (math-match-substring str
1))))
704 (let ((case-fold-search t
)
705 (year nil
) (month nil
) (day nil
) (weekday nil
)
706 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
)
707 (a nil
) (b nil
) (c nil
) (bigyear nil
) temp
)
709 ;; Extract the time, if any.
710 (if (or (string-match "\\([0-9][0-9]?\\):\\([0-9][0-9]?\\)\\(:\\([0-9][0-9]?\\(\\.[0-9]+\\)?\\)\\)? *\\([ap]m?\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)?" str
)
711 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]m?\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" str
))
712 (let ((ampm (math-match-substring str
6)))
713 (setq hour
(string-to-int (math-match-substring str
1))
714 minute
(math-match-substring str
2)
715 second
(math-match-substring str
4)
716 str
(concat (substring str
0 (match-beginning 0))
717 (substring str
(match-end 0))))
718 (if (equal minute
"")
720 (setq minute
(string-to-int minute
)))
721 (if (equal second
"")
723 (setq second
(math-read-number second
)))
726 (throw 'syntax
"Hour value out of range"))
727 (setq ampm
(upcase (aref ampm
0)))
728 (if (memq ampm
'(?N ?M
))
729 (if (and (= hour
12) (= minute
0) (eq second
0))
730 (if (eq ampm ?M
) (setq hour
0))
732 "Time must be 12:00:00 in this context"))
733 (if (or (= hour
0) (> hour
12))
734 (throw 'syntax
"Hour value out of range"))
735 (if (eq (= ampm ?A
) (= hour
12))
736 (setq hour
(%
(+ hour
12) 24)))))))
738 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
739 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" str
)
741 (setq str
(copy-sequence str
))
742 (aset str
(match-beginning 1) ?\
/)))
744 ;; Extract obvious month or weekday names.
745 (if (string-match "[a-zA-Z]" str
)
747 (setq month
(math-parse-date-word math-long-month-names
))
748 (setq weekday
(math-parse-date-word math-long-weekday-names
))
749 (or month
(setq month
750 (math-parse-date-word math-short-month-names
)))
751 (or weekday
(math-parse-date-word math-short-weekday-names
))
753 (if (setq temp
(math-parse-date-word
754 '( "noon" "midnight" "mid" )))
755 (setq hour
(if (= temp
1) 12 0) minute
0 second
0)))
756 (or (math-parse-date-word '( "ad" "a.d." ))
757 (if (math-parse-date-word '( "bc" "b.c." ))
759 (if (string-match "[a-zA-Z]+" str
)
760 (throw 'syntax
(format "Bad word in date: \"%s\""
761 (math-match-substring str
0))))))
763 ;; If there is a huge number other than the year, ignore it.
764 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" str
)
765 (setq temp
(concat (substring str
0 (match-beginning 0))
766 (substring str
(match-end 0))))
767 (string-match "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp
))
770 ;; If there is a number with a sign or a large number, it is a year.
771 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" str
)
772 (string-match "\\(0*[1-9][0-9][0-9]+\\)" str
))
773 (setq year
(math-match-substring str
1)
774 str
(concat (substring str
0 (match-beginning 1))
775 (substring str
(match-end 1)))
776 year
(math-read-number year
)
779 ;; Collect remaining numbers.
781 (while (string-match "[0-9]+" str temp
)
782 (and c
(throw 'syntax
"Too many numbers in date"))
783 (setq c
(string-to-int (math-match-substring str
0)))
784 (or b
(setq b c c nil
))
785 (or a
(setq a b b nil
))
786 (setq temp
(match-end 0)))
788 ;; Check that we have the right amount of information.
789 (setq temp
(+ (if year
1 0) (if month
1 0) (if day
1 0)
790 (if a
1 0) (if b
1 0) (if c
1 0)))
792 (throw 'syntax
"Too many numbers in date")
793 (if (or (< temp
2) (and year
(= temp
2)))
794 (throw 'syntax
"Not enough numbers in date")
795 (if (= temp
2) ; if year omitted, assume current year
796 (setq year
(math-this-year)))))
798 ;; A large number must be a year.
800 (if (and a
(or (> a
31) (< a
1)))
801 (setq year a a b b c c nil
)
802 (if (and b
(or (> b
31) (< b
1)))
803 (setq year b b c c nil
)
804 (if (and c
(or (> c
31) (< c
1)))
805 (setq year c c nil
)))))
807 ;; A medium-large number must be a day.
810 (setq day a a b b c c nil
)
812 (setq day b b c c nil
)
814 (setq day c c nil
)))))
816 ;; We may know enough to sort it out now.
818 (or month
(setq month a
))
822 ;; Interpret order of numbers as same as for display format.
823 (setq temp calc-date-format
)
825 (cond ((not (symbolp (car temp
))))
826 ((memq (car temp
) '(Y YY BY YYY YYYY
))
827 (or year
(setq year a a b b c
)))
828 ((memq (car temp
) '(M MM BM mmm Mmm Mmmm MMM MMMM
))
829 (or month
(setq month a a b b c
)))
830 ((memq (car temp
) '(D DD BD
))
831 (or day
(setq day a a b b c
))))
832 (setq temp
(cdr temp
)))
834 ;; If display format was not complete, assume American style.
835 (or month
(setq month a a b b c
))
836 (or day
(setq day a a b b c
))
837 (or year
(setq year a a b b c
))))
840 (setq year
(math-neg (math-abs year
))))
842 (math-parse-date-validate year bigyear month day
843 hour minute second
)))))
845 (defun math-parse-date-validate (year bigyear month day hour minute second
)
846 (and (not bigyear
) (natnump year
) (< year
100)
847 (setq year
(+ year
(if (< year
40) 2000 1900))))
849 (throw 'syntax
"Year value is out of range"))
850 (if (or (< month
1) (> month
12))
851 (throw 'syntax
"Month value is out of range"))
852 (if (or (< day
1) (> day
(math-days-in-month year month
)))
853 (throw 'syntax
"Day value is out of range"))
856 (if (or (< hour
0) (> hour
23))
857 (throw 'syntax
"Hour value is out of range"))
858 (if (or (< minute
0) (> minute
59))
859 (throw 'syntax
"Minute value is out of range"))
860 (if (or (math-negp second
) (not (Math-lessp second
60)))
861 (throw 'syntax
"Seconds value is out of range"))))
862 (list 'date
(math-dt-to-date (append (list year month day
)
863 (and hour
(list hour minute second
))))))
865 (defun math-parse-date-word (names &optional front
)
867 (while (and names
(not (string-match (if (equal (car names
) "Sep")
869 (regexp-quote (car names
)))
871 (setq names
(cdr names
)
874 (or (not front
) (= (match-beginning 0) 0))
876 (setq str
(concat (substring str
0 (match-beginning 0))
878 (substring str
(match-end 0))))
881 (defun math-parse-standard-date (str with-time
)
882 (let ((case-fold-search t
)
884 (fmt calc-date-format
) this next
(gnext nil
)
885 (year nil
) (month nil
) (day nil
) (bigyear nil
) (yearday nil
)
886 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
))
887 (while (and fmt okay
)
889 fmt
(setq fmt
(or (cdr fmt
)
894 (if (consp next
) (setq next
(car next
)))
895 (or (cond ((listp this
)
901 (if (and (<= (length this
) (length str
))
903 (substring str
0 (length this
))))
904 (setq str
(substring str
(length this
)))))
907 ((memq this
'(n N j J
))
908 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" str
)
909 (setq num
(math-match-substring str
0)
910 str
(substring str
(match-end 0))
911 num
(math-date-to-dt (math-read-number num
))
913 (if (memq this
'(n N
))
918 '(float (bigpos 235 214 17)
920 hour
(or (nth 3 num
) hour
)
921 minute
(or (nth 4 num
) minute
)
922 second
(or (nth 5 num
) second
)
927 (and (string-match "\\`[-+]?[0-9]+" str
)
928 (setq num
(math-match-substring str
0)
929 str
(substring str
(match-end 0))
932 (math-div (math-read-number num
)
940 ((memq this
'(mmm Mmm MMM
))
941 (setq month
(math-parse-date-word math-short-month-names t
)))
942 ((memq this
'(Mmmm MMMM
))
943 (setq month
(math-parse-date-word math-long-month-names t
)))
944 ((memq this
'(www Www WWW
))
945 (math-parse-date-word math-short-weekday-names t
))
946 ((memq this
'(Wwww WWWW
))
947 (math-parse-date-word math-long-weekday-names t
))
949 (if (string-match "\\`a" str
)
950 (setq hour
(if (= hour
12) 0 hour
)
951 str
(substring str
1))
952 (if (string-match "\\`p" str
)
953 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
954 str
(substring str
1)))))
955 ((memq this
'(pp PP pppp PPPP
))
956 (if (string-match "\\`am\\|a\\.m\\." str
)
957 (setq hour
(if (= hour
12) 0 hour
)
958 str
(substring str
(match-end 0)))
959 (if (string-match "\\`pm\\|p\\.m\\." str
)
960 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
961 str
(substring str
(match-end 0))))))
962 ((memq this
'(Y YY BY YYY YYYY
))
963 (and (if (memq next
'(MM DD ddd hh HH mm ss SS
))
964 (if (memq this
'(Y YY BYY
))
965 (string-match "\\` *[0-9][0-9]" str
)
966 (string-match "\\`[0-9][0-9][0-9][0-9]" str
))
967 (string-match "\\`[-+]?[0-9]+" str
))
968 (setq year
(math-match-substring str
0)
969 bigyear
(or (eq this
'YYY
)
970 (memq (aref str
0) '(?\
+ ?\-
)))
971 str
(substring str
(match-end 0))
972 year
(math-read-number year
))))
975 ((memq this
'(aa AA aaaa AAAA
))
976 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" str
)
977 (setq str
(substring str
(match-end 0)))))
978 ((memq this
'(aaa AAA
))
979 (if (string-match "\\` *ad *" str
)
980 (setq str
(substring str
(match-end 0)))))
981 ((memq this
'(bb BB bbb BBB bbbb BBBB
))
982 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" str
)
983 (setq str
(substring str
(match-end 0))
985 ((memq this
'(s ss bs SS BS
))
986 (and (if (memq next
'(YY YYYY MM DD hh HH mm
))
987 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" str
)
988 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" str
))
989 (setq second
(math-match-substring str
0)
990 str
(substring str
(match-end 0))
991 second
(math-read-number second
))))
993 (if (string-match "\\`:[0-9][0-9]" str
)
994 (setq str
(substring str
1))
996 ((or (not (if (and (memq this
'(ddd MM DD hh HH mm
))
997 (memq next
'(YY YYYY MM DD ddd
1000 (string-match "\\` *[0-9][0-9][0-9]" str
)
1001 (string-match "\\` *[0-9][0-9]" str
))
1002 (string-match "\\` *[0-9]+" str
)))
1003 (and (setq num
(string-to-int
1004 (math-match-substring str
0))
1005 str
(substring str
(match-end 0)))
1009 (and (>= num
0) (< num
7)))
1010 ((memq this
'(d ddd bdd
))
1012 ((memq this
'(M MM BM
))
1014 ((memq this
'(D DD BD
))
1016 ((memq this
'(h hh bh H HH BH
))
1018 ((memq this
'(m mm bm
))
1024 (setq month
1 day
1)))
1025 (if (and okay
(equal str
""))
1026 (and month day
(or (not (or hour minute second
))
1029 (or year
(setq year
(math-this-year)))
1030 (or second
(setq second
0))
1032 (setq year
(math-neg (math-abs year
))))
1033 (setq day
(math-parse-date-validate year bigyear month day
1034 hour minute second
))
1036 (setq day
(math-add day
(1- yearday
))))
1040 (defun calcFunc-now (&optional zone
)
1041 (let ((date (let ((calc-date-format nil
))
1042 (math-parse-date (current-time-string)))))
1045 (math-add date
(math-div (math-sub (calcFunc-tzone nil date
)
1046 (calcFunc-tzone zone date
))
1049 (calc-record-why "*Unable to interpret current date from system")
1050 (append (list 'calcFunc-now
) (and zone
(list zone
))))))
1052 (defun calcFunc-year (date)
1053 (car (math-date-to-dt date
)))
1055 (defun calcFunc-month (date)
1056 (nth 1 (math-date-to-dt date
)))
1058 (defun calcFunc-day (date)
1059 (nth 2 (math-date-to-dt date
)))
1061 (defun calcFunc-weekday (date)
1062 (if (eq (car-safe date
) 'date
)
1063 (setq date
(nth 1 date
)))
1064 (or (math-realp date
)
1065 (math-reject-arg date
'datep
))
1066 (math-mod (math-add (math-floor date
) 6) 7))
1068 (defun calcFunc-yearday (date)
1069 (let ((dt (math-date-to-dt date
)))
1070 (math-day-number (car dt
) (nth 1 dt
) (nth 2 dt
))))
1072 (defun calcFunc-hour (date)
1073 (if (eq (car-safe date
) 'hms
)
1075 (or (nth 3 (math-date-to-dt date
)) 0)))
1077 (defun calcFunc-minute (date)
1078 (if (eq (car-safe date
) 'hms
)
1080 (or (nth 4 (math-date-to-dt date
)) 0)))
1082 (defun calcFunc-second (date)
1083 (if (eq (car-safe date
) 'hms
)
1085 (or (nth 5 (math-date-to-dt date
)) 0)))
1087 (defun calcFunc-time (date)
1088 (let ((dt (math-date-to-dt date
)))
1090 (cons 'hms
(nthcdr 3 dt
))
1091 (list 'hms
0 0 0))))
1093 (defun calcFunc-date (date &optional month day hour minute second
)
1094 (and (math-messy-integerp month
) (setq month
(math-trunc month
)))
1095 (and month
(not (integerp month
)) (math-reject-arg month
'fixnump
))
1096 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1097 (and day
(not (integerp day
)) (math-reject-arg day
'fixnump
))
1098 (if (and (eq (car-safe hour
) 'hms
) (not minute
))
1099 (setq second
(nth 3 hour
)
1102 (and (math-messy-integerp hour
) (setq hour
(math-trunc hour
)))
1103 (and hour
(not (integerp hour
)) (math-reject-arg hour
'fixnump
))
1104 (and (math-messy-integerp minute
) (setq minute
(math-trunc minute
)))
1105 (and minute
(not (integerp minute
)) (math-reject-arg minute
'fixnump
))
1106 (and (math-messy-integerp second
) (setq second
(math-trunc second
)))
1107 (and second
(not (math-realp second
)) (math-reject-arg second
'realp
))
1110 (and (math-messy-integerp date
) (setq date
(math-trunc date
)))
1111 (and date
(not (math-integerp date
)) (math-reject-arg date
'integerp
))
1114 (list 'date
(math-dt-to-date (list date month day hour
1117 (list 'date
(math-dt-to-date (list date month day
))))
1118 (list 'date
(math-dt-to-date (list (math-this-year) date month
)))))
1119 (if (math-realp date
)
1121 (if (eq (car date
) 'date
)
1123 (math-reject-arg date
'datep
)))))
1125 (defun calcFunc-julian (date &optional zone
)
1126 (if (math-realp date
)
1127 (list 'date
(if (math-integerp date
)
1128 (math-sub date
'(bigpos 424 721 1))
1129 (setq date
(math-sub date
'(float (bigpos 235 214 17) -
1)))
1130 (math-sub date
(math-div (calcFunc-tzone zone date
)
1132 (if (eq (car date
) 'date
)
1133 (math-add (nth 1 date
) (if (math-integerp (nth 1 date
))
1135 (math-add '(float (bigpos 235 214 17) -
1)
1136 (math-div (calcFunc-tzone zone date
)
1138 (math-reject-arg date
'datep
))))
1140 (defun calcFunc-unixtime (date &optional zone
)
1141 (if (math-realp date
)
1143 (setq date
(math-add 719164 (math-div date
'(float 864 2))))
1144 (list 'date
(math-sub date
(math-div (calcFunc-tzone zone date
)
1146 (if (eq (car date
) 'date
)
1147 (math-add (nth 1 (math-date-parts (nth 1 date
) 719164))
1148 (calcFunc-tzone zone date
))
1149 (math-reject-arg date
'datep
))))
1151 (defun calcFunc-tzone (&optional zone date
)
1153 (cond ((math-realp zone
)
1154 (math-round (math-mul zone
3600)))
1155 ((eq (car zone
) 'hms
)
1156 (math-round (math-mul (math-from-hms zone
'deg
) 3600)))
1158 (math-add (calcFunc-tzone (nth 1 zone
) date
)
1159 (calcFunc-tzone (nth 2 zone
) date
)))
1161 (math-sub (calcFunc-tzone (nth 1 zone
) date
)
1162 (calcFunc-tzone (nth 2 zone
) date
)))
1163 ((eq (car zone
) 'var
)
1164 (let ((name (upcase (symbol-name (nth 1 zone
))))
1166 (if (setq found
(assoc name math-tzone-names
))
1167 (calcFunc-tzone (math-add (nth 1 found
)
1168 (if (integerp (nth 2 found
))
1171 (math-daylight-savings-adjust
1175 (if (equal name
"LOCAL")
1176 (calcFunc-tzone nil date
)
1177 (math-reject-arg zone
"*Unrecognized time zone name")))))
1178 (t (math-reject-arg zone
"*Expected a time zone")))
1179 (if (calc-var-value 'var-TimeZone
)
1180 (calcFunc-tzone (calc-var-value 'var-TimeZone
) date
)
1181 (let ((p math-tzone-names
)
1183 (tz '(var error var-error
)))
1185 (set-buffer (get-buffer-create " *Calc Temporary*"))
1187 (call-process "date" nil t
)
1189 (let ((case-fold-search t
))
1190 (while (and p
(not (search-forward (car (car p
)) nil t
)))
1192 (if (looking-at "\\([-+][0-9]?[0-9]\\)\\([0-9][0-9]\\)?\\(\\'\\|[^0-9]\\)")
1193 (setq offset
(math-add
1194 (string-to-int (buffer-substring
1197 (if (match-beginning 2)
1198 (math-div (string-to-int (buffer-substring
1206 ;; Try to convert to a generalized time zone.
1207 (if (integerp (nth 2 p
))
1208 (let ((gen math-tzone-names
))
1210 (not (equal (nth 2 (car gen
)) (car p
)))
1211 (not (equal (nth 3 (car gen
)) (car p
)))
1212 (not (equal (nth 4 (car gen
)) (car p
)))
1213 (not (equal (nth 5 (car gen
)) (car p
))))
1214 (setq gen
(cdr gen
)))
1216 (setq gen
(car gen
))
1217 (equal (math-daylight-savings-adjust nil
(car gen
))
1220 (setq tz
(math-add (list 'var
1222 (intern (concat "var-" (car p
))))
1224 (kill-buffer " *Calc Temporary*")
1225 (setq var-TimeZone tz
)
1226 (calc-refresh-evaltos 'var-TimeZone
)
1227 (calcFunc-tzone tz date
)))))
1229 ;;; Note: Longer names must appear before shorter names which are
1230 ;;; substrings of them.
1231 (defvar math-tzone-names
1233 ( "MEGT" -
1 "MET" "METDST" ) ; Middle Europe
1234 ( "METDST" -
1 -
1 ) ( "MET" -
1 0 )
1235 ( "MEGZ" -
1 "MEZ" "MESZ" ) ( "MEZ" -
1 0 ) ( "MESZ" -
1 -
1 )
1236 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1237 ( "WETDST" 0 -
1 ) ( "WET" 0 0 )
1238 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -
1 ) ; Britain
1239 ( "NGT" (float 35 -
1) "NST" "NDT" ) ; Newfoundland
1240 ( "NST" (float 35 -
1) 0 ) ( "NDT" (float 35 -
1) -
1 )
1241 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -
1 ) ; Atlantic
1242 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -
1 ) ; Eastern
1243 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -
1 ) ; Central
1244 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -
1 ) ; Mountain
1245 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -
1 ) ; Pacific
1246 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -
1 ) ; Yukon
1248 "No doc yet. See calc manual for now. ")
1251 (defun math-daylight-savings-adjust (date zone
&optional dt
)
1252 (or date
(setq date
(nth 1 (calcFunc-now))))
1254 (if (eq (car-safe date
) 'date
)
1257 (if (and date
(math-realp date
))
1258 (let ((zadj (assoc zone math-tzone-names
)))
1259 (if zadj
(setq bump -
1
1260 date
(math-sub date
(math-div (nth 1 zadj
)
1262 (math-reject-arg date
'datep
)))
1263 (setq date
(math-float date
))
1264 (or dt
(setq dt
(math-date-to-dt date
)))
1265 (and math-daylight-savings-hook
1266 (funcall math-daylight-savings-hook date dt zone bump
))))
1268 (defun calcFunc-dsadj (date &optional zone
)
1270 (or (eq (car-safe zone
) 'var
)
1271 (math-reject-arg zone
"*Time zone variable expected"))
1272 (setq zone
(or (calc-var-value 'var-TimeZone
)
1275 (calc-var-value 'var-TimeZone
)))))
1276 (setq zone
(and (eq (car-safe zone
) 'var
)
1277 (upcase (symbol-name (nth 1 zone
)))))
1278 (let ((zadj (assoc zone math-tzone-names
)))
1279 (or zadj
(math-reject-arg zone
"*Unrecognized time zone name"))
1280 (if (integerp (nth 2 zadj
))
1282 (math-daylight-savings-adjust date zone
))))
1284 (defun calcFunc-tzconv (date z1 z2
)
1285 (if (math-realp date
)
1286 (nth 1 (calcFunc-tzconv (list 'date date
) z1 z2
))
1287 (calcFunc-unixtime (calcFunc-unixtime date z1
) z2
)))
1289 (defvar math-daylight-savings-hook
'math-std-daylight-savings
)
1291 (defun math-std-daylight-savings (date dt zone bump
)
1292 "Standard North American daylight savings algorithm.
1293 This implements the rules for the U.S. and Canada as of 1987.
1294 Daylight savings begins on the first Sunday of April at 2 a.m.,
1295 and ends on the last Sunday of October at 2 a.m."
1296 (cond ((< (nth 1 dt
) 4) 0)
1298 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1299 (cond ((< (nth 2 dt
) sunday
) 0)
1300 ((= (nth 2 dt
) sunday
)
1301 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1303 ((< (nth 1 dt
) 10) -
1)
1305 (let ((sunday (math-prev-weekday-in-month date dt
31 0)))
1306 (cond ((< (nth 2 dt
) sunday
) -
1)
1307 ((= (nth 2 dt
) sunday
)
1308 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1312 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1313 ;;; day of the given month.
1314 (defun math-prev-weekday-in-month (date dt day wday
)
1315 (or day
(setq day
(nth 2 dt
)))
1316 (if (> day
(math-days-in-month (car dt
) (nth 1 dt
)))
1317 (setq day
(math-days-in-month (car dt
) (nth 1 dt
))))
1318 (let ((zeroth (math-sub (math-floor date
) (nth 2 dt
))))
1319 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day
))) zeroth
)))
1321 (defun calcFunc-pwday (date &optional day weekday
)
1322 (if (eq (car-safe date
) 'date
)
1323 (setq date
(nth 1 date
)))
1324 (or (math-realp date
)
1325 (math-reject-arg date
'datep
))
1326 (if (math-messy-integerp day
) (setq day
(math-trunc day
)))
1327 (or (integerp day
) (math-reject-arg day
'fixnump
))
1328 (if (= day
0) (setq day
31))
1329 (and (or (< day
7) (> day
31)) (math-reject-arg day
'range
))
1330 (math-prev-weekday-in-month date
(math-date-to-dt date
) day
(or weekday
0)))
1333 (defun calcFunc-newweek (date &optional weekday
)
1334 (if (eq (car-safe date
) 'date
)
1335 (setq date
(nth 1 date
)))
1336 (or (math-realp date
)
1337 (math-reject-arg date
'datep
))
1338 (or weekday
(setq weekday
0))
1339 (and (math-messy-integerp weekday
) (setq weekday
(math-trunc weekday
)))
1340 (or (integerp weekday
) (math-reject-arg weekday
'fixnump
))
1341 (and (or (< weekday
0) (> weekday
6)) (math-reject-arg weekday
'range
))
1342 (setq date
(math-floor date
))
1343 (list 'date
(math-sub date
(calcFunc-weekday (math-sub date weekday
)))))
1345 (defun calcFunc-newmonth (date &optional day
)
1346 (or day
(setq day
1))
1347 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1348 (or (integerp day
) (math-reject-arg day
'fixnump
))
1349 (and (or (< day
0) (> day
31)) (math-reject-arg day
'range
))
1350 (let ((dt (math-date-to-dt date
)))
1351 (if (or (= day
0) (> day
(math-days-in-month (car dt
) (nth 1 dt
))))
1352 (setq day
(math-days-in-month (car dt
) (nth 1 dt
))))
1353 (and (eq (car dt
) 1752) (= (nth 1 dt
) 9)
1354 (if (>= day
14) (setq day
(- day
11))))
1355 (list 'date
(math-add (math-dt-to-date (list (car dt
) (nth 1 dt
) 1))
1358 (defun calcFunc-newyear (date &optional day
)
1359 (or day
(setq day
1))
1360 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1361 (or (integerp day
) (math-reject-arg day
'fixnump
))
1362 (let ((dt (math-date-to-dt date
)))
1363 (if (and (>= day
0) (<= day
366))
1364 (let ((max (if (eq (car dt
) 1752) 355
1365 (if (math-leap-year-p (car dt
)) 366 365))))
1366 (if (or (= day
0) (> day max
)) (setq day max
))
1367 (list 'date
(math-add (math-dt-to-date (list (car dt
) 1 1))
1369 (if (and (>= day -
12) (<= day -
1))
1370 (list 'date
(math-dt-to-date (list (car dt
) (- day
) 1)))
1371 (math-reject-arg day
'range
)))))
1373 (defun calcFunc-incmonth (date &optional step
)
1374 (or step
(setq step
1))
1375 (and (math-messy-integerp step
) (setq step
(math-trunc step
)))
1376 (or (math-integerp step
) (math-reject-arg step
'integerp
))
1377 (let* ((dt (math-date-to-dt date
))
1379 (month (math-add (1- (nth 1 dt
)) step
))
1380 (extra (calcFunc-idiv month
12))
1382 (setq month
(1+ (math-sub month
(math-mul extra
12)))
1383 year
(math-add year extra
)
1384 day
(min day
(math-days-in-month year month
)))
1385 (and (math-posp (car dt
)) (not (math-posp year
))
1386 (setq year
(math-sub year
1))) ; did we go past the year zero?
1387 (and (math-negp (car dt
)) (not (math-negp year
))
1388 (setq year
(math-add year
1)))
1389 (list 'date
(math-dt-to-date
1390 (cons year
(cons month
(cons day
(cdr (cdr (cdr dt
))))))))))
1392 (defun calcFunc-incyear (date &optional step
)
1393 (calcFunc-incmonth date
(math-mul (or step
1) 12)))
1397 (defun calcFunc-bsub (a b
)
1398 (or (eq (car-safe a
) 'date
)
1399 (math-reject-arg a
'datep
))
1400 (if (eq (car-safe b
) 'date
)
1401 (if (math-lessp (nth 1 a
) (nth 1 b
))
1402 (math-neg (calcFunc-bsub b a
))
1403 (math-setup-holidays b
)
1404 (let* ((da (math-to-business-day a
))
1405 (db (math-to-business-day b
)))
1406 (math-add (math-sub (car da
) (car db
))
1407 (if (and (cdr db
) (not (cdr da
))) 1 0))))
1408 (calcFunc-badd a
(math-neg b
))))
1410 (defvar math-holidays-cache nil
)
1411 (defvar math-holidays-cache-tag t
)
1412 (defun calcFunc-badd (a b
)
1413 (if (eq (car-safe b
) 'date
)
1414 (if (eq (car-safe a
) 'date
)
1415 (math-reject-arg nil
"*Illegal combination in date arithmetic")
1416 (calcFunc-badd b a
))
1417 (if (eq (car-safe a
) 'date
)
1421 (let* ((d (math-to-business-day a
))
1422 (bb (math-add (car d
)
1423 (if (and (cdr d
) (Math-posp b
))
1424 (math-sub b
1) b
))))
1425 (or (math-from-business-day bb
)
1426 (calcFunc-badd a b
))))
1427 (if (eq (car-safe b
) 'hms
)
1428 (let ((hours (nth 7 math-holidays-cache
)))
1429 (setq b
(math-div (math-from-hms b
'deg
) 24))
1431 (setq b
(math-div b
(cdr hours
))))
1432 (calcFunc-badd a b
))
1433 (math-reject-arg nil
"*Illegal combination in date arithmetic")))
1434 (math-reject-arg a
'datep
))))
1436 (defun calcFunc-holiday (a)
1437 (if (cdr (math-to-business-day a
)) 1 0))
1439 ;;; Compute the number of business days since Jan 1, 1 AD.
1441 (defun math-to-business-day (date &optional need-year
)
1442 (if (eq (car-safe date
) 'date
)
1443 (setq date
(nth 1 date
)))
1444 (or (Math-realp date
)
1445 (math-reject-arg date
'datep
))
1446 (let* ((day (math-floor date
))
1447 (time (math-sub date day
))
1448 (dt (math-date-to-dt day
))
1451 (or (not need-year
) (eq (car dt
) need-year
)
1452 (math-reject-arg (list 'date day
) "*Generated holiday has wrong year"))
1453 (math-setup-holidays date
)
1454 (let ((days (car math-holidays-cache
)))
1455 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1456 (setq delta
(1+ delta
)))
1457 (and days
(= day
(car days
))
1459 (let* ((weekdays (nth 3 math-holidays-cache
))
1460 (weeks (1- (/ (+ day
6) 7)))
1461 (wkday (- day
1 (* weeks
7))))
1462 (setq delta
(+ delta
(* weeks
(length weekdays
))))
1463 (while (and weekdays
(< (car weekdays
) wkday
))
1464 (setq weekdays
(cdr weekdays
)
1466 (and weekdays
(eq wkday
(car weekdays
))
1468 (let ((hours (nth 7 math-holidays-cache
)))
1471 (setq time
(math-div (math-sub time
(car hours
)) (cdr hours
)))
1472 (if (Math-lessp time
0) (setq time
0))
1473 (or (Math-lessp time
1)
1476 (math-div 1 (math-mul 86400 (cdr hours
)))))))))
1477 (cons (math-add (math-sub day delta
) time
) holiday
)))
1480 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1481 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1483 (defun math-from-business-day (num)
1484 (let* ((day (math-floor num
))
1485 (time (math-sub num day
)))
1487 (math-reject-arg nil
"*Date is outside valid range"))
1488 (math-setup-holidays)
1489 (let ((days (nth 1 math-holidays-cache
))
1491 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1492 (setq delta
(1+ delta
)))
1493 (setq day
(+ day delta
)))
1494 (let* ((weekdays (nth 3 math-holidays-cache
))
1495 (bweek (- 7 (length weekdays
)))
1496 (weeks (1- (/ (+ day
(1- bweek
)) bweek
)))
1497 (wkday (- day
1 (* weeks bweek
)))
1499 (setq day
(+ day
(* weeks
(length weekdays
))))
1500 (while (if (memq w weekdays
)
1502 (> (setq wkday
(1- wkday
)) 0))
1504 (let ((hours (nth 7 math-holidays-cache
)))
1506 (setq time
(math-add (math-mul time
(cdr hours
)) (car hours
)))))
1507 (and (not (math-setup-holidays day
))
1508 (list 'date
(math-add day time
))))))
1511 (defun math-setup-holidays (&optional date
)
1512 (or (eq (calc-var-value 'var-Holidays
) math-holidays-cache-tag
)
1513 (let ((h (calc-var-value 'var-Holidays
))
1514 (wdnames '( (sun .
0) (mon .
1) (tue .
2) (wed .
3)
1515 (thu .
4) (fri .
5) (sat .
6) ))
1516 (days nil
) (weekdays nil
) (exprs nil
) (limit nil
) (hours nil
))
1517 (or (math-vectorp h
)
1518 (math-reject-arg h
"*Holidays variable must be a vector"))
1519 (while (setq h
(cdr h
))
1520 (cond ((or (and (eq (car-safe (car h
)) 'date
)
1521 (integerp (nth 1 (car h
))))
1522 (and (eq (car-safe (car h
)) 'intv
)
1523 (eq (car-safe (nth 2 (car h
))) 'date
))
1524 (eq (car-safe (car h
)) 'vec
))
1525 (setq days
(cons (car h
) days
)))
1526 ((and (eq (car-safe (car h
)) 'var
)
1527 (assq (nth 1 (car h
)) wdnames
))
1528 (setq weekdays
(cons (cdr (assq (nth 1 (car h
)) wdnames
))
1530 ((and (eq (car-safe (car h
)) 'intv
)
1531 (eq (car-safe (nth 2 (car h
))) 'hms
)
1532 (eq (car-safe (nth 3 (car h
))) 'hms
))
1535 (car h
) "*Only one hours interval allowed in Holidays"))
1536 (setq hours
(math-div (car h
) '(hms 24 0 0)))
1537 (if (or (Math-lessp (nth 2 hours
) 0)
1538 (Math-lessp 1 (nth 3 hours
)))
1540 (car h
) "*Hours interval out of range"))
1541 (setq hours
(cons (nth 2 hours
)
1542 (math-sub (nth 3 hours
) (nth 2 hours
))))
1543 (if (Math-zerop (cdr hours
))
1545 (car h
) "*Degenerate hours interval")))
1546 ((or (and (eq (car-safe (car h
)) 'intv
)
1547 (Math-integerp (nth 2 (car h
)))
1548 (Math-integerp (nth 3 (car h
))))
1549 (and (integerp (car h
))
1550 (> (car h
) 1900) (< (car h
) 2100)))
1553 (car h
) "*Only one limit allowed in Holidays"))
1554 (setq limit
(calcFunc-vint (car h
) '(intv 3 1 2737)))
1555 (if (equal limit
'(vec))
1556 (math-reject-arg (car h
) "*Limit is out of range")))
1557 ((or (math-expr-contains (car h
) '(var y var-y
))
1558 (math-expr-contains (car h
) '(var m var-m
)))
1559 (setq exprs
(cons (car h
) exprs
)))
1561 (car h
) "*Holidays must contain a vector of holidays"))))
1562 (if (= (length weekdays
) 7)
1563 (math-reject-arg nil
"*Too many weekend days"))
1564 (setq math-holidays-cache
(list (list -
1) ; 0: days list
1565 (list -
1) ; 1: inverse-days list
1568 (or limit
'(intv 3 1 2737))
1569 nil
; 5: (lo.hi) expanded years
1571 hours
) ; 7: business hours
1572 math-holidays-cache-tag
(calc-var-value 'var-Holidays
))))
1574 (let ((year (calcFunc-year date
))
1575 (limits (nth 5 math-holidays-cache
))
1577 (or (eq (calcFunc-in year
(nth 4 math-holidays-cache
)) 1)
1579 (or (eq (car-safe date
) 'date
) (setq date
(list 'date date
)))
1580 (math-reject-arg date
"*Date is outside valid range")))
1582 (let ((days (nth 6 math-holidays-cache
)))
1584 (let ((year nil
)) ; see below
1585 (setcar (nthcdr 6 math-holidays-cache
) nil
)
1586 (math-setup-add-holidays (cons 'vec
(cdr days
)))
1587 (setcar (nthcdr 2 math-holidays-cache
) (car days
))))
1588 (cond ((not (nth 2 math-holidays-cache
))
1592 (setcar (nthcdr 5 math-holidays-cache
) (cons year year
))
1593 (math-setup-year-holidays year
)
1595 ((< year
(car limits
))
1596 (message "Computing holidays, %d .. %d"
1597 year
(1- (car limits
)))
1598 (calc-set-command-flag 'clear-message
)
1599 (while (< year
(car limits
))
1600 (setcar limits
(1- (car limits
)))
1601 (math-setup-year-holidays (car limits
)))
1603 ((> year
(cdr limits
))
1604 (message "Computing holidays, %d .. %d"
1605 (1+ (cdr limits
)) year
)
1606 (calc-set-command-flag 'clear-message
)
1607 (while (> year
(cdr limits
))
1608 (setcdr limits
(1+ (cdr limits
)))
1609 (math-setup-year-holidays (cdr limits
)))
1614 (or done
(setq math-holidays-cache-tag t
))))))
1616 (defun math-setup-year-holidays (year)
1617 (let ((exprs (nth 2 math-holidays-cache
)))
1621 (expr (math-evaluate-expr (car exprs
))))
1622 (if (math-expr-contains expr
'(var m var-m
))
1624 (while (<= (setq var-m
(1+ var-m
)) 12)
1625 (math-setup-add-holidays (math-evaluate-expr expr
))))
1626 (math-setup-add-holidays expr
)))
1627 (setq exprs
(cdr exprs
)))))
1629 (defun math-setup-add-holidays (days) ; uses "year"
1630 (cond ((eq (car-safe days
) 'vec
)
1631 (while (setq days
(cdr days
))
1632 (math-setup-add-holidays (car days
))))
1633 ((eq (car-safe days
) 'intv
)
1634 (let ((day (math-ceiling (nth 2 days
))))
1635 (or (eq (calcFunc-in day days
) 1)
1636 (setq day
(math-add day
1)))
1637 (while (eq (calcFunc-in day days
) 1)
1638 (math-setup-add-holidays day
)
1639 (setq day
(math-add day
1)))))
1640 ((eq (car-safe days
) 'date
)
1641 (math-setup-add-holidays (nth 1 days
)))
1644 (let ((b (math-to-business-day days year
)))
1645 (or (cdr b
) ; don't register holidays twice!
1646 (let ((prev (car math-holidays-cache
))
1647 (iprev (nth 1 math-holidays-cache
)))
1648 (while (and (cdr prev
) (< (nth 1 prev
) days
))
1649 (setq prev
(cdr prev
) iprev
(cdr iprev
)))
1650 (setcdr prev
(cons days
(cdr prev
)))
1651 (setcdr iprev
(cons (car b
) (cdr iprev
)))
1652 (while (setq iprev
(cdr iprev
))
1653 (setcar iprev
(1- (car iprev
))))))))
1655 (math-reject-arg (list 'date days
) "*Invalid holiday value"))
1657 (math-reject-arg days
"*Holiday formula failed to evaluate"))))
1664 ;;; Build a standard deviation form. [X X X]
1665 (defun math-make-sdev (x sigma
)
1666 (if (memq (car-safe x
) '(date mod sdev intv vec
))
1667 (math-reject-arg x
'realp
))
1668 (if (memq (car-safe sigma
) '(date mod sdev intv vec
))
1669 (math-reject-arg sigma
'realp
))
1670 (if (or (Math-negp sigma
) (memq (car-safe sigma
) '(cplx polar
)))
1671 (setq sigma
(math-abs sigma
)))
1672 (if (and (Math-zerop sigma
) (Math-scalarp x
))
1674 (list 'sdev x sigma
)))
1675 (defun calcFunc-sdev (x sigma
)
1676 (math-make-sdev x sigma
))
1682 (defun math-normalize-mod (a)
1683 (let ((n (math-normalize (nth 1 a
)))
1684 (m (math-normalize (nth 2 a
))))
1685 (if (and (math-anglep n
) (math-anglep m
) (math-posp m
))
1687 (math-normalize (list 'calcFunc-makemod n m
)))))
1689 ;;; Build a modulo form. [N R R]
1690 (defun math-make-mod (n m
)
1691 (setq calc-previous-modulo m
)
1693 (cond ((not (Math-anglep m
))
1694 (math-reject-arg m
'anglep
))
1695 ((not (math-posp m
))
1696 (math-reject-arg m
'posp
))
1698 (if (or (Math-negp n
)
1699 (not (Math-lessp n m
)))
1700 (list 'mod
(math-mod n m
) m
)
1702 ((memq (car n
) '(+ -
/ vec neg
))
1705 (mapcar (function (lambda (x) (math-make-mod x m
)))
1707 ((and (eq (car n
) '*) (Math-anglep (nth 1 n
)))
1708 (math-mul (math-make-mod (nth 1 n
) m
) (nth 2 n
)))
1709 ((memq (car n
) '(* ^ var calcFunc-subscr
))
1710 (math-mul (math-make-mod 1 m
) n
))
1711 (t (math-reject-arg n
'anglep
)))))
1712 (defun calcFunc-makemod (n m
)
1713 (math-make-mod n m
))
1717 ;;;; Interval forms.
1719 ;;; Build an interval form. [X S X X]
1720 (defun math-make-intv (mask lo hi
)
1721 (if (memq (car-safe lo
) '(cplx polar mod sdev intv vec
))
1722 (math-reject-arg lo
'realp
))
1723 (if (memq (car-safe hi
) '(cplx polar mod sdev intv vec
))
1724 (math-reject-arg hi
'realp
))
1725 (or (eq (eq (car-safe lo
) 'date
) (eq (car-safe hi
) 'date
))
1726 (math-reject-arg (if (eq (car-safe lo
) 'date
) hi lo
) 'datep
))
1727 (if (and (or (Math-realp lo
) (eq (car lo
) 'date
))
1728 (or (Math-realp hi
) (eq (car hi
) 'date
)))
1729 (let ((cmp (math-compare lo hi
)))
1733 (list 'intv mask lo hi
))
1736 (list 'intv
2 lo lo
)
1737 (list 'intv mask lo lo
))
1738 (list 'intv mask lo hi
))))
1739 (list 'intv mask lo hi
)))
1740 (defun calcFunc-intv (mask lo hi
)
1741 (if (math-messy-integerp mask
) (setq mask
(math-trunc mask
)))
1742 (or (natnump mask
) (math-reject-arg mask
'fixnatnump
))
1743 (or (<= mask
3) (math-reject-arg mask
'range
))
1744 (math-make-intv mask lo hi
))
1746 (defun math-sort-intv (mask lo hi
)
1747 (if (Math-lessp hi lo
)
1748 (math-make-intv (aref [0 2 1 3] mask
) hi lo
)
1749 (math-make-intv mask lo hi
)))
1754 (defun math-combine-intervals (a am b bm c cm d dm
)
1756 (if (= (setq res
(math-compare a c
)) 1)
1759 (setq am
(or am cm
))))
1760 (if (= (setq res
(math-compare b d
)) -
1)
1763 (setq bm
(or bm dm
))))
1764 (math-make-intv (+ (if am
2 0) (if bm
1 0)) a b
)))
1767 (defun math-div-mod (a b m
) ; [R R R R] (Returns nil if no solution)
1768 (and (Math-integerp a
) (Math-integerp b
) (Math-integerp m
)
1769 (let ((u1 1) (u3 b
) (v1 0) (v3 m
))
1770 (while (not (eq v3
0)) ; See Knuth sec 4.5.2, exercise 15
1771 (let* ((q (math-idivmod u3 v3
))
1772 (t1 (math-sub u1
(math-mul v1
(car q
)))))
1773 (setq u1 v1 u3 v3 v1 t1 v3
(cdr q
))))
1774 (let ((q (math-idivmod a u3
)))
1776 (math-mod (math-mul (car q
) u1
) m
))))))
1778 (defun math-mod-intv (a b
)
1779 (let* ((q1 (math-floor (math-div (nth 2 a
) b
)))
1780 (q2 (math-floor (math-div (nth 3 a
) b
)))
1781 (m1 (math-sub (nth 2 a
) (math-mul q1 b
)))
1782 (m2 (math-sub (nth 3 a
) (math-mul q2 b
))))
1783 (cond ((equal q1 q2
)
1784 (math-sort-intv (nth 1 a
) m1 m2
))
1785 ((and (math-equal-int (math-sub q2 q1
) 1)
1787 (memq (nth 1 a
) '(0 2)))
1788 (math-make-intv (nth 1 a
) m1 b
))
1790 (math-make-intv 2 0 b
)))))
1793 (defun math-read-angle-brackets ()
1794 (let* ((last (or (math-check-for-commas t
) (length exp-str
)))
1795 (str (substring exp-str exp-pos last
))
1797 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str
)
1798 (let ((str1 (substring str
0 (1- (match-end 0))))
1799 (str2 (substring str
(match-end 0)))
1800 (calc-hashes-used 0))
1801 (setq str1
(math-read-expr (concat "[" str1
"]")))
1802 (if (eq (car-safe str1
) 'error
)
1804 (setq str2
(math-read-expr str2
))
1805 (if (eq (car-safe str2
) 'error
)
1807 (append '(calcFunc-lambda) (cdr str1
) (list str2
)))))
1808 (if (string-match "#" str
)
1809 (let ((calc-hashes-used 0))
1810 (and (setq str
(math-read-expr str
))
1811 (if (eq (car-safe str
) 'error
)
1813 (append '(calcFunc-lambda)
1814 (calc-invent-args calc-hashes-used
)
1816 (math-parse-date str
)))))
1818 (throw 'syntax res
))
1819 (if (eq (car-safe res
) 'error
)
1820 (throw 'syntax
(nth 2 res
)))
1821 (setq exp-pos
(1+ last
))
1825 ;;; arch-tag: a3d8f33b-9508-4043-8060-d02b8c9c750c
1826 ;;; calc-forms.el ends here