1 ;;; calc-forms.el --- data format conversion functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 ;; Declare functions which are defined elsewhere.
33 (declare-function calendar-current-time-zone
"cal-dst" ())
34 (declare-function calendar-absolute-from-gregorian
"calendar" (date))
35 (declare-function dst-in-effect
"cal-dst" (date))
41 (let ((time (current-time-string)))
42 (calc-enter-result 0 "time"
45 (string-to-number (substring time
11 13))
46 (string-to-number (substring time
14 16))
47 (string-to-number (substring time
17 19)))
48 (list 'hms
24 0 0))))))
50 (defun calc-to-hms (arg)
54 (if (eq calc-angle-mode
'rad
)
55 (calc-unary-op ">rad" 'calcFunc-rad arg
)
56 (calc-unary-op ">deg" 'calcFunc-deg arg
))
57 (calc-unary-op ">hms" 'calcFunc-hms arg
))))
59 (defun calc-from-hms (arg)
65 (defun calc-hms-notation (fmt)
66 (interactive "sHours-minutes-seconds format (hms, @ ' \", etc.): ")
68 (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt
)
70 (calc-change-mode 'calc-hms-format
71 (concat "%s" (math-match-substring fmt
1)
72 (math-match-substring fmt
2)
73 "%s" (math-match-substring fmt
3)
74 (math-match-substring fmt
4)
75 "%s" (math-match-substring fmt
5))
77 (setq-default calc-hms-format calc-hms-format
)) ; for minibuffer
78 (error "Bad hours-minutes-seconds format"))))
80 (defun calc-date-notation (fmt arg
)
81 (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP")
83 (if (string-match-p "\\`\\s-*\\'" fmt
)
85 (if (string-match "\\` *[0-9] *\\'" fmt
)
86 (setq fmt
(nth (string-to-number fmt
) calc-standard-date-formats
)))
87 (or (string-match "[a-zA-Z]" fmt
)
88 (error "Bad date format specifier"))
90 (>= (setq arg
(prefix-numeric-value arg
)) 0)
92 (setq calc-standard-date-formats
93 (copy-sequence calc-standard-date-formats
))
94 (setcar (nthcdr arg calc-standard-date-formats
) fmt
))
95 (let ((case-fold-search nil
))
96 (and (not (string-match "<.*>" fmt
))
97 (string-match "\\`[^hHspP]*\\([^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*[bBhHmpPsS]+[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*\\)[^hHspP]*\\'" fmt
)
98 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
99 (regexp-quote (math-match-substring fmt
1))
100 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt
)
101 (setq fmt
(concat (substring fmt
0 (match-beginning 0))
103 (substring fmt
(match-beginning 0) (match-end 0))
105 (substring fmt
(match-end 0))))))
110 (let ((case-fold-search nil
))
111 (and (setq temp
(string-match ":[BS]S" fmt
))
113 (while (setq pos
(string-match "[<>a-zA-Z]" fmt
))
115 (setq lfmt
(cons (substring fmt
0 pos
) lfmt
)))
117 (cond ((= (aref fmt pos
) ?\
<)
118 (and time
(error "Nested <'s not allowed"))
119 (and lfmt
(setq fullfmt
(nconc lfmt fullfmt
)
122 ((= (aref fmt pos
) ?\
>)
123 (or time
(error "Misplaced > in format"))
124 (and lfmt
(setq fullfmt
(cons (nreverse lfmt
) fullfmt
)
128 (if (string-match "\\`[^a-zA-Z]*[bB][a-zA-Z]" fmt
)
129 (setq pos2
(1+ pos2
)))
130 (while (and (< pos2
(length fmt
))
131 (= (upcase (aref fmt pos2
))
132 (upcase (aref fmt
(1- pos2
)))))
133 (setq pos2
(1+ pos2
)))
134 (setq sym
(intern (substring fmt pos pos2
)))
135 (or (memq sym
'(Y YY BY YYY YYYY
136 aa AA aaa AAA aaaa AAAA
137 bb BB bbb BBB bbbb BBBB
138 M MM BM mmm Mmm Mmmm MMM MMMM
140 W www Www Wwww WWW WWWW
143 m mm bm s ss bss SS BS C
145 (and (eq sym
'X
) (not lfmt
) (not fullfmt
))
146 (error "Bad format code: %s" sym
))
147 (and (memq sym
'(bb BB bbb BBB bbbb BBBB
))
148 (setq lfmt
(cons 'b lfmt
)))
149 (setq lfmt
(cons sym lfmt
))))
150 (setq fmt
(substring fmt pos2
)))
152 (setq lfmt
(cons fmt lfmt
)))
154 (setq fullfmt
(cons (nreverse lfmt
) fullfmt
))
155 (setq fullfmt
(nconc lfmt fullfmt
))))
156 (calc-change-mode 'calc-date-format
(nreverse fullfmt
) t
))))
159 (defun calc-hms-mode ()
162 (calc-change-mode 'calc-angle-mode
'hms
)
163 (message "Angles measured in degrees-minutes-seconds")))
166 (defun calc-now (arg)
168 (calc-date-zero-args "now" 'calcFunc-now arg
))
170 (defun calc-date-part (arg)
171 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
172 (if (or (< arg
1) (> arg
9))
173 (error "Part code out of range"))
176 (nth arg
'(nil "year" "mnth" "day" "hour" "minu"
177 "sec" "wday" "yday" "hmst"))
178 (list (nth arg
'(nil calcFunc-year calcFunc-month
179 calcFunc-day calcFunc-hour
180 calcFunc-minute calcFunc-second
181 calcFunc-weekday calcFunc-yearday
185 (defun calc-date (arg)
187 (if (or (< arg
1) (> arg
6))
188 (error "Between one and six arguments are allowed"))
190 (calc-enter-result arg
"date" (cons 'calcFunc-date
(calc-top-list-n arg
)))))
192 (defun calc-julian (arg)
194 (calc-date-one-arg "juln" 'calcFunc-julian arg
))
196 (defun calc-unix-time (arg)
198 (calc-date-one-arg "unix" 'calcFunc-unixtime arg
))
200 (defun calc-time-zone (arg)
202 (calc-date-zero-args "zone" 'calcFunc-tzone arg
))
204 (defun calc-convert-time-zones (old &optional new
)
205 (interactive "sFrom time zone: ")
208 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv
(calc-top-list-n 3)))
209 (if (equal old
"") (setq old
"local"))
211 (setq new
(read-string (concat "From time zone: " old
213 (if (stringp old
) (setq old
(math-read-expr old
)))
214 (if (eq (car-safe old
) 'error
)
215 (error "Error in expression: %S" (nth 1 old
)))
216 (if (equal new
"") (setq new
"local"))
217 (if (stringp new
) (setq new
(math-read-expr new
)))
218 (if (eq (car-safe new
) 'error
)
219 (error "Error in expression: %S" (nth 1 new
)))
220 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
221 (calc-top-n 1) old new
)))))
223 (defun calc-new-week (arg)
225 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg
))
227 (defun calc-new-month (arg)
229 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg
))
231 (defun calc-new-year (arg)
233 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg
))
235 (defun calc-inc-month (arg)
237 (calc-date-one-arg "incm" 'calcFunc-incmonth arg
))
239 (defun calc-business-days-plus (arg)
242 (calc-binary-op "bus+" 'calcFunc-badd arg
)))
244 (defun calc-business-days-minus (arg)
247 (calc-binary-op "bus-" 'calcFunc-bsub arg
)))
249 (defun calc-date-zero-args (prefix func arg
)
252 (calc-enter-result 1 prefix
(list func
(calc-top-n 1)))
253 (calc-enter-result 0 prefix
(if arg
254 (list func
(prefix-numeric-value arg
))
257 (defun calc-date-one-arg (prefix func arg
)
260 (calc-enter-result 2 prefix
(cons func
(calc-top-list-n 2)))
261 (calc-enter-result 1 prefix
(if arg
262 (list func
(calc-top-n 1)
263 (prefix-numeric-value arg
))
264 (list func
(calc-top-n 1)))))))
267 ;;;; Hours-minutes-seconds forms.
269 (defun math-normalize-hms (a)
270 (let ((h (math-normalize (nth 1 a
)))
271 (m (math-normalize (nth 2 a
)))
272 (s (let ((calc-internal-prec (max (- calc-internal-prec
4) 3)))
273 (math-normalize (nth 3 a
)))))
277 (setq s
(math-add s -
60)
280 (setq m
(math-add m -
60)
282 (if (not (Math-lessp -
60 s
))
283 (setq s
(math-add s
60)
285 (if (not (Math-lessp -
60 m
))
286 (setq m
(math-add m
60)
289 (setq s
(math-add s
60)
292 (setq m
(math-add m
60)
294 (if (not (Math-lessp s
60))
295 (setq s
(math-add s -
60)
297 (if (not (Math-lessp m
60))
298 (setq m
(math-add m -
60)
300 (if (and (eq (car-safe s
) 'float
)
301 (<= (+ (math-numdigs (nth 1 s
)) (nth 2 s
))
302 (- 2 calc-internal-prec
)))
306 ;;; Convert A from ANG or current angular mode to HMS format.
307 (defun math-to-hms (a &optional ang
) ; [X R] [Public]
308 (cond ((eq (car-safe a
) 'hms
) a
)
309 ((eq (car-safe a
) 'sdev
)
310 (math-make-sdev (math-to-hms (nth 1 a
))
311 (math-to-hms (nth 2 a
))))
312 ((not (Math-numberp a
))
313 (list 'calcFunc-hms a
))
315 (math-neg (math-to-hms (math-neg a
) ang
)))
316 ((eq (or ang calc-angle-mode
) 'rad
)
317 (math-to-hms (math-div a
(math-pi-over-180)) 'deg
))
318 ((memq (car-safe a
) '(cplx polar
)) a
)
320 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
321 ; (math-normalize a)))
323 (let* ((b (math-mul a
3600))
324 (hm (math-trunc (math-div b
60)))
325 (hmd (math-idivmod hm
60)))
329 (math-sub b
(math-mul hm
60))))))))
330 (defun calcFunc-hms (h &optional m s
)
331 (or (Math-realp h
) (math-reject-arg h
'realp
))
333 (or (Math-realp m
) (math-reject-arg m
'realp
))
335 (or (Math-realp s
) (math-reject-arg s
'realp
))
336 (if (and (not (Math-lessp m
0)) (Math-lessp m
60)
337 (not (Math-lessp s
0)) (Math-lessp s
60))
338 (math-add (math-to-hms h
)
340 (math-to-hms (math-add h
341 (math-add (math-div (or m
0) 60)
342 (math-div (or s
0) 3600)))
345 ;;; Convert A from HMS format to ANG or current angular mode.
346 (defun math-from-hms (a &optional ang
) ; [R X] [Public]
347 (cond ((not (eq (car-safe a
) 'hms
))
350 (if (eq (car-safe a
) 'sdev
)
351 (math-make-sdev (math-from-hms (nth 1 a
) ang
)
352 (math-from-hms (nth 2 a
) ang
))
353 (if (eq (or ang calc-angle-mode
) 'rad
)
354 (list 'calcFunc-rad a
)
355 (list 'calcFunc-deg a
)))))
357 (math-neg (math-from-hms (math-neg a
) ang
)))
358 ((eq (or ang calc-angle-mode
) 'rad
)
359 (math-mul (math-from-hms a
'deg
) (math-pi-over-180)))
361 (math-add (math-div (math-add (math-div (nth 3 a
)
370 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
371 ;;; These versions are rewritten to use arbitrary-size integers.
373 ;;; A numerical date is the number of days since midnight on
374 ;;; the morning of December 31, 1 B.C. Emacs's calendar refers to such
375 ;;; a date as an absolute date, some function names also use that
376 ;;; terminology. If the date is a non-integer, it represents a specific date and time.
377 ;;; A "dt" is a list of the form, (year month day), corresponding to
378 ;;; an integer code, or (year month day hour minute second), corresponding
379 ;;; to a non-integer code.
381 (defun math-date-to-gregorian-dt (date)
382 "Return the day (YEAR MONTH DAY) in the Gregorian calendar.
383 DATE is the number of days since December 31, -1 in the Gregorian calendar."
386 (year (math-quotient (math-add date
(if (Math-lessp date
711859)
387 365 ; for speed, we take
388 -
108)) ; >1950 as a special case
389 (if (math-negp date
) 366 365)))
390 ; this result may be an overestimate
392 (while (Math-lessp date
(setq temp
(math-absolute-from-gregorian-dt year
1 1)))
393 (setq year
(math-add year -
1)))
394 (if (eq year
0) (setq year -
1))
395 (setq date
(1+ (math-sub date temp
)))
397 (if (math-leap-year-p year
)
398 [1 32 61 92 122 153 183 214 245 275 306 336 999]
399 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
400 (while (>= date
(aref temp month
))
401 (setq month
(1+ month
)))
402 (setq day
(1+ (- date
(aref temp
(1- month
)))))
403 (list year month day
)))
405 (defun math-date-to-julian-dt (date)
406 "Return the day (YEAR MONTH DAY) in the Julian calendar.
407 DATE is the number of days since December 31, -1 in the Gregorian calendar."
410 (year (math-quotient (math-add date
(if (Math-lessp date
711859)
411 365 ; for speed, we take
412 -
108)) ; >1950 as a special case
413 (if (math-negp date
) 366 365)))
414 ; this result may be an overestimate
416 (while (Math-lessp date
(setq temp
(math-absolute-from-julian-dt year
1 1)))
417 (setq year
(math-add year -
1)))
418 (if (eq year
0) (setq year -
1))
419 (setq date
(1+ (math-sub date temp
)))
421 (if (math-leap-year-p year t
)
422 [1 32 61 92 122 153 183 214 245 275 306 336 999]
423 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
424 (while (>= date
(aref temp month
))
425 (setq month
(1+ month
)))
426 (setq day
(1+ (- date
(aref temp
(1- month
)))))
427 (list year month day
)))
429 (defun math-date-to-dt (value)
430 "Return the day and time of VALUE.
431 The integer part of VALUE is the number of days since Dec 31, -1
432 in the Gregorian calendar and the remaining part determines the time."
433 (if (eq (car-safe value
) 'date
)
434 (setq value
(nth 1 value
)))
435 (or (math-realp value
)
436 (math-reject-arg value
'datep
))
437 (let* ((parts (math-date-parts value
))
440 (dt (if (and calc-gregorian-switch
443 (nth 3 calc-gregorian-switch
)
444 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))
446 (math-date-to-julian-dt value
)
447 (math-date-to-gregorian-dt value
))))
448 (if (math-integerp value
)
454 (math-add (% time
60) (nth 2 parts
)))))))
456 (defun math-dt-to-date (dt)
457 (or (integerp (nth 1 dt
))
458 (math-reject-arg (nth 1 dt
) 'fixnump
))
459 (if (or (< (nth 1 dt
) 1) (> (nth 1 dt
) 12))
460 (math-reject-arg (nth 1 dt
) "Month value is out of range"))
461 (or (integerp (nth 2 dt
))
462 (math-reject-arg (nth 2 dt
) 'fixnump
))
463 (if (or (< (nth 2 dt
) 1) (> (nth 2 dt
) 31))
464 (math-reject-arg (nth 2 dt
) "Day value is out of range"))
465 (let ((date (math-absolute-from-dt (car dt
) (nth 1 dt
) (nth 2 dt
))))
467 (math-add (math-float date
)
468 (math-div (math-add (+ (* (nth 3 dt
) 3600)
474 (defun math-date-parts (value &optional offset
)
475 (let* ((date (math-floor value
))
476 (time (math-round (math-mul (math-sub value
(or offset date
)) 86400)
477 (and (> calc-internal-prec
12)
478 (- calc-internal-prec
12))))
479 (ftime (math-floor time
)))
482 (math-sub time ftime
))))
485 (defun math-this-year ()
486 (nth 5 (decode-time)))
488 (defun math-leap-year-p (year &optional julian
)
489 "Non-nil if YEAR is a leap year.
490 If JULIAN is non-nil, then use the criterion for leap years
491 in the Julian calendar, otherwise use the criterion in the
495 (= (math-imod (math-neg year
) 4) 1)
496 (= (math-imod year
4) 0))
497 (setq year
(math-imod year
400))
498 (or (and (= (% year
4) 0) (/= (% year
100) 0))
501 (defun math-days-in-month (year month
)
502 (if (and (= month
2) (math-leap-year-p year
))
504 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month
))))
506 (defun math-day-in-year (year month day
&optional julian
)
507 "Return the number of days of the year up to YEAR MONTH DAY.
508 The count includes the given date.
509 If JULIAN is non-nil, use the Julian calendar, otherwise
510 use the Gregorian calendar."
511 (let ((day-of-year (+ day
(* 31 (1- month
)))))
514 (setq day-of-year
(- day-of-year
(/ (+ 23 (* 4 month
)) 10)))
515 (if (math-leap-year-p year julian
)
516 (setq day-of-year
(1+ day-of-year
)))))
519 (defun math-day-number (year month day
)
520 "Return the number of days of the year up to YEAR MONTH DAY.
521 The count includes the given date."
522 (if calc-gregorian-switch
523 (cond ((eq year
(nth 0 calc-gregorian-switch
))
525 (- (math-absolute-from-dt year month day
)
526 (math-absolute-from-dt year
1 1))))
527 ((Math-lessp year
(nth 0 calc-gregorian-switch
))
528 (math-day-in-year year month day t
))
530 (math-day-in-year year month day
)))
531 (math-day-in-year year month day
)))
533 (defun math-dt-before-p (dt1 dt2
)
534 "Non-nil if DT1 occurs before DT2.
535 A DT is a list of the form (YEAR MONTH DAY)."
536 (or (Math-lessp (nth 0 dt1
) (nth 0 dt2
))
537 (and (equal (nth 0 dt1
) (nth 0 dt2
))
538 (or (< (nth 1 dt1
) (nth 1 dt2
))
539 (and (= (nth 1 dt1
) (nth 1 dt2
))
540 (< (nth 2 dt1
) (nth 2 dt2
)))))))
542 (defun math-absolute-from-gregorian-dt (year month day
)
543 "Return the DATE of the day given by the Gregorian day YEAR MONTH DAY.
544 Recall that DATE is the number of days since December 31, -1
545 in the Gregorian calendar."
546 (if (eq year
0) (setq year -
1))
547 (let ((yearm1 (math-sub year
1)))
549 ;; Add the number of days of the year and the numbers of days
550 ;; in the previous years (leap year days to be added separately)
551 (math-add (math-day-in-year year month day
)
552 (math-add (math-mul 365 yearm1
)
553 ;; Add the number of Julian leap years
555 (math-quotient yearm1
4)
557 (math-quotient (math-sub 3 year
)
559 ;; Subtract the number of Julian leap years which are not
560 ;; Gregorian leap years. In C=4N+r centuries, there will
561 ;; be 3N+r of these days. The following will compute
563 (let* ((correction (math-mul (math-quotient yearm1
100) 3))
564 (res (math-idivmod correction
4)))
565 (math-add (if (= (cdr res
) 0)
570 (defun math-absolute-from-julian-dt (year month day
)
571 "Return the DATE of the day given by the Julian day YEAR MONTH DAY.
572 Recall that DATE is the number of days since December 31, -1
573 in the Gregorian calendar."
574 (if (eq year
0) (setq year -
1))
575 (let ((yearm1 (math-sub year
1)))
577 ;; Add the number of days of the year and the numbers of days
578 ;; in the previous years (leap year days to be added separately)
579 (math-add (math-day-in-year year month day
)
580 (math-add (math-mul 365 yearm1
)
581 ;; Add the number of Julian leap years
583 (math-quotient yearm1
4)
585 (math-quotient (math-sub 3 year
)
587 ;; Adjustment, since January 1, 1 (Julian) is absolute day -1
590 ;; calc-gregorian-switch is a customizable variable defined in calc.el
591 (defvar calc-gregorian-switch
)
594 (defun math-absolute-from-dt (year month day
)
595 "Return the DATE of the day given by the day YEAR MONTH DAY.
596 Recall that DATE is the number of days since December 31, -1
597 in the Gregorian calendar."
598 (if (and calc-gregorian-switch
599 ;; The next few lines determine if the given date
600 ;; occurs before the switch to the Gregorian calendar.
601 (math-dt-before-p (list year month day
) calc-gregorian-switch
))
602 (math-absolute-from-julian-dt year month day
)
603 (math-absolute-from-gregorian-dt year month day
)))
605 ;;; It is safe to redefine these in your init file to use a different
608 (defvar math-long-weekday-names
'( "Sunday" "Monday" "Tuesday" "Wednesday"
609 "Thursday" "Friday" "Saturday" ))
610 (defvar math-short-weekday-names
'( "Sun" "Mon" "Tue" "Wed"
613 (defvar math-long-month-names
'( "January" "February" "March" "April"
614 "May" "June" "July" "August"
615 "September" "October" "November" "December" ))
616 (defvar math-short-month-names
'( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
617 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
620 (defvar math-format-date-cache nil
)
622 ;; The variables math-fd-date, math-fd-dt, math-fd-year,
623 ;; math-fd-month, math-fd-day, math-fd-weekday, math-fd-hour,
624 ;; math-fd-minute, math-fd-second, math-fd-bc-flag are local
625 ;; to math-format-date, but are used by math-format-date-part,
626 ;; which is called by math-format-date.
627 (defvar math-fd-date
)
629 (defvar math-fd-year
)
630 (defvar math-fd-month
)
632 (defvar math-fd-weekday
)
633 (defvar math-fd-hour
)
634 (defvar math-fd-minute
)
635 (defvar math-fd-second
)
636 (defvar math-fd-bc-flag
)
638 (defun math-format-date (math-fd-date)
639 (if (eq (car-safe math-fd-date
) 'date
)
640 (setq math-fd-date
(nth 1 math-fd-date
)))
641 (let ((entry (list math-fd-date calc-internal-prec calc-date-format
)))
642 (or (cdr (assoc entry math-format-date-cache
))
643 (let* ((math-fd-dt nil
)
644 (calc-group-digits nil
)
645 (calc-leading-zeros nil
)
646 (calc-number-radix 10)
647 (calc-twos-complement-mode nil
)
648 math-fd-year math-fd-month math-fd-day math-fd-weekday
649 math-fd-hour math-fd-minute math-fd-second
650 (math-fd-bc-flag nil
)
651 (fmt (apply 'concat
(mapcar 'math-format-date-part
653 (setq math-format-date-cache
(cons (cons entry fmt
)
654 math-format-date-cache
))
655 (and (setq math-fd-dt
(nthcdr 10 math-format-date-cache
))
656 (setcdr math-fd-dt nil
))
659 (defconst math-julian-date-beginning
'(float 17214225 -
1)
660 "The beginning of the Julian date calendar,
661 as measured in the number of days before December 31, 1 BC (Gregorian).")
663 (defconst math-julian-date-beginning-int
1721423
664 "The beginning of the Julian date calendar,
665 as measured in the integer number of days before December 31, 1 BC (Gregorian).")
667 (defun math-format-date-part (x)
671 (if (math-integerp math-fd-date
)
673 (apply 'concat
(mapcar 'math-format-date-part x
))))
677 (math-format-number math-fd-date
))
679 (math-format-number (math-floor math-fd-date
)))
682 (math-add math-fd-date math-julian-date-beginning
)))
684 (math-format-number (math-add
685 (math-floor math-fd-date
)
686 math-julian-date-beginning-int
)))
688 (math-format-number (nth 1 (math-date-parts math-fd-date
719164))))
692 (setq math-fd-dt
(math-date-to-dt math-fd-date
)
693 math-fd-year
(car math-fd-dt
)
694 math-fd-month
(nth 1 math-fd-dt
)
695 math-fd-day
(nth 2 math-fd-dt
)
696 math-fd-weekday
(math-mod (math-floor math-fd-date
) 7)
697 math-fd-hour
(nth 3 math-fd-dt
)
698 math-fd-minute
(nth 4 math-fd-dt
)
699 math-fd-second
(nth 5 math-fd-dt
))
700 (and (memq 'b calc-date-format
)
701 (math-negp math-fd-year
)
702 (setq math-fd-year
(math-neg math-fd-year
)
703 math-fd-bc-flag t
))))
705 (if (and (integerp math-fd-year
) (> math-fd-year
1940) (< math-fd-year
2040))
706 (format (cond ((eq x
'YY
) "%02d")
709 (% math-fd-year
100))
710 (if (and (natnump math-fd-year
) (< math-fd-year
100))
711 (format "+%d" math-fd-year
)
712 (math-format-number math-fd-year
))))
714 (math-format-number math-fd-year
))
716 (if (and (natnump math-fd-year
) (< math-fd-year
100))
717 (format "+%d" math-fd-year
)
718 (math-format-number math-fd-year
)))
721 (and (not math-fd-bc-flag
) "ad"))
723 (and (not math-fd-bc-flag
) "AD"))
725 (and (not math-fd-bc-flag
) "ad "))
727 (and (not math-fd-bc-flag
) "AD "))
729 (and (not math-fd-bc-flag
) "a.d."))
731 (and (not math-fd-bc-flag
) "A.D."))
733 (and math-fd-bc-flag
"bc"))
735 (and math-fd-bc-flag
"BC"))
737 (and math-fd-bc-flag
" bc"))
739 (and math-fd-bc-flag
" BC"))
741 (and math-fd-bc-flag
"b.c."))
743 (and math-fd-bc-flag
"B.C."))
745 (format "%d" math-fd-month
))
747 (format "%02d" math-fd-month
))
749 (format "%2d" math-fd-month
))
751 (downcase (nth (1- math-fd-month
) math-short-month-names
)))
753 (nth (1- math-fd-month
) math-short-month-names
))
755 (upcase (nth (1- math-fd-month
) math-short-month-names
)))
757 (nth (1- math-fd-month
) math-long-month-names
))
759 (upcase (nth (1- math-fd-month
) math-long-month-names
)))
761 (format "%d" math-fd-day
))
763 (format "%02d" math-fd-day
))
765 (format "%2d" math-fd-day
))
767 (format "%d" math-fd-weekday
))
769 (downcase (nth math-fd-weekday math-short-weekday-names
)))
771 (nth math-fd-weekday math-short-weekday-names
))
773 (upcase (nth math-fd-weekday math-short-weekday-names
)))
775 (nth math-fd-weekday math-long-weekday-names
))
777 (upcase (nth math-fd-weekday math-long-weekday-names
)))
779 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
781 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
783 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
785 (and math-fd-hour
(format "%d" math-fd-hour
)))
787 (and math-fd-hour
(format "%02d" math-fd-hour
)))
789 (and math-fd-hour
(format "%2d" math-fd-hour
)))
791 (and math-fd-hour
(format "%d" (1+ (%
(+ math-fd-hour
11) 12)))))
793 (and math-fd-hour
(format "%02d" (1+ (%
(+ math-fd-hour
11) 12)))))
795 (and math-fd-hour
(format "%2d" (1+ (%
(+ math-fd-hour
11) 12)))))
797 (and math-fd-hour
(if (< math-fd-hour
12) "a" "p")))
799 (and math-fd-hour
(if (< math-fd-hour
12) "A" "P")))
801 (and math-fd-hour
(if (< math-fd-hour
12) "am" "pm")))
803 (and math-fd-hour
(if (< math-fd-hour
12) "AM" "PM")))
805 (and math-fd-hour
(if (< math-fd-hour
12) "a.m." "p.m.")))
807 (and math-fd-hour
(if (< math-fd-hour
12) "A.M." "P.M.")))
809 (and math-fd-minute
(format "%d" math-fd-minute
)))
811 (and math-fd-minute
(format "%02d" math-fd-minute
)))
813 (and math-fd-minute
(format "%2d" math-fd-minute
)))
815 (and math-fd-second
(not (math-zerop math-fd-second
))
817 ((memq x
'(s ss bs SS BS
))
819 (not (and (memq x
'(SS BS
)) (math-zerop math-fd-second
)))
820 (if (integerp math-fd-second
)
821 (format (cond ((memq x
'(ss SS
)) "%02d")
822 ((memq x
'(bs BS
)) "%2d")
825 (concat (if (Math-lessp math-fd-second
10)
826 (cond ((memq x
'(ss SS
)) "0")
827 ((memq x
'(bs BS
)) " ")
830 (let ((calc-float-format
831 (list 'fix
(min (- 12 calc-internal-prec
)
833 (math-format-number math-fd-second
))))))))
835 ;; The variable math-pd-str is local to math-parse-date and
836 ;; math-parse-standard-date, but is used by math-parse-date-word,
837 ;; which is called by math-parse-date and math-parse-standard-date.
840 (defun math-parse-date (math-pd-str)
842 (or (math-parse-standard-date math-pd-str t
)
843 (math-parse-standard-date math-pd-str nil
)
844 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str
)
845 (list 'date
(math-read-number (math-match-substring math-pd-str
1))))
846 (let ((case-fold-search t
)
847 (year nil
) (month nil
) (day nil
) (weekday nil
)
848 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
)
849 (a nil
) (b nil
) (c nil
) (bigyear nil
) temp
)
851 ;; Extract the time, if any.
852 (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
)
853 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str
))
854 (let ((ampm (math-match-substring math-pd-str
6)))
855 (setq hour
(string-to-number (math-match-substring math-pd-str
1))
856 minute
(math-match-substring math-pd-str
2)
857 second
(math-match-substring math-pd-str
4)
858 math-pd-str
(concat (substring math-pd-str
0 (match-beginning 0))
859 (substring math-pd-str
(match-end 0))))
860 (if (equal minute
"")
862 (setq minute
(string-to-number minute
)))
863 (if (equal second
"")
865 (setq second
(math-read-number second
)))
868 (throw 'syntax
"Hour value out of range"))
869 (setq ampm
(upcase (aref ampm
0)))
870 (if (memq ampm
'(?N ?M
))
871 (if (and (= hour
12) (= minute
0) (eq second
0))
872 (if (eq ampm ?M
) (setq hour
0))
874 "Time must be 12:00:00 in this context"))
875 (if (or (= hour
0) (> hour
12))
876 (throw 'syntax
"Hour value out of range"))
877 (if (eq (= ampm ?A
) (= hour
12))
878 (setq hour
(%
(+ hour
12) 24)))))))
880 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
881 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str
)
883 (setq math-pd-str
(copy-sequence math-pd-str
))
884 (aset math-pd-str
(match-beginning 1) ?\
/)))
886 ;; Extract obvious month or weekday names.
887 (if (string-match "[a-zA-Z]" math-pd-str
)
889 (setq month
(math-parse-date-word math-long-month-names
))
890 (setq weekday
(math-parse-date-word math-long-weekday-names
))
891 (or month
(setq month
892 (math-parse-date-word math-short-month-names
)))
893 (or weekday
(math-parse-date-word math-short-weekday-names
))
895 (if (setq temp
(math-parse-date-word
896 '( "noon" "midnight" "mid" )))
897 (setq hour
(if (= temp
1) 12 0) minute
0 second
0)))
898 (or (math-parse-date-word '( "ad" "a.d." ))
899 (if (math-parse-date-word '( "bc" "b.c." ))
901 (if (string-match "[a-zA-Z]+" math-pd-str
)
902 (throw 'syntax
(format "Bad word in date: \"%s\""
903 (math-match-substring math-pd-str
0))))))
905 ;; If there is a huge number other than the year, ignore it.
906 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str
)
907 (setq temp
(concat (substring math-pd-str
0 (match-beginning 0))
908 (substring math-pd-str
(match-end 0))))
910 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp
))
911 (setq math-pd-str temp
))
913 ;; If there is a number with a sign or a large number, it is a year.
914 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str
)
915 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str
))
916 (setq year
(math-match-substring math-pd-str
1)
917 math-pd-str
(concat (substring math-pd-str
0 (match-beginning 1))
918 (substring math-pd-str
(match-end 1)))
919 year
(math-read-number year
)
922 ;; Collect remaining numbers.
924 (while (string-match "[0-9]+" math-pd-str temp
)
925 (and c
(throw 'syntax
"Too many numbers in date"))
926 (setq c
(string-to-number (math-match-substring math-pd-str
0)))
927 (or b
(setq b c c nil
))
928 (or a
(setq a b b nil
))
929 (setq temp
(match-end 0)))
931 ;; Check that we have the right amount of information.
932 (setq temp
(+ (if year
1 0) (if month
1 0) (if day
1 0)
933 (if a
1 0) (if b
1 0) (if c
1 0)))
935 (throw 'syntax
"Too many numbers in date")
936 (if (or (< temp
2) (and year
(= temp
2)))
937 (throw 'syntax
"Not enough numbers in date")
938 (if (= temp
2) ; if year omitted, assume current year
939 (setq year
(math-this-year)))))
941 ;; A large number must be a year.
943 (if (and a
(or (> a
31) (< a
1)))
944 (setq year a a b b c c nil
)
945 (if (and b
(or (> b
31) (< b
1)))
946 (setq year b b c c nil
)
947 (if (and c
(or (> c
31) (< c
1)))
948 (setq year c c nil
)))))
950 ;; A medium-large number must be a day.
953 (setq day a a b b c c nil
)
955 (setq day b b c c nil
)
957 (setq day c c nil
)))))
959 ;; We may know enough to sort it out now.
961 (or month
(setq month a
))
965 ;; Interpret order of numbers as same as for display format.
966 (setq temp calc-date-format
)
968 (cond ((not (symbolp (car temp
))))
969 ((memq (car temp
) '(Y YY BY YYY YYYY
))
970 (or year
(setq year a a b b c
)))
971 ((memq (car temp
) '(M MM BM mmm Mmm Mmmm MMM MMMM
))
972 (or month
(setq month a a b b c
)))
973 ((memq (car temp
) '(D DD BD
))
974 (or day
(setq day a a b b c
))))
975 (setq temp
(cdr temp
)))
977 ;; If display format was not complete, assume American style.
978 (or month
(setq month a a b b c
))
979 (or day
(setq day a a b b c
))
980 (or year
(setq year a a b b c
))))
983 (setq year
(math-neg (math-abs year
))))
985 (math-parse-date-validate year bigyear month day
986 hour minute second
)))))
988 (defun math-parse-date-validate (year bigyear month day hour minute second
)
989 (and (not bigyear
) (natnump year
) (< year
100)
990 (setq year
(+ year
(if (< year
40) 2000 1900))))
992 (throw 'syntax
"Year value is out of range"))
993 (if (or (< month
1) (> month
12))
994 (throw 'syntax
"Month value is out of range"))
995 (if (or (< day
1) (> day
(math-days-in-month year month
)))
996 (throw 'syntax
"Day value is out of range"))
999 (if (or (< hour
0) (> hour
23))
1000 (throw 'syntax
"Hour value is out of range"))
1001 (if (or (< minute
0) (> minute
59))
1002 (throw 'syntax
"Minute value is out of range"))
1003 (if (or (math-negp second
) (not (Math-lessp second
60)))
1004 (throw 'syntax
"Seconds value is out of range"))))
1005 (list 'date
(math-dt-to-date (append (list year month day
)
1006 (and hour
(list hour minute second
))))))
1008 (defun math-parse-date-word (names &optional front
)
1010 (while (and names
(not (string-match (if (equal (car names
) "Sep")
1012 (regexp-quote (car names
)))
1014 (setq names
(cdr names
)
1017 (or (not front
) (= (match-beginning 0) 0))
1019 (setq math-pd-str
(concat (substring math-pd-str
0 (match-beginning 0))
1021 (substring math-pd-str
(match-end 0))))
1024 (defun math-parse-standard-date (math-pd-str with-time
)
1025 (let ((case-fold-search t
)
1027 (fmt calc-date-format
) this next
(gnext nil
)
1028 (year nil
) (month nil
) (day nil
) (bigyear nil
) (yearday nil
)
1029 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
))
1030 (while (and fmt okay
)
1031 (setq this
(car fmt
)
1032 fmt
(setq fmt
(or (cdr fmt
)
1037 (if (consp next
) (setq next
(car next
)))
1038 (or (cond ((listp this
)
1044 (if (and (<= (length this
) (length math-pd-str
))
1046 (substring math-pd-str
0 (length this
))))
1047 (setq math-pd-str
(substring math-pd-str
(length this
)))))
1050 ((memq this
'(n N j J
))
1051 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str
)
1052 (setq num
(math-match-substring math-pd-str
0)
1053 math-pd-str
(substring math-pd-str
(match-end 0))
1054 num
(math-date-to-dt (math-read-number num
))
1056 (if (memq this
'(n N
))
1058 (if (or (eq this
'j
)
1059 (math-integerp num
))
1060 math-julian-date-beginning-int
1061 math-julian-date-beginning
)))
1062 hour
(or (nth 3 num
) hour
)
1063 minute
(or (nth 4 num
) minute
)
1064 second
(or (nth 5 num
) second
)
1069 (and (string-match "\\`[-+]?[0-9]+" math-pd-str
)
1070 (setq num
(math-match-substring math-pd-str
0)
1071 math-pd-str
(substring math-pd-str
(match-end 0))
1072 num
(math-date-to-dt
1074 (math-div (math-read-number num
)
1082 ((memq this
'(mmm Mmm MMM
))
1083 (setq month
(math-parse-date-word math-short-month-names t
)))
1084 ((memq this
'(Mmmm MMMM
))
1085 (setq month
(math-parse-date-word math-long-month-names t
)))
1086 ((memq this
'(www Www WWW
))
1087 (math-parse-date-word math-short-weekday-names t
))
1088 ((memq this
'(Wwww WWWW
))
1089 (math-parse-date-word math-long-weekday-names t
))
1091 (if (string-match "\\`a" math-pd-str
)
1092 (setq hour
(if (= hour
12) 0 hour
)
1093 math-pd-str
(substring math-pd-str
1))
1094 (if (string-match "\\`p" math-pd-str
)
1095 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
1096 math-pd-str
(substring math-pd-str
1)))))
1097 ((memq this
'(pp PP pppp PPPP
))
1098 (if (string-match "\\`am\\|a\\.m\\." math-pd-str
)
1099 (setq hour
(if (= hour
12) 0 hour
)
1100 math-pd-str
(substring math-pd-str
(match-end 0)))
1101 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str
)
1102 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
1103 math-pd-str
(substring math-pd-str
(match-end 0))))))
1104 ((memq this
'(Y YY BY YYY YYYY
))
1105 (and (if (memq next
'(MM DD ddd hh HH mm ss SS
))
1106 (if (memq this
'(Y YY BYY
))
1107 (string-match "\\` *[0-9][0-9]" math-pd-str
)
1108 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str
))
1109 (string-match "\\`[-+]?[0-9]+" math-pd-str
))
1110 (setq year
(math-match-substring math-pd-str
0)
1111 bigyear
(or (eq this
'YYY
)
1112 (memq (aref math-pd-str
0) '(?\
+ ?\-
)))
1113 math-pd-str
(substring math-pd-str
(match-end 0))
1114 year
(math-read-number year
))))
1117 ((memq this
'(aa AA aaaa AAAA
))
1118 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str
)
1119 (setq math-pd-str
(substring math-pd-str
(match-end 0)))))
1120 ((memq this
'(aaa AAA
))
1121 (if (string-match "\\` *ad *" math-pd-str
)
1122 (setq math-pd-str
(substring math-pd-str
(match-end 0)))))
1123 ((memq this
'(bb BB bbb BBB bbbb BBBB
))
1124 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str
)
1125 (setq math-pd-str
(substring math-pd-str
(match-end 0))
1127 ((memq this
'(s ss bs SS BS
))
1128 (and (if (memq next
'(YY YYYY MM DD hh HH mm
))
1129 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str
)
1130 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str
))
1131 (setq second
(math-match-substring math-pd-str
0)
1132 math-pd-str
(substring math-pd-str
(match-end 0))
1133 second
(math-read-number second
))))
1135 (if (string-match "\\`:[0-9][0-9]" math-pd-str
)
1136 (setq math-pd-str
(substring math-pd-str
1))
1138 ((or (not (if (and (memq this
'(ddd MM DD hh HH mm
))
1139 (memq next
'(YY YYYY MM DD ddd
1142 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str
)
1143 (string-match "\\` *[0-9][0-9]" math-pd-str
))
1144 (string-match "\\` *[0-9]+" math-pd-str
)))
1145 (and (setq num
(string-to-number
1146 (math-match-substring math-pd-str
0))
1147 math-pd-str
(substring math-pd-str
(match-end 0)))
1151 (and (>= num
0) (< num
7)))
1152 ((memq this
'(d ddd bdd
))
1154 ((memq this
'(M MM BM
))
1156 ((memq this
'(D DD BD
))
1158 ((memq this
'(h hh bh H HH BH
))
1160 ((memq this
'(m mm bm
))
1166 (setq month
1 day
1)))
1167 (if (and okay
(equal math-pd-str
""))
1168 (and month day
(or (not (or hour minute second
))
1171 (or year
(setq year
(math-this-year)))
1172 (or second
(setq second
0))
1174 (setq year
(math-neg (math-abs year
))))
1175 (setq day
(math-parse-date-validate year bigyear month day
1176 hour minute second
))
1178 (setq day
(math-add day
(1- yearday
))))
1182 (defun calcFunc-now (&optional zone
)
1183 (let ((date (let ((calc-date-format nil
))
1184 (math-parse-date (current-time-string)))))
1187 (math-add date
(math-div (math-sub (calcFunc-tzone nil date
)
1188 (calcFunc-tzone zone date
))
1191 (calc-record-why "*Unable to interpret current date from system")
1192 (append (list 'calcFunc-now
) (and zone
(list zone
))))))
1194 (defun calcFunc-year (date)
1195 (car (math-date-to-dt date
)))
1197 (defun calcFunc-month (date)
1198 (nth 1 (math-date-to-dt date
)))
1200 (defun calcFunc-day (date)
1201 (nth 2 (math-date-to-dt date
)))
1203 (defun calcFunc-weekday (date)
1204 (if (eq (car-safe date
) 'date
)
1205 (setq date
(nth 1 date
)))
1206 (or (math-realp date
)
1207 (math-reject-arg date
'datep
))
1208 (math-mod (math-floor date
) 7))
1210 (defun calcFunc-yearday (date)
1211 (let ((dt (math-date-to-dt date
)))
1212 (math-day-number (car dt
) (nth 1 dt
) (nth 2 dt
))))
1214 (defun calcFunc-hour (date)
1215 (if (eq (car-safe date
) 'hms
)
1217 (or (nth 3 (math-date-to-dt date
)) 0)))
1219 (defun calcFunc-minute (date)
1220 (if (eq (car-safe date
) 'hms
)
1222 (or (nth 4 (math-date-to-dt date
)) 0)))
1224 (defun calcFunc-second (date)
1225 (if (eq (car-safe date
) 'hms
)
1227 (or (nth 5 (math-date-to-dt date
)) 0)))
1229 (defun calcFunc-time (date)
1230 (let ((dt (math-date-to-dt date
)))
1232 (cons 'hms
(nthcdr 3 dt
))
1233 (list 'hms
0 0 0))))
1235 (defun calcFunc-date (date &optional month day hour minute second
)
1236 (and (math-messy-integerp month
) (setq month
(math-trunc month
)))
1237 (and month
(not (integerp month
)) (math-reject-arg month
'fixnump
))
1238 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1239 (and day
(not (integerp day
)) (math-reject-arg day
'fixnump
))
1240 (if (and (eq (car-safe hour
) 'hms
) (not minute
))
1241 (setq second
(nth 3 hour
)
1244 (and (math-messy-integerp hour
) (setq hour
(math-trunc hour
)))
1245 (and hour
(not (integerp hour
)) (math-reject-arg hour
'fixnump
))
1246 (and (math-messy-integerp minute
) (setq minute
(math-trunc minute
)))
1247 (and minute
(not (integerp minute
)) (math-reject-arg minute
'fixnump
))
1248 (and (math-messy-integerp second
) (setq second
(math-trunc second
)))
1249 (and second
(not (math-realp second
)) (math-reject-arg second
'realp
))
1252 (and (math-messy-integerp date
) (setq date
(math-trunc date
)))
1253 (and date
(not (math-integerp date
)) (math-reject-arg date
'integerp
))
1256 (list 'date
(math-dt-to-date (list date month day hour
1259 (list 'date
(math-dt-to-date (list date month day
))))
1260 (list 'date
(math-dt-to-date (list (math-this-year) date month
)))))
1261 (if (math-realp date
)
1263 (if (eq (car date
) 'date
)
1265 (math-reject-arg date
'datep
)))))
1267 (defun calcFunc-julian (date &optional zone
)
1268 (if (math-realp date
)
1269 (list 'date
(if (math-integerp date
)
1270 (math-sub date math-julian-date-beginning-int
)
1271 (setq date
(math-sub date math-julian-date-beginning
))
1272 (math-sub date
(math-div (calcFunc-tzone zone date
)
1274 (if (eq (car date
) 'date
)
1275 (math-add (nth 1 date
) (if (math-integerp (nth 1 date
))
1276 math-julian-date-beginning-int
1277 (math-add math-julian-date-beginning
1278 (math-div (calcFunc-tzone zone date
)
1280 (math-reject-arg date
'datep
))))
1282 (defun calcFunc-unixtime (date &optional zone
)
1283 (if (math-realp date
)
1285 (setq date
(math-add 719164 (math-div date
'(float 864 2))))
1286 (list 'date
(math-sub date
(math-div (calcFunc-tzone zone date
)
1288 (if (eq (car date
) 'date
)
1289 (math-add (nth 1 (math-date-parts (nth 1 date
) 719164))
1290 (calcFunc-tzone zone date
))
1291 (math-reject-arg date
'datep
))))
1294 ;;; Note: Longer names must appear before shorter names which are
1295 ;;; substrings of them.
1296 (defvar math-tzone-names
1298 ( "MEGT" -
1 "MET" "METDST" ) ; Middle Europe
1299 ( "METDST" -
1 -
1 ) ( "MET" -
1 0 )
1300 ( "MEGZ" -
1 "MEZ" "MESZ" ) ( "MEZ" -
1 0 ) ( "MESZ" -
1 -
1 )
1301 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1302 ( "WETDST" 0 -
1 ) ( "WET" 0 0 )
1303 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -
1 ) ; Britain
1304 ( "NGT" (float 35 -
1) "NST" "NDT" ) ; Newfoundland
1305 ( "NST" (float 35 -
1) 0 ) ( "NDT" (float 35 -
1) -
1 )
1306 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -
1 ) ; Atlantic
1307 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -
1 ) ; Eastern
1308 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -
1 ) ; Central
1309 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -
1 ) ; Mountain
1310 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -
1 ) ; Pacific
1311 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -
1 ) ; Yukon
1313 "No doc yet. See calc manual for now. ")
1315 (defvar var-TimeZone nil
)
1318 (defvar calendar-current-time-zone-cache
)
1320 (defvar math-calendar-tzinfo
1322 "Information about the timezone, retrieved from the calendar.")
1324 (defun math-get-calendar-tzinfo ()
1325 "Get information about the timezone from the calendar.
1326 The result should be a list of two items about the current time zone:
1327 first, the number of seconds difference from GMT
1328 second, the number of seconds offset for daylight savings."
1329 (if math-calendar-tzinfo
1330 math-calendar-tzinfo
1332 (let ((tzinfo (progn
1333 (calendar-current-time-zone)
1334 calendar-current-time-zone-cache
)))
1335 (setq math-calendar-tzinfo
1336 (list (* 60 (abs (nth 0 tzinfo
)))
1337 (* 60 (nth 1 tzinfo
)))))))
1339 (defun calcFunc-tzone (&optional zone date
)
1341 (cond ((math-realp zone
)
1342 (math-round (math-mul zone
3600)))
1343 ((eq (car zone
) 'hms
)
1344 (math-round (math-mul (math-from-hms zone
'deg
) 3600)))
1346 (math-add (calcFunc-tzone (nth 1 zone
) date
)
1347 (calcFunc-tzone (nth 2 zone
) date
)))
1349 (math-sub (calcFunc-tzone (nth 1 zone
) date
)
1350 (calcFunc-tzone (nth 2 zone
) date
)))
1351 ((eq (car zone
) 'var
)
1352 (let ((name (upcase (symbol-name (nth 1 zone
))))
1354 (if (setq found
(assoc name math-tzone-names
))
1355 (calcFunc-tzone (math-add (nth 1 found
)
1356 (if (integerp (nth 2 found
))
1359 (math-daylight-savings-adjust
1363 (if (equal name
"LOCAL")
1364 (calcFunc-tzone nil date
)
1365 (math-reject-arg zone
"*Unrecognized time zone name")))))
1366 (t (math-reject-arg zone
"*Expected a time zone")))
1367 (if (calc-var-value 'var-TimeZone
)
1368 (calcFunc-tzone (calc-var-value 'var-TimeZone
) date
)
1369 (let ((tzinfo (math-get-calendar-tzinfo)))
1371 (* (math-cal-daylight-savings-adjust date
) (nth 1 tzinfo
)))))))
1373 (defvar math-daylight-savings-hook
'math-std-daylight-savings
)
1375 (defun math-daylight-savings-adjust (date zone
&optional dt
)
1376 (or date
(setq date
(nth 1 (calcFunc-now))))
1378 (if (eq (car-safe date
) 'date
)
1381 (if (and date
(math-realp date
))
1382 (let ((zadj (assoc zone math-tzone-names
)))
1383 (if zadj
(setq bump -
1
1384 date
(math-sub date
(math-div (nth 1 zadj
)
1386 (math-reject-arg date
'datep
)))
1387 (setq date
(math-float date
))
1388 (or dt
(setq dt
(math-date-to-dt date
)))
1389 (and math-daylight-savings-hook
1390 (funcall math-daylight-savings-hook date dt zone bump
))))
1392 ;;; Based on part of dst-adjust-time in cal-dst.el
1393 ;;; For calcFunc-dst, when zone=nil
1394 (defun math-cal-daylight-savings-adjust (date)
1395 "Return -1 if DATE is using daylight saving, 0 otherwise."
1397 (unless date
(setq date
(calcFunc-now)))
1398 (let* ((dt (math-date-to-dt date
))
1403 (+ (nth 3 dt
) (/ (nth 4 dt
) 60.0)))
1408 (calendar-absolute-from-gregorian
1409 (list (nth 1 dt
) (nth 2 dt
) (nth 0 dt
)))
1410 (/ (round (* 60 time
)) 60.0 24.0))))
1411 (if (dst-in-effect rounded-abs-date
)
1415 (defun calcFunc-dsadj (date &optional zone
)
1417 (or (eq (car-safe zone
) 'var
)
1418 (math-reject-arg zone
"*Time zone variable expected"))
1419 (setq zone
(calc-var-value 'var-TimeZone
)))
1422 (setq zone
(and (eq (car-safe zone
) 'var
)
1423 (upcase (symbol-name (nth 1 zone
)))))
1424 (let ((zadj (assoc zone math-tzone-names
)))
1425 (or zadj
(math-reject-arg zone
"*Unrecognized time zone name"))
1426 (if (integerp (nth 2 zadj
))
1428 (math-daylight-savings-adjust date zone
))))
1429 (math-cal-daylight-savings-adjust date
)))
1431 ;; (defun calcFunc-dsadj (date &optional zone)
1433 ;; (or (eq (car-safe zone) 'var)
1434 ;; (math-reject-arg zone "*Time zone variable expected"))
1435 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1438 ;; (calc-var-value 'var-TimeZone)))))
1439 ;; (setq zone (and (eq (car-safe zone) 'var)
1440 ;; (upcase (symbol-name (nth 1 zone)))))
1441 ;; (let ((zadj (assoc zone math-tzone-names)))
1442 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1443 ;; (if (integerp (nth 2 zadj))
1445 ;; (math-daylight-savings-adjust date zone))))
1447 (defun calcFunc-tzconv (date z1 z2
)
1448 (if (math-realp date
)
1449 (nth 1 (calcFunc-tzconv (list 'date date
) z1 z2
))
1450 (calcFunc-unixtime (calcFunc-unixtime date z1
) z2
)))
1452 (defun math-std-daylight-savings (date dt zone bump
)
1453 "Standard North American daylight saving algorithm.
1454 Before 2007, this uses `math-std-daylight-savings-old', where
1455 daylight saving began on the first Sunday of April at 2 a.m.,
1456 and ended on the last Sunday of October at 2 a.m.
1457 As of 2007, this uses `math-std-daylight-savings-new', where
1458 daylight saving begins on the second Sunday of March at 2 a.m.,
1459 and ends on the first Sunday of November at 2 a.m."
1460 (if (< (car dt
) 2007)
1461 (math-std-daylight-savings-old date dt zone bump
)
1462 (math-std-daylight-savings-new date dt zone bump
)))
1464 (defun math-std-daylight-savings-new (date dt zone bump
)
1465 "Standard North American daylight saving algorithm as of 2007.
1466 This implements the rules for the U.S. and Canada.
1467 Daylight saving begins on the second Sunday of March at 2 a.m.,
1468 and ends on the first Sunday of November at 2 a.m."
1469 (cond ((< (nth 1 dt
) 3) 0)
1471 (let ((sunday (math-prev-weekday-in-month date dt
14 0)))
1472 (cond ((< (nth 2 dt
) sunday
) 0)
1473 ((= (nth 2 dt
) sunday
)
1474 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1476 ((< (nth 1 dt
) 11) -
1)
1478 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1479 (cond ((< (nth 2 dt
) sunday
) -
1)
1480 ((= (nth 2 dt
) sunday
)
1481 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1485 (defun math-std-daylight-savings-old (date dt zone bump
)
1486 "Standard North American daylight saving algorithm before 2007.
1487 This implements the rules for the U.S. and Canada.
1488 Daylight saving begins on the first Sunday of April at 2 a.m.,
1489 and ends on the last Sunday of October at 2 a.m."
1490 (cond ((< (nth 1 dt
) 4) 0)
1492 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1493 (cond ((< (nth 2 dt
) sunday
) 0)
1494 ((= (nth 2 dt
) sunday
)
1495 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1497 ((< (nth 1 dt
) 10) -
1)
1499 (let ((sunday (math-prev-weekday-in-month date dt
31 0)))
1500 (cond ((< (nth 2 dt
) sunday
) -
1)
1501 ((= (nth 2 dt
) sunday
)
1502 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1506 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1507 ;;; day of the given month.
1508 (defun math-prev-weekday-in-month (date dt day wday
)
1509 (or day
(setq day
(nth 2 dt
)))
1510 (if (> day
(math-days-in-month (car dt
) (nth 1 dt
)))
1511 (setq day
(math-days-in-month (car dt
) (nth 1 dt
))))
1512 (let ((zeroth (math-sub (math-floor date
) (nth 2 dt
))))
1513 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day
))) zeroth
)))
1515 (defun calcFunc-pwday (date &optional day weekday
)
1516 (if (eq (car-safe date
) 'date
)
1517 (setq date
(nth 1 date
)))
1518 (or (math-realp date
)
1519 (math-reject-arg date
'datep
))
1520 (if (math-messy-integerp day
) (setq day
(math-trunc day
)))
1521 (or (integerp day
) (math-reject-arg day
'fixnump
))
1522 (if (= day
0) (setq day
31))
1523 (and (or (< day
7) (> day
31)) (math-reject-arg day
'range
))
1524 (math-prev-weekday-in-month date
(math-date-to-dt date
) day
(or weekday
0)))
1527 (defun calcFunc-newweek (date &optional weekday
)
1528 (if (eq (car-safe date
) 'date
)
1529 (setq date
(nth 1 date
)))
1530 (or (math-realp date
)
1531 (math-reject-arg date
'datep
))
1532 (or weekday
(setq weekday
0))
1533 (and (math-messy-integerp weekday
) (setq weekday
(math-trunc weekday
)))
1534 (or (integerp weekday
) (math-reject-arg weekday
'fixnump
))
1535 (and (or (< weekday
0) (> weekday
6)) (math-reject-arg weekday
'range
))
1536 (setq date
(math-floor date
))
1537 (list 'date
(math-sub date
(calcFunc-weekday (math-sub date weekday
)))))
1539 (defun calcFunc-newmonth (date &optional day
)
1540 (or day
(setq day
1))
1541 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1542 (or (integerp day
) (math-reject-arg day
'fixnump
))
1543 (and (or (< day
0) (> day
31)) (math-reject-arg day
'range
))
1544 (let* ((dt (math-date-to-dt date
))
1545 (dim (math-days-in-month (car dt
) (nth 1 dt
)))
1546 (julian (if calc-gregorian-switch
1547 (math-date-to-dt (math-sub
1548 (or (nth 3 calc-gregorian-switch
)
1549 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))
1551 (if (or (= day
0) (> day dim
))
1553 (setq day
(1- day
)))
1554 ;; Adjust if this occurs near the switch to the Gregorian calendar
1555 (if calc-gregorian-switch
1557 ((and (math-dt-before-p (list (car dt
) (nth 1 dt
) 1) calc-gregorian-switch
)
1558 (math-dt-before-p julian
(list (car dt
) (nth 1 dt
) 1)))
1559 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the month
1561 (math-dt-to-date (list (car calc-gregorian-switch
)
1562 (nth 1 calc-gregorian-switch
)
1563 (if (> (+ (nth 2 calc-gregorian-switch
) day
) dim
)
1565 (+ (nth 2 calc-gregorian-switch
) day
))))))
1566 ((and (eq (car dt
) (car calc-gregorian-switch
))
1567 (= (nth 1 dt
) (nth 1 calc-gregorian-switch
)))
1568 ;; In this case, the switch to the Gregorian calendar occurs in the given month
1569 (if (< (+ (nth 2 julian
) day
) (nth 2 calc-gregorian-switch
))
1570 ;; If the DAYth day occurs before the switch, use it
1571 (list 'date
(math-dt-to-date (list (car dt
) (nth 1 dt
) (1+ day
))))
1572 ;; Otherwise do some computations
1573 (let ((tm (+ day
(- (nth 2 calc-gregorian-switch
) (nth 2 julian
)))))
1574 (list 'date
(math-dt-to-date
1578 (if (> tm dim
) dim tm
)))))))
1579 ((and (eq (car dt
) (car julian
))
1580 (= (nth 1 dt
) (nth 1 julian
)))
1581 ;; In this case, the current month is truncated because of the switch
1582 ;; to the Gregorian calendar
1583 (list 'date
(math-dt-to-date
1586 (if (>= day
(nth 2 julian
))
1591 (list 'date
(math-add (math-dt-to-date (list (car dt
) (nth 1 dt
) 1)) day
))))
1592 (list 'date
(math-add (math-dt-to-date (list (car dt
) (nth 1 dt
) 1)) day
)))))
1594 (defun calcFunc-newyear (date &optional day
)
1595 (if (eq (car-safe date
) 'date
) (setq date
(nth 1 date
)))
1596 (or day
(setq day
1))
1597 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1598 (or (integerp day
) (math-reject-arg day
'fixnump
))
1599 (let* ((dt (math-date-to-dt date
))
1600 (gregbeg (if calc-gregorian-switch
1601 (or (nth 3 calc-gregorian-switch
)
1602 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))))
1603 (julianend (if calc-gregorian-switch
(math-sub gregbeg
1)))
1604 (julian (if calc-gregorian-switch
1605 (math-date-to-dt julianend
))))
1606 (if (and (>= day
0) (<= day
366))
1607 (let ((max (if (math-leap-year-p (car dt
)) 366 365)))
1608 (if (or (= day
0) (> day max
)) (setq day max
))
1609 (if calc-gregorian-switch
1610 ;; Now to break this down into cases
1612 ((and (math-dt-before-p (list (car dt
) 1 1) calc-gregorian-switch
)
1613 (math-dt-before-p julian
(list (car dt
) 1 1)))
1614 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the year
1615 (list 'date
(math-min (math-add gregbeg
(1- day
))
1616 (math-dt-to-date (list (car calc-gregorian-switch
) 12 31)))))
1617 ((eq (car dt
) (car julian
))
1618 ;; In this case, the switch to the Gregorian calendar occurs in the given year
1619 (if (Math-lessp (car julian
) (car calc-gregorian-switch
))
1620 ;; Here, the last Julian day is the last day of the year.
1621 (list 'date
(math-min (math-add (math-dt-to-date (list (car dt
) 1 1)) (1- day
))
1623 ;; Otherwise, just make sure the date doesn't go past the end of the year
1624 (list 'date
(math-min (math-add (math-dt-to-date (list (car dt
) 1 1)) (1- day
))
1625 (math-dt-to-date (list (car dt
) 12 31))))))
1627 (list 'date
(math-add (math-dt-to-date (list (car dt
) 1 1))
1629 (list 'date
(math-add (math-dt-to-date (list (car dt
) 1 1))
1631 (if (and (>= day -
12) (<= day -
1))
1632 (if (and calc-gregorian-switch
1633 (math-dt-before-p (list (car dt
) (- day
) 1) calc-gregorian-switch
)
1634 (math-dt-before-p julian
(list (car dt
) (- day
) 1)))
1635 (list 'date gregbeg
)
1636 (list 'date
(math-dt-to-date (list (car dt
) (- day
) 1))))
1637 (math-reject-arg day
'range
)))))
1639 (defun calcFunc-incmonth (date &optional step
)
1640 (or step
(setq step
1))
1641 (and (math-messy-integerp step
) (setq step
(math-trunc step
)))
1642 (or (math-integerp step
) (math-reject-arg step
'integerp
))
1643 (let* ((dt (math-date-to-dt date
))
1645 (month (math-add (1- (nth 1 dt
)) step
))
1646 (extra (calcFunc-idiv month
12))
1648 (setq month
(1+ (math-sub month
(math-mul extra
12)))
1649 year
(math-add year extra
)
1650 day
(min day
(math-days-in-month year month
)))
1651 (and (math-posp (car dt
)) (not (math-posp year
))
1652 (setq year
(math-sub year
1))) ; did we go past the year zero?
1653 (and (math-negp (car dt
)) (not (math-negp year
))
1654 (setq year
(math-add year
1)))
1655 (list 'date
(math-dt-to-date
1656 (cons year
(cons month
(cons day
(cdr (cdr (cdr dt
))))))))))
1658 (defun calcFunc-incyear (date &optional step
)
1659 (calcFunc-incmonth date
(math-mul (or step
1) 12)))
1663 (defun calcFunc-bsub (a b
)
1664 (or (eq (car-safe a
) 'date
)
1665 (math-reject-arg a
'datep
))
1666 (if (eq (car-safe b
) 'date
)
1667 (if (math-lessp (nth 1 a
) (nth 1 b
))
1668 (math-neg (calcFunc-bsub b a
))
1669 (math-setup-holidays b
)
1670 (let* ((da (math-to-business-day a
))
1671 (db (math-to-business-day b
)))
1672 (math-add (math-sub (car da
) (car db
))
1673 (if (and (cdr db
) (not (cdr da
))) 1 0))))
1674 (calcFunc-badd a
(math-neg b
))))
1676 (defvar math-holidays-cache nil
)
1677 (defvar math-holidays-cache-tag t
)
1678 (defun calcFunc-badd (a b
)
1679 (if (eq (car-safe b
) 'date
)
1680 (if (eq (car-safe a
) 'date
)
1681 (math-reject-arg nil
"*Invalid combination in date arithmetic")
1682 (calcFunc-badd b a
))
1683 (if (eq (car-safe a
) 'date
)
1687 (let* ((d (math-to-business-day a
))
1688 (bb (math-add (car d
)
1689 (if (and (cdr d
) (Math-posp b
))
1690 (math-sub b
1) b
))))
1691 (or (math-from-business-day bb
)
1692 (calcFunc-badd a b
))))
1693 (if (eq (car-safe b
) 'hms
)
1694 (let ((hours (nth 7 math-holidays-cache
)))
1695 (setq b
(math-div (math-from-hms b
'deg
) 24))
1697 (setq b
(math-div b
(cdr hours
))))
1698 (calcFunc-badd a b
))
1699 (math-reject-arg nil
"*Invalid combination in date arithmetic")))
1700 (math-reject-arg a
'datep
))))
1702 (defun calcFunc-holiday (a)
1703 (if (cdr (math-to-business-day a
)) 1 0))
1705 ;;; Compute the number of business days since Jan 1, 1 AD.
1707 (defun math-to-business-day (date &optional need-year
)
1708 (if (eq (car-safe date
) 'date
)
1709 (setq date
(nth 1 date
)))
1710 (or (Math-realp date
)
1711 (math-reject-arg date
'datep
))
1712 (let* ((day (math-floor date
))
1713 (time (math-sub date day
))
1714 (dt (math-date-to-dt day
))
1717 (or (not need-year
) (eq (car dt
) need-year
)
1718 (math-reject-arg (list 'date day
) "*Generated holiday has wrong year"))
1719 (math-setup-holidays date
)
1720 (let ((days (car math-holidays-cache
)))
1721 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1722 (setq delta
(1+ delta
)))
1723 (and days
(= day
(car days
))
1725 (let* ((weekdays (nth 3 math-holidays-cache
))
1726 (weeks (1- (/ (+ day
6) 7)))
1727 (wkday (- day
1 (* weeks
7))))
1728 (setq delta
(+ delta
(* weeks
(length weekdays
))))
1729 (while (and weekdays
(< (car weekdays
) wkday
))
1730 (setq weekdays
(cdr weekdays
)
1732 (and weekdays
(eq wkday
(car weekdays
))
1734 (let ((hours (nth 7 math-holidays-cache
)))
1737 (setq time
(math-div (math-sub time
(car hours
)) (cdr hours
)))
1738 (if (Math-lessp time
0) (setq time
0))
1739 (or (Math-lessp time
1)
1742 (math-div 1 (math-mul 86400 (cdr hours
)))))))))
1743 (cons (math-add (math-sub day delta
) time
) holiday
)))
1746 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1747 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1749 (defun math-from-business-day (num)
1750 (let* ((day (math-floor num
))
1751 (time (math-sub num day
)))
1753 (math-reject-arg nil
"*Date is outside valid range"))
1754 (math-setup-holidays)
1755 (let ((days (nth 1 math-holidays-cache
))
1757 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1758 (setq delta
(1+ delta
)))
1759 (setq day
(+ day delta
)))
1760 (let* ((weekdays (nth 3 math-holidays-cache
))
1761 (bweek (- 7 (length weekdays
)))
1762 (weeks (1- (/ (+ day
(1- bweek
)) bweek
)))
1763 (wkday (- day
1 (* weeks bweek
)))
1765 (setq day
(+ day
(* weeks
(length weekdays
))))
1766 (while (if (memq w weekdays
)
1768 (> (setq wkday
(1- wkday
)) 0))
1770 (let ((hours (nth 7 math-holidays-cache
)))
1772 (setq time
(math-add (math-mul time
(cdr hours
)) (car hours
)))))
1773 (and (not (math-setup-holidays day
))
1774 (list 'date
(math-add day time
))))))
1776 ;; The variable math-sh-year is local to math-setup-holidays
1777 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1778 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1779 (defvar math-sh-year
)
1781 (defun math-setup-holidays (&optional date
)
1782 (or (eq (calc-var-value 'var-Holidays
) math-holidays-cache-tag
)
1783 (let ((h (calc-var-value 'var-Holidays
))
1784 (wdnames '( (sun .
0) (mon .
1) (tue .
2) (wed .
3)
1785 (thu .
4) (fri .
5) (sat .
6) ))
1786 (days nil
) (weekdays nil
) (exprs nil
) (limit nil
) (hours nil
))
1787 (or (math-vectorp h
)
1788 (math-reject-arg h
"*Holidays variable must be a vector"))
1789 (while (setq h
(cdr h
))
1790 (cond ((or (and (eq (car-safe (car h
)) 'date
)
1791 (integerp (nth 1 (car h
))))
1792 (and (eq (car-safe (car h
)) 'intv
)
1793 (eq (car-safe (nth 2 (car h
))) 'date
))
1794 (eq (car-safe (car h
)) 'vec
))
1795 (setq days
(cons (car h
) days
)))
1796 ((and (eq (car-safe (car h
)) 'var
)
1797 (assq (nth 1 (car h
)) wdnames
))
1798 (setq weekdays
(cons (cdr (assq (nth 1 (car h
)) wdnames
))
1800 ((and (eq (car-safe (car h
)) 'intv
)
1801 (eq (car-safe (nth 2 (car h
))) 'hms
)
1802 (eq (car-safe (nth 3 (car h
))) 'hms
))
1805 (car h
) "*Only one hours interval allowed in Holidays"))
1806 (setq hours
(math-div (car h
) '(hms 24 0 0)))
1807 (if (or (Math-lessp (nth 2 hours
) 0)
1808 (Math-lessp 1 (nth 3 hours
)))
1810 (car h
) "*Hours interval out of range"))
1811 (setq hours
(cons (nth 2 hours
)
1812 (math-sub (nth 3 hours
) (nth 2 hours
))))
1813 (if (Math-zerop (cdr hours
))
1815 (car h
) "*Degenerate hours interval")))
1816 ((or (and (eq (car-safe (car h
)) 'intv
)
1817 (Math-integerp (nth 2 (car h
)))
1818 (Math-integerp (nth 3 (car h
))))
1819 (and (integerp (car h
))
1820 (> (car h
) 1900) (< (car h
) 2100)))
1823 (car h
) "*Only one limit allowed in Holidays"))
1824 (setq limit
(calcFunc-vint (car h
) '(intv 3 1 2737)))
1825 (if (equal limit
'(vec))
1826 (math-reject-arg (car h
) "*Limit is out of range")))
1827 ((or (math-expr-contains (car h
) '(var y var-y
))
1828 (math-expr-contains (car h
) '(var m var-m
)))
1829 (setq exprs
(cons (car h
) exprs
)))
1831 (car h
) "*Holidays must contain a vector of holidays"))))
1832 (if (= (length weekdays
) 7)
1833 (math-reject-arg nil
"*Too many weekend days"))
1834 (setq math-holidays-cache
(list (list -
1) ; 0: days list
1835 (list -
1) ; 1: inverse-days list
1838 (or limit
'(intv 3 1 2737))
1839 nil
; 5: (lo.hi) expanded years
1841 hours
) ; 7: business hours
1842 math-holidays-cache-tag
(calc-var-value 'var-Holidays
))))
1844 (let ((year (calcFunc-year date
))
1845 (limits (nth 5 math-holidays-cache
))
1847 (or (eq (calcFunc-in year
(nth 4 math-holidays-cache
)) 1)
1849 (or (eq (car-safe date
) 'date
) (setq date
(list 'date date
)))
1850 (math-reject-arg date
"*Date is outside valid range")))
1852 (let ((days (nth 6 math-holidays-cache
)))
1854 (let ((math-sh-year nil
)) ; see below
1855 (setcar (nthcdr 6 math-holidays-cache
) nil
)
1856 (math-setup-add-holidays (cons 'vec
(cdr days
)))
1857 (setcar (nthcdr 2 math-holidays-cache
) (car days
))))
1858 (cond ((not (nth 2 math-holidays-cache
))
1862 (setcar (nthcdr 5 math-holidays-cache
) (cons year year
))
1863 (math-setup-year-holidays year
)
1865 ((< year
(car limits
))
1866 (message "Computing holidays, %d .. %d"
1867 year
(1- (car limits
)))
1868 (calc-set-command-flag 'clear-message
)
1869 (while (< year
(car limits
))
1870 (setcar limits
(1- (car limits
)))
1871 (math-setup-year-holidays (car limits
)))
1873 ((> year
(cdr limits
))
1874 (message "Computing holidays, %d .. %d"
1875 (1+ (cdr limits
)) year
)
1876 (calc-set-command-flag 'clear-message
)
1877 (while (> year
(cdr limits
))
1878 (setcdr limits
(1+ (cdr limits
)))
1879 (math-setup-year-holidays (cdr limits
)))
1884 (or done
(setq math-holidays-cache-tag t
))))))
1886 (defun math-setup-year-holidays (math-sh-year)
1887 (let ((exprs (nth 2 math-holidays-cache
)))
1889 (let* ((var-y math-sh-year
)
1891 (expr (math-evaluate-expr (car exprs
))))
1892 (if (math-expr-contains expr
'(var m var-m
))
1894 (while (<= (setq var-m
(1+ var-m
)) 12)
1895 (math-setup-add-holidays (math-evaluate-expr expr
))))
1896 (math-setup-add-holidays expr
)))
1897 (setq exprs
(cdr exprs
)))))
1899 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
1900 (cond ((eq (car-safe days
) 'vec
)
1901 (while (setq days
(cdr days
))
1902 (math-setup-add-holidays (car days
))))
1903 ((eq (car-safe days
) 'intv
)
1904 (let ((day (math-ceiling (nth 2 days
))))
1905 (or (eq (calcFunc-in day days
) 1)
1906 (setq day
(math-add day
1)))
1907 (while (eq (calcFunc-in day days
) 1)
1908 (math-setup-add-holidays day
)
1909 (setq day
(math-add day
1)))))
1910 ((eq (car-safe days
) 'date
)
1911 (math-setup-add-holidays (nth 1 days
)))
1914 (let ((b (math-to-business-day days math-sh-year
)))
1915 (or (cdr b
) ; don't register holidays twice!
1916 (let ((prev (car math-holidays-cache
))
1917 (iprev (nth 1 math-holidays-cache
)))
1918 (while (and (cdr prev
) (< (nth 1 prev
) days
))
1919 (setq prev
(cdr prev
) iprev
(cdr iprev
)))
1920 (setcdr prev
(cons days
(cdr prev
)))
1921 (setcdr iprev
(cons (car b
) (cdr iprev
)))
1922 (while (setq iprev
(cdr iprev
))
1923 (setcar iprev
(1- (car iprev
))))))))
1925 (math-reject-arg (list 'date days
) "*Invalid holiday value"))
1927 (math-reject-arg days
"*Holiday formula failed to evaluate"))))
1934 ;;; Build a standard deviation form. [X X X]
1935 (defun math-make-sdev (x sigma
)
1936 (if (memq (car-safe x
) '(date mod sdev intv vec
))
1937 (math-reject-arg x
'realp
))
1938 (if (memq (car-safe sigma
) '(date mod sdev intv vec
))
1939 (math-reject-arg sigma
'realp
))
1940 (if (or (Math-negp sigma
) (memq (car-safe sigma
) '(cplx polar
)))
1941 (setq sigma
(math-abs sigma
)))
1942 (if (and (Math-zerop sigma
) (Math-scalarp x
))
1944 (list 'sdev x sigma
)))
1945 (defun calcFunc-sdev (x sigma
)
1946 (math-make-sdev x sigma
))
1952 (defun math-normalize-mod (a)
1953 (let ((n (math-normalize (nth 1 a
)))
1954 (m (math-normalize (nth 2 a
))))
1955 (if (and (math-anglep n
) (math-anglep m
) (math-posp m
))
1957 (math-normalize (list 'calcFunc-makemod n m
)))))
1959 ;;; Build a modulo form. [N R R]
1960 (defun math-make-mod (n m
)
1961 (setq calc-previous-modulo m
)
1963 (cond ((not (Math-anglep m
))
1964 (math-reject-arg m
'anglep
))
1965 ((not (math-posp m
))
1966 (math-reject-arg m
'posp
))
1968 (if (or (Math-negp n
)
1969 (not (Math-lessp n m
)))
1970 (list 'mod
(math-mod n m
) m
)
1972 ((memq (car n
) '(+ -
/ vec neg
))
1975 (mapcar (function (lambda (x) (math-make-mod x m
)))
1977 ((and (eq (car n
) '*) (Math-anglep (nth 1 n
)))
1978 (math-mul (math-make-mod (nth 1 n
) m
) (nth 2 n
)))
1979 ((memq (car n
) '(* ^ var calcFunc-subscr
))
1980 (math-mul (math-make-mod 1 m
) n
))
1981 (t (math-reject-arg n
'anglep
)))))
1982 (defun calcFunc-makemod (n m
)
1983 (math-make-mod n m
))
1987 ;;;; Interval forms.
1989 ;;; Build an interval form. [X S X X]
1990 (defun math-make-intv (mask lo hi
)
1991 (if (memq (car-safe lo
) '(cplx polar mod sdev intv vec
))
1992 (math-reject-arg lo
'realp
))
1993 (if (memq (car-safe hi
) '(cplx polar mod sdev intv vec
))
1994 (math-reject-arg hi
'realp
))
1995 (or (eq (eq (car-safe lo
) 'date
) (eq (car-safe hi
) 'date
))
1996 (math-reject-arg (if (eq (car-safe lo
) 'date
) hi lo
) 'datep
))
1997 (if (and (or (Math-realp lo
) (eq (car lo
) 'date
))
1998 (or (Math-realp hi
) (eq (car hi
) 'date
)))
1999 (let ((cmp (math-compare lo hi
)))
2003 (list 'intv mask lo hi
))
2006 (list 'intv
2 lo lo
)
2007 (list 'intv mask lo lo
))
2008 (list 'intv mask lo hi
))))
2009 (list 'intv mask lo hi
)))
2010 (defun calcFunc-intv (mask lo hi
)
2011 (if (math-messy-integerp mask
) (setq mask
(math-trunc mask
)))
2012 (or (natnump mask
) (math-reject-arg mask
'fixnatnump
))
2013 (or (<= mask
3) (math-reject-arg mask
'range
))
2014 (math-make-intv mask lo hi
))
2016 (defun math-sort-intv (mask lo hi
)
2017 (if (Math-lessp hi lo
)
2018 (math-make-intv (aref [0 2 1 3] mask
) hi lo
)
2019 (math-make-intv mask lo hi
)))
2024 (defun math-combine-intervals (a am b bm c cm d dm
)
2026 (if (= (setq res
(math-compare a c
)) 1)
2029 (setq am
(or am cm
))))
2030 (if (= (setq res
(math-compare b d
)) -
1)
2033 (setq bm
(or bm dm
))))
2034 (math-make-intv (+ (if am
2 0) (if bm
1 0)) a b
)))
2037 (defun math-div-mod (a b m
) ; [R R R R] (Returns nil if no solution)
2038 (and (Math-integerp a
) (Math-integerp b
) (Math-integerp m
)
2039 (let ((u1 1) (u3 b
) (v1 0) (v3 m
))
2040 (while (not (eq v3
0)) ; See Knuth sec 4.5.2, exercise 15
2041 (let* ((q (math-idivmod u3 v3
))
2042 (t1 (math-sub u1
(math-mul v1
(car q
)))))
2043 (setq u1 v1 u3 v3 v1 t1 v3
(cdr q
))))
2044 (let ((q (math-idivmod a u3
)))
2046 (math-mod (math-mul (car q
) u1
) m
))))))
2048 (defun math-mod-intv (a b
)
2049 (let* ((q1 (math-floor (math-div (nth 2 a
) b
)))
2050 (q2 (math-floor (math-div (nth 3 a
) b
)))
2051 (m1 (math-sub (nth 2 a
) (math-mul q1 b
)))
2052 (m2 (math-sub (nth 3 a
) (math-mul q2 b
))))
2053 (cond ((equal q1 q2
)
2054 (math-sort-intv (nth 1 a
) m1 m2
))
2055 ((and (math-equal-int (math-sub q2 q1
) 1)
2057 (memq (nth 1 a
) '(0 2)))
2058 (math-make-intv (nth 1 a
) m1 b
))
2060 (math-make-intv 2 0 b
)))))
2062 ;; The variables math-exp-str and math-exp-pos are local to
2063 ;; math-read-exprs in math-aent.el, but are used by
2064 ;; math-read-angle-brackets, which is called (indirectly) by
2066 (defvar math-exp-str
)
2067 (defvar math-exp-pos
)
2069 (defun math-read-angle-brackets ()
2070 (let* ((last (or (math-check-for-commas t
) (length math-exp-str
)))
2071 (str (substring math-exp-str math-exp-pos last
))
2073 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str
)
2074 (let ((str1 (substring str
0 (1- (match-end 0))))
2075 (str2 (substring str
(match-end 0)))
2076 (calc-hashes-used 0))
2077 (setq str1
(math-read-expr (concat "[" str1
"]")))
2078 (if (eq (car-safe str1
) 'error
)
2080 (setq str2
(math-read-expr str2
))
2081 (if (eq (car-safe str2
) 'error
)
2083 (append '(calcFunc-lambda) (cdr str1
) (list str2
)))))
2084 (if (string-match "#" str
)
2085 (let ((calc-hashes-used 0))
2086 (and (setq str
(math-read-expr str
))
2087 (if (eq (car-safe str
) 'error
)
2089 (append '(calcFunc-lambda)
2090 (calc-invent-args calc-hashes-used
)
2092 (math-parse-date str
)))))
2094 (throw 'syntax res
))
2095 (if (eq (car-safe res
) 'error
)
2096 (throw 'syntax
(nth 2 res
)))
2097 (setq math-exp-pos
(1+ last
))
2101 (provide 'calc-forms
)
2103 ;;; calc-forms.el ends here