1 ;;; calc-forms.el --- data format conversion functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
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]\\|10\\|11\\) *\\'" 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 ;; Find time part to put in <...>
98 (string-match "\\`[^hHspPT]*\\([^ac-gi-lnoqrt-zAC-GI-OQRU-Z]*\\(bs\\|bm\\|bh\\|BS\\|BH\\|[hHmpPsST]\\)+[^ac-gi-lnoqrt-zAC-GI-OQRU-Z]*\\)[^hHspPT]*\\'" fmt
)
99 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
100 (regexp-quote (math-match-substring fmt
1))
101 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt
)
102 (setq fmt
(concat (substring fmt
0 (match-beginning 0))
104 (substring fmt
(match-beginning 0) (match-end 0))
106 (substring fmt
(match-end 0))))))
111 (let ((case-fold-search nil
))
112 (and (setq temp
(string-match ":[BS]S" fmt
))
114 (while (setq pos
(string-match "[<>a-zA-Z]" fmt
))
116 (setq lfmt
(cons (substring fmt
0 pos
) lfmt
)))
118 (cond ((= (aref fmt pos
) ?\
<)
119 (and time
(error "Nested <'s not allowed"))
120 (and lfmt
(setq fullfmt
(nconc lfmt fullfmt
)
123 ((= (aref fmt pos
) ?\
>)
124 (or time
(error "Misplaced > in format"))
125 (and lfmt
(setq fullfmt
(cons (nreverse lfmt
) fullfmt
)
129 (if (string-match "\\`[^a-zA-Z]*[bBZI][a-zA-Z]" fmt
)
130 (setq pos2
(1+ pos2
)))
131 (while (and (< pos2
(length fmt
))
132 (= (upcase (aref fmt pos2
))
133 (upcase (aref fmt
(1- pos2
)))))
134 (setq pos2
(1+ pos2
)))
135 (setq sym
(intern (substring fmt pos pos2
)))
136 (or (memq sym
'(Y YY BY YYY YYYY
138 aa AA aaa AAA aaaa AAAA
139 bb BB bbb BBB bbbb BBBB
140 M MM BM mmm Mmm Mmmm MMM MMMM
142 W www Www Wwww WWW WWWW
145 m mm bm s ss bs SS BS C
147 (and (eq sym
'X
) (not lfmt
) (not fullfmt
))
148 (error "Bad format code: %s" sym
))
149 (and (memq sym
'(bb BB bbb BBB bbbb BBBB
))
150 (setq lfmt
(cons 'b lfmt
)))
151 (setq lfmt
(cons sym lfmt
))))
152 (setq fmt
(substring fmt pos2
)))
154 (setq lfmt
(cons fmt lfmt
)))
156 (setq fullfmt
(cons (nreverse lfmt
) fullfmt
))
157 (setq fullfmt
(nconc lfmt fullfmt
))))
158 (calc-change-mode 'calc-date-format
(nreverse fullfmt
) t
))))
161 (defun calc-hms-mode ()
164 (calc-change-mode 'calc-angle-mode
'hms
)
165 (message "Angles measured in degrees-minutes-seconds")))
168 (defun calc-now (arg)
170 (calc-date-zero-args "now" 'calcFunc-now arg
))
172 (defun calc-date-part (arg)
173 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
174 (if (or (< arg
1) (> arg
9))
175 (error "Part code out of range"))
178 (nth arg
'(nil "year" "mnth" "day" "hour" "minu"
179 "sec" "wday" "yday" "hmst"))
180 (list (nth arg
'(nil calcFunc-year calcFunc-month
181 calcFunc-day calcFunc-hour
182 calcFunc-minute calcFunc-second
183 calcFunc-weekday calcFunc-yearday
187 (defun calc-date (arg)
189 (if (or (< arg
1) (> arg
6))
190 (error "Between one and six arguments are allowed"))
192 (calc-enter-result arg
"date" (cons 'calcFunc-date
(calc-top-list-n arg
)))))
194 (defun calc-julian (arg)
196 (calc-date-one-arg "juln" 'calcFunc-julian arg
))
198 (defun calc-unix-time (arg)
200 (calc-date-one-arg "unix" 'calcFunc-unixtime arg
))
202 (defun calc-time-zone (arg)
204 (calc-date-zero-args "zone" 'calcFunc-tzone arg
))
206 (defun calc-convert-time-zones (old &optional new
)
207 (interactive "sFrom time zone: ")
210 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv
(calc-top-list-n 3)))
211 (if (equal old
"") (setq old
"local"))
213 (setq new
(read-string (concat "From time zone: " old
215 (if (stringp old
) (setq old
(math-read-expr old
)))
216 (if (eq (car-safe old
) 'error
)
217 (error "Error in expression: %S" (nth 1 old
)))
218 (if (equal new
"") (setq new
"local"))
219 (if (stringp new
) (setq new
(math-read-expr new
)))
220 (if (eq (car-safe new
) 'error
)
221 (error "Error in expression: %S" (nth 1 new
)))
222 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
223 (calc-top-n 1) old new
)))))
225 (defun calc-new-week (arg)
227 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg
))
229 (defun calc-new-month (arg)
231 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg
))
233 (defun calc-new-year (arg)
235 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg
))
237 (defun calc-inc-month (arg)
239 (calc-date-one-arg "incm" 'calcFunc-incmonth arg
))
241 (defun calc-business-days-plus (arg)
244 (calc-binary-op "bus+" 'calcFunc-badd arg
)))
246 (defun calc-business-days-minus (arg)
249 (calc-binary-op "bus-" 'calcFunc-bsub arg
)))
251 (defun calc-date-zero-args (prefix func arg
)
254 (calc-enter-result 1 prefix
(list func
(calc-top-n 1)))
255 (calc-enter-result 0 prefix
(if arg
256 (list func
(prefix-numeric-value arg
))
259 (defun calc-date-one-arg (prefix func arg
)
262 (calc-enter-result 2 prefix
(cons func
(calc-top-list-n 2)))
263 (calc-enter-result 1 prefix
(if arg
264 (list func
(calc-top-n 1)
265 (prefix-numeric-value arg
))
266 (list func
(calc-top-n 1)))))))
269 ;;;; Hours-minutes-seconds forms.
271 (defun math-normalize-hms (a)
272 (let ((h (math-normalize (nth 1 a
)))
273 (m (math-normalize (nth 2 a
)))
274 (s (let ((calc-internal-prec (max (- calc-internal-prec
4) 3)))
275 (math-normalize (nth 3 a
)))))
278 (and (= h
0) (math-negp m
))
279 (and (= h
0) (= m
0) (math-negp s
)))
282 (setq s
(math-add s -
60)
285 (setq m
(math-add m -
60)
287 (if (not (Math-lessp -
60 s
))
288 (setq s
(math-add s
60)
290 (if (not (Math-lessp -
60 m
))
291 (setq m
(math-add m
60)
294 (setq s
(math-add s
60)
297 (setq m
(math-add m
60)
299 (if (not (Math-lessp s
60))
300 (setq s
(math-add s -
60)
302 (if (not (Math-lessp m
60))
303 (setq m
(math-add m -
60)
305 (if (and (eq (car-safe s
) 'float
)
306 (<= (+ (math-numdigs (nth 1 s
)) (nth 2 s
))
307 (- 2 calc-internal-prec
)))
311 ;;; Convert A from ANG or current angular mode to HMS format.
312 (defun math-to-hms (a &optional ang
) ; [X R] [Public]
313 (cond ((eq (car-safe a
) 'hms
) a
)
314 ((eq (car-safe a
) 'sdev
)
315 (math-make-sdev (math-to-hms (nth 1 a
))
316 (math-to-hms (nth 2 a
))))
317 ((not (Math-numberp a
))
318 (list 'calcFunc-hms a
))
320 (math-neg (math-to-hms (math-neg a
) ang
)))
321 ((eq (or ang calc-angle-mode
) 'rad
)
322 (math-to-hms (math-div a
(math-pi-over-180)) 'deg
))
323 ((memq (car-safe a
) '(cplx polar
)) a
)
325 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
326 ; (math-normalize a)))
328 (let* ((b (math-mul a
3600))
329 (hm (math-trunc (math-div b
60)))
330 (hmd (math-idivmod hm
60)))
334 (math-sub b
(math-mul hm
60))))))))
335 (defun calcFunc-hms (h &optional m s
)
336 (or (Math-realp h
) (math-reject-arg h
'realp
))
338 (or (Math-realp m
) (math-reject-arg m
'realp
))
340 (or (Math-realp s
) (math-reject-arg s
'realp
))
341 (if (and (not (Math-lessp m
0)) (Math-lessp m
60)
342 (not (Math-lessp s
0)) (Math-lessp s
60))
343 (math-add (math-to-hms h
)
345 (math-to-hms (math-add h
346 (math-add (math-div (or m
0) 60)
347 (math-div (or s
0) 3600)))
350 ;;; Convert A from HMS format to ANG or current angular mode.
351 (defun math-from-hms (a &optional ang
) ; [R X] [Public]
352 (cond ((not (eq (car-safe a
) 'hms
))
355 (if (eq (car-safe a
) 'sdev
)
356 (math-make-sdev (math-from-hms (nth 1 a
) ang
)
357 (math-from-hms (nth 2 a
) ang
))
358 (if (eq (or ang calc-angle-mode
) 'rad
)
359 (list 'calcFunc-rad a
)
360 (list 'calcFunc-deg a
)))))
362 (math-neg (math-from-hms (math-neg a
) ang
)))
363 ((eq (or ang calc-angle-mode
) 'rad
)
364 (math-mul (math-from-hms a
'deg
) (math-pi-over-180)))
366 (math-add (math-div (math-add (math-div (nth 3 a
)
375 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
376 ;;; These versions are rewritten to use arbitrary-size integers.
378 ;;; A numerical date is the number of days since midnight on
379 ;;; the morning of December 31, 1 B.C. (Gregorian) or January 2, 1 A.D. (Julian).
380 ;;; Emacs's calendar refers to such a date as an absolute date, some Calc function
381 ;;; names also use that terminology. If the date is a non-integer, it represents
382 ;;; a specific date and time.
383 ;;; A "dt" is a list of the form, (year month day), corresponding to
384 ;;; an integer code, or (year month day hour minute second), corresponding
385 ;;; to a non-integer code.
387 (defun math-date-to-gregorian-dt (date)
388 "Return the day (YEAR MONTH DAY) in the Gregorian calendar.
389 DATE is the number of days since December 31, -1 in the Gregorian calendar."
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 date
) 366 365)))
396 ; this result may be an overestimate
398 (while (Math-lessp date
(setq temp
(math-absolute-from-gregorian-dt 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
)))
403 (if (math-leap-year-p year
)
404 [1 32 61 92 122 153 183 214 245 275 306 336 999]
405 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
406 (while (>= date
(aref temp month
))
407 (setq month
(1+ month
)))
408 (setq day
(1+ (- date
(aref temp
(1- month
)))))
409 (list year month day
)))
411 (defun math-date-to-julian-dt (date)
412 "Return the day (YEAR MONTH DAY) in the Julian calendar.
413 DATE is the number of days since December 31, -1 in the Gregorian calendar."
416 (year (math-quotient (math-add date
(if (Math-lessp date
711859)
417 367 ; for speed, we take
418 -
106)) ; >1950 as a special case
419 (if (math-negp date
) 366 365)))
420 ; this result may be an overestimate
422 (while (Math-lessp date
(setq temp
(math-absolute-from-julian-dt year
1 1)))
423 (setq year
(math-add year -
1)))
424 (if (eq year
0) (setq year -
1))
425 (setq date
(1+ (math-sub date temp
)))
427 (if (math-leap-year-p year t
)
428 [1 32 61 92 122 153 183 214 245 275 306 336 999]
429 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
430 (while (>= date
(aref temp month
))
431 (setq month
(1+ month
)))
432 (setq day
(1+ (- date
(aref temp
(1- month
)))))
433 (list year month day
)))
435 (defun math-date-to-dt (value)
436 "Return the day and time of VALUE.
437 The integer part of VALUE is the number of days since Dec 31, -1
438 in the Gregorian calendar and the remaining part determines the time."
439 (if (eq (car-safe value
) 'date
)
440 (setq value
(nth 1 value
)))
441 (or (math-realp value
)
442 (math-reject-arg value
'datep
))
443 (let* ((parts (math-date-parts value
))
446 (dt (if (and calc-gregorian-switch
449 (nth 3 calc-gregorian-switch
)
450 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))
452 (math-date-to-julian-dt date
)
453 (math-date-to-gregorian-dt date
))))
454 (if (math-integerp value
)
460 (math-add (% time
60) (nth 2 parts
)))))))
462 (defun math-date-to-iso-dt (date)
463 "Return the ISO8601 date (year week day) of DATE."
464 (unless (Math-integerp date
)
465 (setq date
(math-floor date
)))
466 (let* ((approx (nth 0 (math-date-to-gregorian-dt (math-sub date
3))))
467 (year (math-add approx
470 (while (>= (math-compare date
471 (math-absolute-from-iso-dt (setq y
(math-add y
1)) 1 1)) 0)
472 (setq sum
(+ sum
1)))
476 (math-add (car (math-idivmod
477 (math-sub date
(math-absolute-from-iso-dt year
1 1))
480 (let ((day (calcFunc-mod date
7)))
481 (if (= day
0) 7 day
)))))
483 (defun math-dt-to-date (dt)
484 (or (integerp (nth 1 dt
))
485 (math-reject-arg (nth 1 dt
) 'fixnump
))
486 (if (or (< (nth 1 dt
) 1) (> (nth 1 dt
) 12))
487 (math-reject-arg (nth 1 dt
) "Month value is out of range"))
488 (or (integerp (nth 2 dt
))
489 (math-reject-arg (nth 2 dt
) 'fixnump
))
490 (if (or (< (nth 2 dt
) 1) (> (nth 2 dt
) 31))
491 (math-reject-arg (nth 2 dt
) "Day value is out of range"))
492 (let ((date (math-absolute-from-dt (car dt
) (nth 1 dt
) (nth 2 dt
))))
494 (math-add (math-float date
)
495 (math-div (math-add (+ (* (nth 3 dt
) 3600)
501 (defun math-iso-dt-to-date (dt)
502 (let ((date (math-absolute-from-iso-dt (car dt
) (nth 1 dt
) (nth 2 dt
))))
504 (math-add (math-float date
)
505 (math-div (math-add (+ (* (nth 3 dt
) 3600)
511 (defun math-date-parts (value &optional offset
)
512 (let* ((date (math-floor value
))
513 (time (math-round (math-mul (math-sub value
(or offset date
)) 86400)
514 (and (> calc-internal-prec
12)
515 (- calc-internal-prec
12))))
516 (ftime (math-floor time
)))
519 (math-sub time ftime
))))
522 (defun math-this-year ()
523 (nth 5 (decode-time)))
525 (defun math-leap-year-p (year &optional julian
)
526 "Non-nil if YEAR is a leap year.
527 If JULIAN is non-nil, then use the criterion for leap years
528 in the Julian calendar, otherwise use the criterion in the
532 (= (math-imod (math-neg year
) 4) 1)
533 (= (math-imod year
4) 0))
535 (setq year
(math-sub -
1 year
)))
536 (setq year
(math-imod year
400))
537 (or (and (= (% year
4) 0) (/= (% year
100) 0))
540 (defun math-days-in-month (year month
)
541 (if (and (= month
2) (math-leap-year-p year
))
543 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month
))))
545 (defun math-day-in-year (year month day
&optional julian
)
546 "Return the number of days of the year up to YEAR MONTH DAY.
547 The count includes the given date.
548 If JULIAN is non-nil, use the Julian calendar, otherwise
549 use the Gregorian calendar."
550 (let ((day-of-year (+ day
(* 31 (1- month
)))))
553 (setq day-of-year
(- day-of-year
(/ (+ 23 (* 4 month
)) 10)))
554 (if (math-leap-year-p year julian
)
555 (setq day-of-year
(1+ day-of-year
)))))
558 (defun math-day-number (year month day
)
559 "Return the number of days of the year up to YEAR MONTH DAY.
560 The count includes the given date."
561 (if calc-gregorian-switch
562 (cond ((eq year
(nth 0 calc-gregorian-switch
))
564 (- (math-absolute-from-dt year month day
)
565 (math-absolute-from-dt year
1 1))))
566 ((Math-lessp year
(nth 0 calc-gregorian-switch
))
567 (math-day-in-year year month day t
))
569 (math-day-in-year year month day
)))
570 (math-day-in-year year month day
)))
572 (defun math-dt-before-p (dt1 dt2
)
573 "Non-nil if DT1 occurs before DT2.
574 A DT is a list of the form (YEAR MONTH DAY)."
575 (or (Math-lessp (nth 0 dt1
) (nth 0 dt2
))
576 (and (equal (nth 0 dt1
) (nth 0 dt2
))
577 (or (< (nth 1 dt1
) (nth 1 dt2
))
578 (and (= (nth 1 dt1
) (nth 1 dt2
))
579 (< (nth 2 dt1
) (nth 2 dt2
)))))))
581 (defun math-absolute-from-gregorian-dt (year month day
)
582 "Return the DATE of the day given by the Gregorian day YEAR MONTH DAY.
583 Recall that DATE is the number of days since December 31, -1
584 in the Gregorian calendar."
585 (if (eq year
0) (setq year -
1))
586 (let ((yearm1 (math-sub year
1)))
588 ;; Add the number of days of the year and the numbers of days
589 ;; in the previous years (leap year days to be added separately)
590 (math-add (math-day-in-year year month day
)
591 (math-add (math-mul 365 yearm1
)
592 ;; Add the number of Julian leap years
594 (math-quotient yearm1
4)
596 (math-quotient (math-sub 3 year
)
598 ;; Subtract the number of Julian leap years which are not
599 ;; Gregorian leap years. In C=4N+r centuries, there will
600 ;; be 3N+r of these days. The following will compute
602 (let* ((correction (math-mul (math-quotient yearm1
100) 3))
603 (res (math-idivmod correction
4)))
604 (math-add (if (= (cdr res
) 0)
609 (defun math-absolute-from-julian-dt (year month day
)
610 "Return the DATE of the day given by the Julian day YEAR MONTH DAY.
611 Recall that DATE is the number of days since December 31, -1
612 in the Gregorian calendar."
613 (if (eq year
0) (setq year -
1))
614 (let ((yearm1 (math-sub year
1)))
616 ;; Add the number of days of the year and the numbers of days
617 ;; in the previous years (leap year days to be added separately)
618 (math-add (math-day-in-year year month day
)
619 (math-add (math-mul 365 yearm1
)
620 ;; Add the number of Julian leap years
622 (math-quotient yearm1
4)
624 (math-quotient (math-sub 3 year
)
626 ;; Adjustment, since January 1, 1 (Julian) is absolute day -1
629 ;; calc-gregorian-switch is a customizable variable defined in calc.el
630 (defvar calc-gregorian-switch
)
632 (defun math-absolute-from-iso-dt (year week day
)
633 "Return the DATE of the day given by the iso8601 day YEAR WEEK DAY."
634 (let* ((janfour (math-absolute-from-gregorian-dt year
1 4))
635 (prevmon (math-sub janfour
636 (cdr (math-idivmod (math-sub janfour
1) 7)))))
638 (math-add prevmon
(* (1- week
) 7))
639 (if (zerop day
) 6 (1- day
)))))
641 (defun math-absolute-from-dt (year month day
)
642 "Return the DATE of the day given by the day YEAR MONTH DAY.
643 Recall that DATE is the number of days since December 31, -1
644 in the Gregorian calendar."
645 (if (and calc-gregorian-switch
646 ;; The next few lines determine if the given date
647 ;; occurs before the switch to the Gregorian calendar.
648 (math-dt-before-p (list year month day
) calc-gregorian-switch
))
649 (math-absolute-from-julian-dt year month day
)
650 (math-absolute-from-gregorian-dt year month day
)))
652 ;;; It is safe to redefine these in your init file to use a different
655 (defvar math-long-weekday-names
'( "Sunday" "Monday" "Tuesday" "Wednesday"
656 "Thursday" "Friday" "Saturday" ))
657 (defvar math-short-weekday-names
'( "Sun" "Mon" "Tue" "Wed"
660 (defvar math-long-month-names
'( "January" "February" "March" "April"
661 "May" "June" "July" "August"
662 "September" "October" "November" "December" ))
663 (defvar math-short-month-names
'( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
664 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
667 (defvar math-format-date-cache nil
)
669 ;; The variables math-fd-date, math-fd-dt, math-fd-year,
670 ;; math-fd-month, math-fd-day, math-fd-weekday, math-fd-hour,
671 ;; math-fd-minute, math-fd-second, math-fd-bc-flag are local
672 ;; to math-format-date, but are used by math-format-date-part,
673 ;; which is called by math-format-date.
674 (defvar math-fd-date
)
676 (defvar math-fd-year
)
677 (defvar math-fd-month
)
679 (defvar math-fd-weekday
)
680 (defvar math-fd-hour
)
681 (defvar math-fd-minute
)
682 (defvar math-fd-second
)
683 (defvar math-fd-bc-flag
)
684 (defvar math-fd-iso-dt
)
685 (defvar math-fd-isoyear
)
686 (defvar math-fd-isoweek
)
687 (defvar math-fd-isoweekday
)
689 (defun math-format-date (math-fd-date)
690 (if (eq (car-safe math-fd-date
) 'date
)
691 (setq math-fd-date
(nth 1 math-fd-date
)))
692 (let ((entry (list math-fd-date calc-internal-prec calc-date-format
)))
693 (or (cdr (assoc entry math-format-date-cache
))
694 (let* ((math-fd-dt nil
)
696 (calc-group-digits nil
)
697 (calc-leading-zeros nil
)
698 (calc-number-radix 10)
699 (calc-twos-complement-mode nil
)
700 math-fd-year math-fd-month math-fd-day math-fd-weekday
701 math-fd-hour math-fd-minute math-fd-second
702 math-fd-isoyear math-fd-isoweek math-fd-isoweekday
703 (math-fd-bc-flag nil
)
704 (fmt (apply 'concat
(mapcar 'math-format-date-part
706 (setq math-format-date-cache
(cons (cons entry fmt
)
707 math-format-date-cache
))
708 (and (setq math-fd-dt
(nthcdr 10 math-format-date-cache
))
709 (setcdr math-fd-dt nil
))
712 (defconst math-julian-date-beginning
'(float 17214225 -
1)
713 "The beginning of the Julian date calendar,
714 as measured in the number of days before December 31, 1 BC (Gregorian).")
716 (defconst math-julian-date-beginning-int
1721423
717 "The beginning of the Julian date calendar,
718 as measured in the integer number of days before December 31, 1 BC (Gregorian).")
720 (defun math-format-date-part (x)
724 (if (math-integerp math-fd-date
)
726 (apply 'concat
(mapcar 'math-format-date-part x
))))
730 (math-format-number math-fd-date
))
732 (math-format-number (math-floor math-fd-date
)))
735 (math-add math-fd-date math-julian-date-beginning
)))
737 (math-format-number (math-add
738 (math-floor math-fd-date
)
739 math-julian-date-beginning-int
)))
741 (math-format-number (nth 1 (math-date-parts math-fd-date
719164))))
742 ((memq x
'(IYYY Iww w
))
745 (setq math-fd-iso-dt
(math-date-to-iso-dt math-fd-date
)
746 math-fd-isoyear
(car math-fd-iso-dt
)
747 math-fd-isoweek
(nth 1 math-fd-iso-dt
)
748 math-fd-isoweekday
(nth 2 math-fd-iso-dt
)))
750 (let* ((neg (Math-negp math-fd-isoyear
))
751 (pyear (calcFunc-abs math-fd-isoyear
)))
752 (if (and (natnump pyear
) (< pyear
10000))
753 (concat (if neg
"-" "") (format "%04d" pyear
))
754 (concat (if neg
"-" "+") (math-format-number pyear
)))))
756 (concat "W" (format "%02d" math-fd-isoweek
)))
758 (format "%d" math-fd-isoweekday
)))))
762 (setq math-fd-dt
(math-date-to-dt math-fd-date
)
763 math-fd-year
(car math-fd-dt
)
764 math-fd-month
(nth 1 math-fd-dt
)
765 math-fd-day
(nth 2 math-fd-dt
)
766 math-fd-weekday
(math-mod (math-floor math-fd-date
) 7)
767 math-fd-hour
(nth 3 math-fd-dt
)
768 math-fd-minute
(nth 4 math-fd-dt
)
769 math-fd-second
(nth 5 math-fd-dt
))
770 (and (memq 'b calc-date-format
)
771 (math-negp math-fd-year
)
772 (setq math-fd-year
(math-neg math-fd-year
)
773 math-fd-bc-flag t
))))
775 (if (and (integerp math-fd-year
) (> math-fd-year
1940) (< math-fd-year
2040))
776 (format (cond ((eq x
'YY
) "%02d")
779 (% math-fd-year
100))
780 (if (and (natnump math-fd-year
) (< math-fd-year
100))
781 (format "+%d" math-fd-year
)
782 (math-format-number math-fd-year
))))
784 (math-format-number math-fd-year
))
786 (if (and (natnump math-fd-year
) (< math-fd-year
100))
787 (format "+%d" math-fd-year
)
788 (math-format-number math-fd-year
)))
790 (let* ((year (if (Math-negp math-fd-year
)
791 (math-add math-fd-year
1)
793 (neg (Math-negp year
))
794 (pyear (calcFunc-abs year
)))
795 (if (and (natnump pyear
) (< pyear
10000))
796 (concat (if neg
"-" "") (format "%04d" pyear
))
797 (concat (if neg
"-" "+") (math-format-number pyear
)))))
800 (and (not math-fd-bc-flag
) "ad"))
802 (and (not math-fd-bc-flag
) "AD"))
804 (and (not math-fd-bc-flag
) "ad "))
806 (and (not math-fd-bc-flag
) "AD "))
808 (and (not math-fd-bc-flag
) "a.d."))
810 (and (not math-fd-bc-flag
) "A.D."))
812 (and math-fd-bc-flag
"bc"))
814 (and math-fd-bc-flag
"BC"))
816 (and math-fd-bc-flag
" bc"))
818 (and math-fd-bc-flag
" BC"))
820 (and math-fd-bc-flag
"b.c."))
822 (and math-fd-bc-flag
"B.C."))
825 (format "%d" math-fd-month
))
827 (format "%02d" math-fd-month
))
829 (format "%2d" math-fd-month
))
831 (downcase (nth (1- math-fd-month
) math-short-month-names
)))
833 (nth (1- math-fd-month
) math-short-month-names
))
835 (upcase (nth (1- math-fd-month
) math-short-month-names
)))
837 (nth (1- math-fd-month
) math-long-month-names
))
839 (upcase (nth (1- math-fd-month
) math-long-month-names
)))
841 (format "%d" math-fd-day
))
843 (format "%02d" math-fd-day
))
845 (format "%2d" math-fd-day
))
847 (format "%d" math-fd-weekday
))
849 (downcase (nth math-fd-weekday math-short-weekday-names
)))
851 (nth math-fd-weekday math-short-weekday-names
))
853 (upcase (nth math-fd-weekday math-short-weekday-names
)))
855 (nth math-fd-weekday math-long-weekday-names
))
857 (upcase (nth math-fd-weekday math-long-weekday-names
)))
859 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
861 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
863 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
865 (and math-fd-hour
(format "%d" math-fd-hour
)))
867 (and math-fd-hour
(format "%02d" math-fd-hour
)))
869 (and math-fd-hour
(format "%2d" math-fd-hour
)))
871 (and math-fd-hour
(format "%d" (1+ (%
(+ math-fd-hour
11) 12)))))
873 (and math-fd-hour
(format "%02d" (1+ (%
(+ math-fd-hour
11) 12)))))
875 (and math-fd-hour
(format "%2d" (1+ (%
(+ math-fd-hour
11) 12)))))
877 (and math-fd-hour
(if (< math-fd-hour
12) "a" "p")))
879 (and math-fd-hour
(if (< math-fd-hour
12) "A" "P")))
881 (and math-fd-hour
(if (< math-fd-hour
12) "am" "pm")))
883 (and math-fd-hour
(if (< math-fd-hour
12) "AM" "PM")))
885 (and math-fd-hour
(if (< math-fd-hour
12) "a.m." "p.m.")))
887 (and math-fd-hour
(if (< math-fd-hour
12) "A.M." "P.M.")))
889 (and math-fd-minute
(format "%d" math-fd-minute
)))
891 (and math-fd-minute
(format "%02d" math-fd-minute
)))
893 (and math-fd-minute
(format "%2d" math-fd-minute
)))
895 (and math-fd-second
(not (math-zerop math-fd-second
))
897 ((memq x
'(s ss bs SS BS
))
899 (not (and (memq x
'(SS BS
)) (math-zerop math-fd-second
)))
900 (if (integerp math-fd-second
)
901 (format (cond ((memq x
'(ss SS
)) "%02d")
902 ((memq x
'(bs BS
)) "%2d")
905 (concat (if (Math-lessp math-fd-second
10)
906 (cond ((memq x
'(ss SS
)) "0")
907 ((memq x
'(bs BS
)) " ")
910 (let ((calc-float-format
911 (list 'fix
(min (- 12 calc-internal-prec
)
913 (math-format-number math-fd-second
))))))))
915 ;; The variable math-pd-str is local to math-parse-date and
916 ;; math-parse-standard-date, but is used by math-parse-date-word,
917 ;; which is called by math-parse-date and math-parse-standard-date.
920 (defun math-parse-date (math-pd-str)
922 (or (math-parse-standard-date math-pd-str t
)
923 (math-parse-standard-date math-pd-str nil
)
924 (and (string-match "W[0-9][0-9]" math-pd-str
)
925 (math-parse-iso-date math-pd-str
))
926 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str
)
927 (list 'date
(math-read-number (math-match-substring math-pd-str
1))))
928 (let ((case-fold-search t
)
929 (year nil
) (month nil
) (day nil
) (weekday nil
)
930 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
)
931 (a nil
) (b nil
) (c nil
) (bigyear nil
) temp
)
933 ;; Extract the time, if any.
934 (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
)
935 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str
))
936 (let ((ampm (math-match-substring math-pd-str
6)))
937 (setq hour
(string-to-number (math-match-substring math-pd-str
1))
938 minute
(math-match-substring math-pd-str
2)
939 second
(math-match-substring math-pd-str
4)
940 math-pd-str
(concat (substring math-pd-str
0 (match-beginning 0))
941 (substring math-pd-str
(match-end 0))))
942 (if (equal minute
"")
944 (setq minute
(string-to-number minute
)))
945 (if (equal second
"")
947 (setq second
(math-read-number second
)))
953 (not (eq second
0))))
954 (throw 'syntax
"Hour value is out of range"))
955 (setq ampm
(upcase (aref ampm
0)))
956 (if (memq ampm
'(?N ?M
))
957 (if (and (= hour
12) (= minute
0) (eq second
0))
958 (if (eq ampm ?M
) (setq hour
0))
960 "Time must be 12:00:00 in this context"))
961 (if (or (= hour
0) (> hour
12))
962 (throw 'syntax
"Hour value is out of range"))
963 (if (eq (= ampm ?A
) (= hour
12))
964 (setq hour
(%
(+ hour
12) 24)))))))
966 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
967 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str
)
969 (setq math-pd-str
(copy-sequence math-pd-str
))
970 (aset math-pd-str
(match-beginning 1) ?\
/)))
972 ;; Extract obvious month or weekday names.
973 (if (string-match "[a-zA-Z]" math-pd-str
)
975 (setq month
(math-parse-date-word math-long-month-names
))
976 (setq weekday
(math-parse-date-word math-long-weekday-names
))
977 (or month
(setq month
978 (math-parse-date-word math-short-month-names
)))
979 (or weekday
(math-parse-date-word math-short-weekday-names
))
981 (if (setq temp
(math-parse-date-word
982 '( "noon" "midnight" "mid" )))
983 (setq hour
(if (= temp
1) 12 0) minute
0 second
0)))
984 (or (math-parse-date-word '( "ad" "a.d." ))
985 (if (math-parse-date-word '( "bc" "b.c." ))
987 (if (string-match "[a-zA-Z]+" math-pd-str
)
988 (throw 'syntax
(format "Bad word in date: \"%s\""
989 (math-match-substring math-pd-str
0))))))
991 ;; If there is a huge number other than the year, ignore it.
992 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str
)
993 (setq temp
(concat (substring math-pd-str
0 (match-beginning 0))
994 (substring math-pd-str
(match-end 0))))
996 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp
))
997 (setq math-pd-str temp
))
999 ;; If there is a number with a sign or a large number, it is a year.
1000 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str
)
1001 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str
))
1002 (setq year
(math-match-substring math-pd-str
1)
1003 math-pd-str
(concat (substring math-pd-str
0 (match-beginning 1))
1004 (substring math-pd-str
(match-end 1)))
1005 year
(math-read-number year
)
1008 ;; Collect remaining numbers.
1010 (while (string-match "[0-9]+" math-pd-str temp
)
1011 (and c
(throw 'syntax
"Too many numbers in date"))
1012 (setq c
(string-to-number (math-match-substring math-pd-str
0)))
1013 (or b
(setq b c c nil
))
1014 (or a
(setq a b b nil
))
1015 (setq temp
(match-end 0)))
1017 ;; Check that we have the right amount of information.
1018 (setq temp
(+ (if year
1 0) (if month
1 0) (if day
1 0)
1019 (if a
1 0) (if b
1 0) (if c
1 0)))
1021 (throw 'syntax
"Too many numbers in date")
1022 (if (or (< temp
2) (and year
(= temp
2)))
1023 (throw 'syntax
"Not enough numbers in date")
1024 (if (= temp
2) ; if year omitted, assume current year
1025 (setq year
(math-this-year)))))
1027 ;; A large number must be a year.
1029 (if (and a
(or (> a
31) (< a
1)))
1030 (setq year a a b b c c nil
)
1031 (if (and b
(or (> b
31) (< b
1)))
1032 (setq year b b c c nil
)
1033 (if (and c
(or (> c
31) (< c
1)))
1034 (setq year c c nil
)))))
1036 ;; A medium-large number must be a day.
1038 (if (and a
(> a
12))
1039 (setq day a a b b c c nil
)
1040 (if (and b
(> b
12))
1041 (setq day b b c c nil
)
1042 (if (and c
(> c
12))
1043 (setq day c c nil
)))))
1045 ;; We may know enough to sort it out now.
1047 (or month
(setq month a
))
1048 (if (and year month
)
1051 ;; Interpret order of numbers as same as for display format.
1052 (setq temp calc-date-format
)
1054 (cond ((not (symbolp (car temp
))))
1055 ((memq (car temp
) '(Y YY BY YYY YYYY
))
1056 (or year
(setq year a a b b c
)))
1057 ((memq (car temp
) '(M MM BM mmm Mmm Mmmm MMM MMMM
))
1058 (or month
(setq month a a b b c
)))
1059 ((memq (car temp
) '(D DD BD
))
1060 (or day
(setq day a a b b c
))))
1061 (setq temp
(cdr temp
)))
1063 ;; If display format was not complete, assume American style.
1064 (or month
(setq month a a b b c
))
1065 (or day
(setq day a a b b c
))
1066 (or year
(setq year a a b b c
))))
1069 (setq year
(math-neg (math-abs year
))))
1071 (math-parse-date-validate year bigyear month day
1072 hour minute second
)))))
1074 (defun math-parse-date-validate (year bigyear month day hour minute second
)
1075 (and (not bigyear
) (natnump year
) (< year
100)
1076 (setq year
(+ year
(if (< year
40) 2000 1900))))
1078 (throw 'syntax
"Year value is out of range"))
1079 (if (or (< month
1) (> month
12))
1080 (throw 'syntax
"Month value is out of range"))
1081 (if (or (< day
1) (> day
(math-days-in-month year month
)))
1082 (throw 'syntax
"Day value is out of range"))
1089 (not (eq second
0))))
1090 (throw 'syntax
"Hour value is out of range"))
1091 (if (or (< minute
0) (> minute
59))
1092 (throw 'syntax
"Minute value is out of range"))
1093 (if (or (math-negp second
) (not (Math-lessp second
60)))
1094 (throw 'syntax
"Seconds value is out of range"))))
1095 (list 'date
(math-dt-to-date (append (list year month day
)
1096 (and hour
(list hour minute second
))))))
1098 (defun math-parse-iso-date-validate (isoyear isoweek isoweekday hour minute second
)
1099 (if (or (< isoweek
1) (> isoweek
53))
1100 (throw 'syntax
"Week value is out of range"))
1101 (if (or (< isoweekday
1) (> isoweekday
7))
1102 (throw 'syntax
"Weekday value is out of range"))
1109 (not (eq second
0))))
1110 (throw 'syntax
"Hour value is out of range"))
1111 (if (or (< minute
0) (> minute
59))
1112 (throw 'syntax
"Minute value is out of range"))
1113 (if (or (math-negp second
) (not (Math-lessp second
60)))
1114 (throw 'syntax
"Seconds value is out of range"))))
1115 (list 'date
(math-iso-dt-to-date (append (list isoyear isoweek isoweekday
)
1116 (and hour
(list hour minute second
))))))
1118 (defun math-parse-date-word (names &optional front
)
1120 (while (and names
(not (string-match (if (equal (car names
) "Sep")
1122 (regexp-quote (car names
)))
1124 (setq names
(cdr names
)
1127 (or (not front
) (= (match-beginning 0) 0))
1129 (setq math-pd-str
(concat (substring math-pd-str
0 (match-beginning 0))
1131 (substring math-pd-str
(match-end 0))))
1134 (defun math-parse-standard-date (math-pd-str with-time
)
1135 (let ((case-fold-search t
)
1137 (fmt calc-date-format
) this next
(gnext nil
)
1138 (isoyear nil
) (isoweek nil
) (isoweekday nil
)
1139 (year nil
) (month nil
) (day nil
) (bigyear nil
) (yearday nil
)
1140 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
))
1141 (while (and fmt okay
)
1142 (setq this
(car fmt
)
1143 fmt
(setq fmt
(or (cdr fmt
)
1148 (if (consp next
) (setq next
(car next
)))
1149 (or (cond ((listp this
)
1155 (if (and (<= (length this
) (length math-pd-str
))
1157 (substring math-pd-str
0 (length this
))))
1158 (setq math-pd-str
(substring math-pd-str
(length this
)))))
1161 ((memq this
'(n N j J
))
1162 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str
)
1163 (setq num
(math-match-substring math-pd-str
0)
1164 math-pd-str
(substring math-pd-str
(match-end 0))
1165 num
(math-date-to-dt (math-read-number num
))
1167 (if (memq this
'(n N
))
1169 (if (or (eq this
'j
)
1170 (math-integerp num
))
1171 math-julian-date-beginning-int
1172 math-julian-date-beginning
)))
1173 hour
(or (nth 3 num
) hour
)
1174 minute
(or (nth 4 num
) minute
)
1175 second
(or (nth 5 num
) second
)
1180 (and (string-match "\\`[-+]?[0-9]+" math-pd-str
)
1181 (setq num
(math-match-substring math-pd-str
0)
1182 math-pd-str
(substring math-pd-str
(match-end 0))
1183 num
(math-date-to-dt
1185 (math-div (math-read-number num
)
1193 ((memq this
'(mmm Mmm MMM
))
1194 (setq month
(math-parse-date-word math-short-month-names t
)))
1195 ((memq this
'(Mmmm MMMM
))
1196 (setq month
(math-parse-date-word math-long-month-names t
)))
1197 ((memq this
'(www Www WWW
))
1198 (math-parse-date-word math-short-weekday-names t
))
1199 ((memq this
'(Wwww WWWW
))
1200 (math-parse-date-word math-long-weekday-names t
))
1202 (if (string-match "\\`a" math-pd-str
)
1203 (setq hour
(if (= hour
12) 0 hour
)
1204 math-pd-str
(substring math-pd-str
1))
1205 (if (string-match "\\`p" math-pd-str
)
1206 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
1207 math-pd-str
(substring math-pd-str
1)))))
1208 ((memq this
'(pp PP pppp PPPP
))
1209 (if (string-match "\\`am\\|a\\.m\\." math-pd-str
)
1210 (setq hour
(if (= hour
12) 0 hour
)
1211 math-pd-str
(substring math-pd-str
(match-end 0)))
1212 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str
)
1213 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
1214 math-pd-str
(substring math-pd-str
(match-end 0))))))
1215 ((memq this
'(Y YY BY YYY YYYY ZYYY
))
1216 (and (if (memq next
'(MM DD ddd hh HH mm ss SS
))
1217 (if (memq this
'(Y YY BYY
))
1218 (string-match "\\` *[0-9][0-9]" math-pd-str
)
1219 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str
))
1220 (string-match "\\`[-+]?[0-9]+" math-pd-str
))
1221 (setq year
(math-match-substring math-pd-str
0)
1222 bigyear
(or (eq this
'YYY
)
1223 (memq (aref math-pd-str
0) '(?\
+ ?\-
)))
1224 math-pd-str
(substring math-pd-str
(match-end 0))
1225 year
(math-read-number year
))
1226 (if (and (eq this
'ZYYY
) (eq year
0))
1227 (setq year
(math-sub year
1)
1231 (if (string-match "\\`[-+]?[0-9]+" math-pd-str
)
1232 (setq isoyear
(string-to-number (math-match-substring math-pd-str
0))
1233 math-pd-str
(substring math-pd-str
(match-end 0)))))
1235 (if (string-match "W\\([0-9][0-9]\\)" math-pd-str
)
1236 (setq isoweek
(string-to-number (math-match-substring math-pd-str
1))
1237 math-pd-str
(substring math-pd-str
3))))
1241 (if (eq (aref math-pd-str
0) ?T
)
1242 (setq math-pd-str
(substring math-pd-str
1))
1244 ((memq this
'(aa AA aaaa AAAA
))
1245 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str
)
1246 (setq math-pd-str
(substring math-pd-str
(match-end 0)))))
1247 ((memq this
'(aaa AAA
))
1248 (if (string-match "\\` *ad *" math-pd-str
)
1249 (setq math-pd-str
(substring math-pd-str
(match-end 0)))))
1250 ((memq this
'(bb BB bbb BBB bbbb BBBB
))
1251 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str
)
1252 (setq math-pd-str
(substring math-pd-str
(match-end 0))
1254 ((memq this
'(s ss bs SS BS
))
1255 (and (if (memq next
'(YY YYYY MM DD hh HH mm
))
1256 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str
)
1257 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str
))
1258 (setq second
(math-match-substring math-pd-str
0)
1259 math-pd-str
(substring math-pd-str
(match-end 0))
1260 second
(math-read-number second
))))
1262 (if (string-match "\\`:[0-9][0-9]" math-pd-str
)
1263 (setq math-pd-str
(substring math-pd-str
1))
1265 ((or (not (if (and (memq this
'(ddd MM DD hh HH mm
))
1266 (memq next
'(YY YYYY MM DD ddd
1269 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str
)
1270 (string-match "\\` *[0-9][0-9]" math-pd-str
))
1271 (string-match "\\` *[0-9]+" math-pd-str
)))
1272 (and (setq num
(string-to-number
1273 (math-match-substring math-pd-str
0))
1274 math-pd-str
(substring math-pd-str
(match-end 0)))
1278 (and (>= num
0) (< num
7)))
1280 (setq isoweekday num
))
1281 ((memq this
'(d ddd bdd
))
1283 ((memq this
'(M MM BM
))
1285 ((memq this
'(D DD BD
))
1287 ((memq this
'(h hh bh H HH BH
))
1289 ((memq this
'(m mm bm
))
1295 (setq month
1 day
1)))
1296 (if (and okay
(equal math-pd-str
""))
1298 (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second
)
1299 (and month day
(or (not (or hour minute second
))
1302 (or year
(setq year
(math-this-year)))
1303 (or second
(setq second
0))
1305 (setq year
(math-neg (math-abs year
))))
1306 (setq day
(math-parse-date-validate year bigyear month day
1307 hour minute second
))
1309 (setq day
(math-add day
(1- yearday
))))
1312 (defun math-parse-iso-date (math-pd-str)
1313 "Parse MATH-PD-STR as an ISO week date, or return nil."
1314 (let ((case-fold-search t
)
1315 (isoyear nil
) (isoweek nil
) (isoweekday nil
)
1316 (hour nil
) (minute nil
) (second nil
))
1317 ;; Extract the time, if any.
1318 (if (string-match "T[^0-9]*\\([0-9][0-9]\\)[^0-9]*\\([0-9][0-9]\\)?[^0-9]*\\([0-9][0-9]\\(\\.[0-9]+\\)?\\)?" math-pd-str
)
1320 (setq hour
(string-to-number (math-match-substring math-pd-str
1))
1321 minute
(math-match-substring math-pd-str
2)
1322 second
(math-match-substring math-pd-str
3)
1323 math-pd-str
(substring math-pd-str
0 (match-beginning 0)))
1324 (if (equal minute
"")
1326 (setq minute
(string-to-number minute
)))
1327 (if (equal second
"")
1329 (setq second
(math-read-number second
)))))
1330 ;; Next, the year, week and weekday
1331 (if (string-match "\\(-?[0-9]*\\)[^0-9]*W\\([0-9][0-9]\\)[^0-9]*\\([0-9]\\)[^0-9]*\\'" math-pd-str
)
1333 (setq isoyear
(string-to-number (math-match-substring math-pd-str
1))
1334 isoweek
(string-to-number (math-match-substring math-pd-str
2))
1335 isoweekday
(string-to-number (math-match-substring math-pd-str
3)))
1336 (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second
)))))
1338 (defun calcFunc-now (&optional zone
)
1339 (let ((date (let ((calc-date-format nil
))
1340 (math-parse-date (current-time-string)))))
1343 (math-add date
(math-div (math-sub (calcFunc-tzone nil date
)
1344 (calcFunc-tzone zone date
))
1347 (calc-record-why "*Unable to interpret current date from system")
1348 (append (list 'calcFunc-now
) (and zone
(list zone
))))))
1350 (defun calcFunc-year (date)
1351 (car (math-date-to-dt date
)))
1353 (defun calcFunc-month (date)
1354 (nth 1 (math-date-to-dt date
)))
1356 (defun calcFunc-day (date)
1357 (nth 2 (math-date-to-dt date
)))
1359 (defun calcFunc-weekday (date)
1360 (if (eq (car-safe date
) 'date
)
1361 (setq date
(nth 1 date
)))
1362 (or (math-realp date
)
1363 (math-reject-arg date
'datep
))
1364 (math-mod (math-floor date
) 7))
1366 (defun calcFunc-yearday (date)
1367 (let ((dt (math-date-to-dt date
)))
1368 (math-day-number (car dt
) (nth 1 dt
) (nth 2 dt
))))
1370 (defun calcFunc-hour (date)
1371 (if (eq (car-safe date
) 'hms
)
1373 (or (nth 3 (math-date-to-dt date
)) 0)))
1375 (defun calcFunc-minute (date)
1376 (if (eq (car-safe date
) 'hms
)
1378 (or (nth 4 (math-date-to-dt date
)) 0)))
1380 (defun calcFunc-second (date)
1381 (if (eq (car-safe date
) 'hms
)
1383 (or (nth 5 (math-date-to-dt date
)) 0)))
1385 (defun calcFunc-time (date)
1386 (let ((dt (math-date-to-dt date
)))
1388 (cons 'hms
(nthcdr 3 dt
))
1389 (list 'hms
0 0 0))))
1391 (defun calcFunc-date (date &optional month day hour minute second
)
1392 (and (math-messy-integerp month
) (setq month
(math-trunc month
)))
1393 (and month
(not (integerp month
)) (math-reject-arg month
'fixnump
))
1394 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1395 (and day
(not (integerp day
)) (math-reject-arg day
'fixnump
))
1396 (if (and (eq (car-safe hour
) 'hms
) (not minute
))
1397 (setq second
(nth 3 hour
)
1400 (and (math-messy-integerp hour
) (setq hour
(math-trunc hour
)))
1401 (and hour
(not (integerp hour
)) (math-reject-arg hour
'fixnump
))
1402 (and (math-messy-integerp minute
) (setq minute
(math-trunc minute
)))
1403 (and minute
(not (integerp minute
)) (math-reject-arg minute
'fixnump
))
1404 (and (math-messy-integerp second
) (setq second
(math-trunc second
)))
1405 (and second
(not (math-realp second
)) (math-reject-arg second
'realp
))
1408 (and (math-messy-integerp date
) (setq date
(math-trunc date
)))
1409 (and date
(not (math-integerp date
)) (math-reject-arg date
'integerp
))
1412 (list 'date
(math-dt-to-date (list date month day hour
1415 (list 'date
(math-dt-to-date (list date month day
))))
1416 (list 'date
(math-dt-to-date (list (math-this-year) date month
)))))
1417 (if (math-realp date
)
1419 (if (eq (car date
) 'date
)
1421 (math-reject-arg date
'datep
)))))
1423 (defun calcFunc-julian (date &optional zone
)
1424 (if (math-realp date
)
1425 (list 'date
(if (math-integerp date
)
1426 (math-sub date math-julian-date-beginning-int
)
1427 (setq date
(math-sub date math-julian-date-beginning
))
1428 (math-sub date
(math-div (calcFunc-tzone zone date
)
1430 (if (eq (car date
) 'date
)
1431 (math-add (nth 1 date
) (if (math-integerp (nth 1 date
))
1432 math-julian-date-beginning-int
1433 (math-add math-julian-date-beginning
1434 (math-div (calcFunc-tzone zone date
)
1436 (math-reject-arg date
'datep
))))
1438 (defun calcFunc-unixtime (date &optional zone
)
1439 (if (math-realp date
)
1441 (setq date
(math-add 719163 (math-div date
'(float 864 2))))
1442 (list 'date
(math-sub date
(math-div (calcFunc-tzone zone date
)
1444 (if (eq (car date
) 'date
)
1445 (math-add (nth 1 (math-date-parts (nth 1 date
) 719163))
1446 (calcFunc-tzone zone date
))
1447 (math-reject-arg date
'datep
))))
1450 ;;; Note: Longer names must appear before shorter names which are
1451 ;;; substrings of them.
1452 (defvar math-tzone-names
1454 ( "MEGT" -
1 "MET" "METDST" ) ; Middle Europe
1455 ( "METDST" -
1 -
1 ) ( "MET" -
1 0 )
1456 ( "MEGZ" -
1 "MEZ" "MESZ" ) ( "MEZ" -
1 0 ) ( "MESZ" -
1 -
1 )
1457 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1458 ( "WETDST" 0 -
1 ) ( "WET" 0 0 )
1459 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -
1 ) ; Britain
1460 ( "NGT" (float 35 -
1) "NST" "NDT" ) ; Newfoundland
1461 ( "NST" (float 35 -
1) 0 ) ( "NDT" (float 35 -
1) -
1 )
1462 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -
1 ) ; Atlantic
1463 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -
1 ) ; Eastern
1464 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -
1 ) ; Central
1465 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -
1 ) ; Mountain
1466 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -
1 ) ; Pacific
1467 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -
1 ) ; Yukon
1469 "No doc yet. See calc manual for now. ")
1471 (defvar var-TimeZone nil
)
1474 (defvar calendar-current-time-zone-cache
)
1476 (defvar math-calendar-tzinfo
1478 "Information about the timezone, retrieved from the calendar.")
1480 (defun math-get-calendar-tzinfo ()
1481 "Get information about the timezone from the calendar.
1482 The result should be a list of two items about the current time zone:
1483 first, the number of seconds difference from GMT
1484 second, the number of seconds offset for daylight savings."
1485 (if math-calendar-tzinfo
1486 math-calendar-tzinfo
1488 (let ((tzinfo (progn
1489 (calendar-current-time-zone)
1490 calendar-current-time-zone-cache
)))
1491 (setq math-calendar-tzinfo
1492 (list (* 60 (abs (nth 0 tzinfo
)))
1493 (* 60 (nth 1 tzinfo
)))))))
1495 (defun calcFunc-tzone (&optional zone date
)
1497 (cond ((math-realp zone
)
1498 (math-round (math-mul zone
3600)))
1499 ((eq (car zone
) 'hms
)
1500 (math-round (math-mul (math-from-hms zone
'deg
) 3600)))
1502 (math-add (calcFunc-tzone (nth 1 zone
) date
)
1503 (calcFunc-tzone (nth 2 zone
) date
)))
1505 (math-sub (calcFunc-tzone (nth 1 zone
) date
)
1506 (calcFunc-tzone (nth 2 zone
) date
)))
1507 ((eq (car zone
) 'var
)
1508 (let ((name (upcase (symbol-name (nth 1 zone
))))
1510 (if (setq found
(assoc name math-tzone-names
))
1511 (calcFunc-tzone (math-add (nth 1 found
)
1512 (if (integerp (nth 2 found
))
1515 (math-daylight-savings-adjust
1519 (if (equal name
"LOCAL")
1520 (calcFunc-tzone nil date
)
1521 (math-reject-arg zone
"*Unrecognized time zone name")))))
1522 (t (math-reject-arg zone
"*Expected a time zone")))
1523 (if (calc-var-value 'var-TimeZone
)
1524 (calcFunc-tzone (calc-var-value 'var-TimeZone
) date
)
1525 (let ((tzinfo (math-get-calendar-tzinfo)))
1527 (* (math-cal-daylight-savings-adjust date
) (nth 1 tzinfo
)))))))
1529 (defvar math-daylight-savings-hook
'math-std-daylight-savings
)
1531 (defun math-daylight-savings-adjust (date zone
&optional dt
)
1532 (or date
(setq date
(nth 1 (calcFunc-now))))
1534 (if (eq (car-safe date
) 'date
)
1537 (if (and date
(math-realp date
))
1538 (let ((zadj (assoc zone math-tzone-names
)))
1539 (if zadj
(setq bump -
1
1540 date
(math-sub date
(math-div (nth 1 zadj
)
1542 (math-reject-arg date
'datep
)))
1543 (setq date
(math-float date
))
1544 (or dt
(setq dt
(math-date-to-dt date
)))
1545 (and math-daylight-savings-hook
1546 (funcall math-daylight-savings-hook date dt zone bump
))))
1548 ;;; Based on part of dst-adjust-time in cal-dst.el
1549 ;;; For calcFunc-dst, when zone=nil
1550 (defun math-cal-daylight-savings-adjust (date)
1551 "Return -1 if DATE is using daylight saving, 0 otherwise."
1553 (unless date
(setq date
(calcFunc-now)))
1554 (let* ((dt (math-date-to-dt date
))
1559 (+ (nth 3 dt
) (/ (nth 4 dt
) 60.0)))
1564 (calendar-absolute-from-gregorian
1565 (list (nth 1 dt
) (nth 2 dt
) (nth 0 dt
)))
1566 (/ (round (* 60 time
)) 60.0 24.0))))
1567 (if (dst-in-effect rounded-abs-date
)
1571 (defun calcFunc-dsadj (date &optional zone
)
1573 (or (eq (car-safe zone
) 'var
)
1574 (math-reject-arg zone
"*Time zone variable expected"))
1575 (setq zone
(calc-var-value 'var-TimeZone
)))
1578 (setq zone
(and (eq (car-safe zone
) 'var
)
1579 (upcase (symbol-name (nth 1 zone
)))))
1580 (let ((zadj (assoc zone math-tzone-names
)))
1581 (or zadj
(math-reject-arg zone
"*Unrecognized time zone name"))
1582 (if (integerp (nth 2 zadj
))
1584 (math-daylight-savings-adjust date zone
))))
1585 (math-cal-daylight-savings-adjust date
)))
1587 ;; (defun calcFunc-dsadj (date &optional zone)
1589 ;; (or (eq (car-safe zone) 'var)
1590 ;; (math-reject-arg zone "*Time zone variable expected"))
1591 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1594 ;; (calc-var-value 'var-TimeZone)))))
1595 ;; (setq zone (and (eq (car-safe zone) 'var)
1596 ;; (upcase (symbol-name (nth 1 zone)))))
1597 ;; (let ((zadj (assoc zone math-tzone-names)))
1598 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1599 ;; (if (integerp (nth 2 zadj))
1601 ;; (math-daylight-savings-adjust date zone))))
1603 (defun calcFunc-tzconv (date z1 z2
)
1604 (if (math-realp date
)
1605 (nth 1 (calcFunc-tzconv (list 'date date
) z1 z2
))
1606 (calcFunc-unixtime (calcFunc-unixtime date z1
) z2
)))
1608 (defun math-std-daylight-savings (date dt zone bump
)
1609 "Standard North American daylight saving algorithm.
1610 Before 2007, this uses `math-std-daylight-savings-old', where
1611 daylight saving began on the first Sunday of April at 2 a.m.,
1612 and ended on the last Sunday of October at 2 a.m.
1613 As of 2007, this uses `math-std-daylight-savings-new', where
1614 daylight saving begins on the second Sunday of March at 2 a.m.,
1615 and ends on the first Sunday of November at 2 a.m."
1616 (if (< (car dt
) 2007)
1617 (math-std-daylight-savings-old date dt zone bump
)
1618 (math-std-daylight-savings-new date dt zone bump
)))
1620 (defun math-std-daylight-savings-new (date dt zone bump
)
1621 "Standard North American daylight saving algorithm as of 2007.
1622 This implements the rules for the U.S. and Canada.
1623 Daylight saving begins on the second Sunday of March at 2 a.m.,
1624 and ends on the first Sunday of November at 2 a.m."
1625 (cond ((< (nth 1 dt
) 3) 0)
1627 (let ((sunday (math-prev-weekday-in-month date dt
14 0)))
1628 (cond ((< (nth 2 dt
) sunday
) 0)
1629 ((= (nth 2 dt
) sunday
)
1630 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1632 ((< (nth 1 dt
) 11) -
1)
1634 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1635 (cond ((< (nth 2 dt
) sunday
) -
1)
1636 ((= (nth 2 dt
) sunday
)
1637 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1641 (defun math-std-daylight-savings-old (date dt zone bump
)
1642 "Standard North American daylight saving algorithm before 2007.
1643 This implements the rules for the U.S. and Canada.
1644 Daylight saving begins on the first Sunday of April at 2 a.m.,
1645 and ends on the last Sunday of October at 2 a.m."
1646 (cond ((< (nth 1 dt
) 4) 0)
1648 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1649 (cond ((< (nth 2 dt
) sunday
) 0)
1650 ((= (nth 2 dt
) sunday
)
1651 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1653 ((< (nth 1 dt
) 10) -
1)
1655 (let ((sunday (math-prev-weekday-in-month date dt
31 0)))
1656 (cond ((< (nth 2 dt
) sunday
) -
1)
1657 ((= (nth 2 dt
) sunday
)
1658 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1662 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1663 ;;; day of the given month.
1664 (defun math-prev-weekday-in-month (date dt day wday
)
1665 (or day
(setq day
(nth 2 dt
)))
1666 (if (> day
(math-days-in-month (car dt
) (nth 1 dt
)))
1667 (setq day
(math-days-in-month (car dt
) (nth 1 dt
))))
1668 (let ((zeroth (math-sub (math-floor date
) (nth 2 dt
))))
1669 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day
))) zeroth
)))
1671 (defun calcFunc-pwday (date &optional day weekday
)
1672 (if (eq (car-safe date
) 'date
)
1673 (setq date
(nth 1 date
)))
1674 (or (math-realp date
)
1675 (math-reject-arg date
'datep
))
1676 (if (math-messy-integerp day
) (setq day
(math-trunc day
)))
1677 (or (integerp day
) (math-reject-arg day
'fixnump
))
1678 (if (= day
0) (setq day
31))
1679 (and (or (< day
7) (> day
31)) (math-reject-arg day
'range
))
1680 (math-prev-weekday-in-month date
(math-date-to-dt date
) day
(or weekday
0)))
1683 (defun calcFunc-newweek (date &optional weekday
)
1684 (if (eq (car-safe date
) 'date
)
1685 (setq date
(nth 1 date
)))
1686 (or (math-realp date
)
1687 (math-reject-arg date
'datep
))
1688 (or weekday
(setq weekday
0))
1689 (and (math-messy-integerp weekday
) (setq weekday
(math-trunc weekday
)))
1690 (or (integerp weekday
) (math-reject-arg weekday
'fixnump
))
1691 (and (or (< weekday
0) (> weekday
6)) (math-reject-arg weekday
'range
))
1692 (setq date
(math-floor date
))
1693 (list 'date
(math-sub date
(calcFunc-weekday (math-sub date weekday
)))))
1695 (defun calcFunc-newmonth (date &optional day
)
1696 (or day
(setq day
1))
1697 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1698 (or (integerp day
) (math-reject-arg day
'fixnump
))
1699 (and (or (< day
0) (> day
31)) (math-reject-arg day
'range
))
1700 (let* ((dt (math-date-to-dt date
))
1701 (dim (math-days-in-month (car dt
) (nth 1 dt
)))
1702 (julian (if calc-gregorian-switch
1703 (math-date-to-dt (math-sub
1704 (or (nth 3 calc-gregorian-switch
)
1705 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))
1707 (if (or (= day
0) (> day dim
))
1709 (setq day
(1- day
)))
1710 ;; Adjust if this occurs near the switch to the Gregorian calendar
1711 (if calc-gregorian-switch
1713 ((and (math-dt-before-p (list (car dt
) (nth 1 dt
) 1) calc-gregorian-switch
)
1714 (math-dt-before-p julian
(list (car dt
) (nth 1 dt
) 1)))
1715 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the month
1717 (math-dt-to-date (list (car calc-gregorian-switch
)
1718 (nth 1 calc-gregorian-switch
)
1719 (if (> (+ (nth 2 calc-gregorian-switch
) day
) dim
)
1721 (+ (nth 2 calc-gregorian-switch
) day
))))))
1722 ((and (eq (car dt
) (car calc-gregorian-switch
))
1723 (= (nth 1 dt
) (nth 1 calc-gregorian-switch
)))
1724 ;; In this case, the switch to the Gregorian calendar occurs in the given month
1725 (if (< (+ (nth 2 julian
) day
) (nth 2 calc-gregorian-switch
))
1726 ;; If the DAYth day occurs before the switch, use it
1727 (list 'date
(math-dt-to-date (list (car dt
) (nth 1 dt
) (1+ day
))))
1728 ;; Otherwise do some computations
1729 (let ((tm (+ day
(- (nth 2 calc-gregorian-switch
) (nth 2 julian
)))))
1730 (list 'date
(math-dt-to-date
1734 (if (> tm dim
) dim tm
)))))))
1735 ((and (eq (car dt
) (car julian
))
1736 (= (nth 1 dt
) (nth 1 julian
)))
1737 ;; In this case, the current month is truncated because of the switch
1738 ;; to the Gregorian calendar
1739 (list 'date
(math-dt-to-date
1742 (if (>= day
(nth 2 julian
))
1747 (list 'date
(math-add (math-dt-to-date (list (car dt
) (nth 1 dt
) 1)) day
))))
1748 (list 'date
(math-add (math-dt-to-date (list (car dt
) (nth 1 dt
) 1)) day
)))))
1750 (defun calcFunc-newyear (date &optional day
)
1751 (if (eq (car-safe date
) 'date
) (setq date
(nth 1 date
)))
1752 (or day
(setq day
1))
1753 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1754 (or (integerp day
) (math-reject-arg day
'fixnump
))
1755 (let* ((dt (math-date-to-dt date
))
1756 (gregbeg (if calc-gregorian-switch
1757 (or (nth 3 calc-gregorian-switch
)
1758 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))))
1759 (julianend (if calc-gregorian-switch
(math-sub gregbeg
1)))
1760 (julian (if calc-gregorian-switch
1761 (math-date-to-dt julianend
))))
1762 (if (and (>= day
0) (<= day
366))
1763 (let ((max (if (math-leap-year-p (car dt
)) 366 365)))
1764 (if (or (= day
0) (> day max
)) (setq day max
))
1765 (if calc-gregorian-switch
1766 ;; Now to break this down into cases
1768 ((and (math-dt-before-p (list (car dt
) 1 1) calc-gregorian-switch
)
1769 (math-dt-before-p julian
(list (car dt
) 1 1)))
1770 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the year
1771 (list 'date
(math-min (math-add gregbeg
(1- day
))
1772 (math-dt-to-date (list (car calc-gregorian-switch
) 12 31)))))
1773 ((eq (car dt
) (car julian
))
1774 ;; In this case, the switch to the Gregorian calendar occurs in the given year
1775 (if (Math-lessp (car julian
) (car calc-gregorian-switch
))
1776 ;; Here, the last Julian day is the last day of the year.
1777 (list 'date
(math-min (math-add (math-dt-to-date (list (car dt
) 1 1)) (1- day
))
1779 ;; Otherwise, just make sure the date doesn't go past the end of the year
1780 (list 'date
(math-min (math-add (math-dt-to-date (list (car dt
) 1 1)) (1- day
))
1781 (math-dt-to-date (list (car dt
) 12 31))))))
1783 (list 'date
(math-add (math-dt-to-date (list (car dt
) 1 1))
1785 (list 'date
(math-add (math-dt-to-date (list (car dt
) 1 1))
1787 (if (and (>= day -
12) (<= day -
1))
1788 (if (and calc-gregorian-switch
1789 (math-dt-before-p (list (car dt
) (- day
) 1) calc-gregorian-switch
)
1790 (math-dt-before-p julian
(list (car dt
) (- day
) 1)))
1791 (list 'date gregbeg
)
1792 (list 'date
(math-dt-to-date (list (car dt
) (- day
) 1))))
1793 (math-reject-arg day
'range
)))))
1795 (defun calcFunc-incmonth (date &optional step
)
1796 (or step
(setq step
1))
1797 (and (math-messy-integerp step
) (setq step
(math-trunc step
)))
1798 (or (math-integerp step
) (math-reject-arg step
'integerp
))
1799 (let* ((dt (math-date-to-dt date
))
1801 (month (math-add (1- (nth 1 dt
)) step
))
1802 (extra (calcFunc-idiv month
12))
1804 (setq month
(1+ (math-sub month
(math-mul extra
12)))
1805 year
(math-add year extra
)
1806 day
(min day
(math-days-in-month year month
)))
1807 (and (math-posp (car dt
)) (not (math-posp year
))
1808 (setq year
(math-sub year
1))) ; did we go past the year zero?
1809 (and (math-negp (car dt
)) (not (math-negp year
))
1810 (setq year
(math-add year
1)))
1811 (list 'date
(math-dt-to-date
1812 (cons year
(cons month
(cons day
(cdr (cdr (cdr dt
))))))))))
1814 (defun calcFunc-incyear (date &optional step
)
1815 (calcFunc-incmonth date
(math-mul (or step
1) 12)))
1819 (defun calcFunc-bsub (a b
)
1820 (or (eq (car-safe a
) 'date
)
1821 (math-reject-arg a
'datep
))
1822 (if (eq (car-safe b
) 'date
)
1823 (if (math-lessp (nth 1 a
) (nth 1 b
))
1824 (math-neg (calcFunc-bsub b a
))
1825 (math-setup-holidays b
)
1826 (let* ((da (math-to-business-day a
))
1827 (db (math-to-business-day b
)))
1828 (math-add (math-sub (car da
) (car db
))
1829 (if (and (cdr db
) (not (cdr da
))) 1 0))))
1830 (calcFunc-badd a
(math-neg b
))))
1832 (defvar math-holidays-cache nil
)
1833 (defvar math-holidays-cache-tag t
)
1834 (defun calcFunc-badd (a b
)
1835 (if (eq (car-safe b
) 'date
)
1836 (if (eq (car-safe a
) 'date
)
1837 (math-reject-arg nil
"*Invalid combination in date arithmetic")
1838 (calcFunc-badd b a
))
1839 (if (eq (car-safe a
) 'date
)
1843 (let* ((d (math-to-business-day a
))
1844 (bb (math-add (car d
)
1845 (if (and (cdr d
) (Math-posp b
))
1846 (math-sub b
1) b
))))
1847 (or (math-from-business-day bb
)
1848 (calcFunc-badd a b
))))
1849 (if (eq (car-safe b
) 'hms
)
1850 (let ((hours (nth 7 math-holidays-cache
)))
1851 (setq b
(math-div (math-from-hms b
'deg
) 24))
1853 (setq b
(math-div b
(cdr hours
))))
1854 (calcFunc-badd a b
))
1855 (math-reject-arg nil
"*Invalid combination in date arithmetic")))
1856 (math-reject-arg a
'datep
))))
1858 (defun calcFunc-holiday (a)
1859 (if (cdr (math-to-business-day a
)) 1 0))
1861 ;;; Compute the number of business days since Jan 1, 1 AD.
1863 (defun math-to-business-day (date &optional need-year
)
1864 (if (eq (car-safe date
) 'date
)
1865 (setq date
(nth 1 date
)))
1866 (or (Math-realp date
)
1867 (math-reject-arg date
'datep
))
1868 (let* ((day (math-floor date
))
1869 (time (math-sub date day
))
1870 (dt (math-date-to-dt day
))
1873 (or (not need-year
) (eq (car dt
) need-year
)
1874 (math-reject-arg (list 'date day
) "*Generated holiday has wrong year"))
1875 (math-setup-holidays date
)
1876 (let ((days (car math-holidays-cache
)))
1877 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1878 (setq delta
(1+ delta
)))
1879 (and days
(= day
(car days
))
1881 (let* ((weekdays (nth 3 math-holidays-cache
))
1882 (weeks (1- (/ (+ day
6) 7)))
1883 (wkday (- day
1 (* weeks
7))))
1884 (setq delta
(+ delta
(* weeks
(length weekdays
))))
1885 (while (and weekdays
(< (car weekdays
) wkday
))
1886 (setq weekdays
(cdr weekdays
)
1888 (and weekdays
(eq wkday
(car weekdays
))
1890 (let ((hours (nth 7 math-holidays-cache
)))
1893 (setq time
(math-div (math-sub time
(car hours
)) (cdr hours
)))
1894 (if (Math-lessp time
0) (setq time
0))
1895 (or (Math-lessp time
1)
1898 (math-div 1 (math-mul 86400 (cdr hours
)))))))))
1899 (cons (math-add (math-sub day delta
) time
) holiday
)))
1902 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1903 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1905 (defun math-from-business-day (num)
1906 (let* ((day (math-floor num
))
1907 (time (math-sub num day
)))
1909 (math-reject-arg nil
"*Date is outside valid range"))
1910 (math-setup-holidays)
1911 (let ((days (nth 1 math-holidays-cache
))
1913 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1914 (setq delta
(1+ delta
)))
1915 (setq day
(+ day delta
)))
1916 (let* ((weekdays (nth 3 math-holidays-cache
))
1917 (bweek (- 7 (length weekdays
)))
1918 (weeks (1- (/ (+ day
(1- bweek
)) bweek
)))
1919 (wkday (- day
1 (* weeks bweek
)))
1921 (setq day
(+ day
(* weeks
(length weekdays
))))
1922 (while (if (memq w weekdays
)
1924 (> (setq wkday
(1- wkday
)) 0))
1926 (let ((hours (nth 7 math-holidays-cache
)))
1928 (setq time
(math-add (math-mul time
(cdr hours
)) (car hours
)))))
1929 (and (not (math-setup-holidays day
))
1930 (list 'date
(math-add day time
))))))
1932 ;; The variable math-sh-year is local to math-setup-holidays
1933 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1934 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1935 (defvar math-sh-year
)
1937 (defun math-setup-holidays (&optional date
)
1938 (or (eq (calc-var-value 'var-Holidays
) math-holidays-cache-tag
)
1939 (let ((h (calc-var-value 'var-Holidays
))
1940 (wdnames '( (sun .
0) (mon .
1) (tue .
2) (wed .
3)
1941 (thu .
4) (fri .
5) (sat .
6) ))
1942 (days nil
) (weekdays nil
) (exprs nil
) (limit nil
) (hours nil
))
1943 (or (math-vectorp h
)
1944 (math-reject-arg h
"*Holidays variable must be a vector"))
1945 (while (setq h
(cdr h
))
1946 (cond ((or (and (eq (car-safe (car h
)) 'date
)
1947 (integerp (nth 1 (car h
))))
1948 (and (eq (car-safe (car h
)) 'intv
)
1949 (eq (car-safe (nth 2 (car h
))) 'date
))
1950 (eq (car-safe (car h
)) 'vec
))
1951 (setq days
(cons (car h
) days
)))
1952 ((and (eq (car-safe (car h
)) 'var
)
1953 (assq (nth 1 (car h
)) wdnames
))
1954 (setq weekdays
(cons (cdr (assq (nth 1 (car h
)) wdnames
))
1956 ((and (eq (car-safe (car h
)) 'intv
)
1957 (eq (car-safe (nth 2 (car h
))) 'hms
)
1958 (eq (car-safe (nth 3 (car h
))) 'hms
))
1961 (car h
) "*Only one hours interval allowed in Holidays"))
1962 (setq hours
(math-div (car h
) '(hms 24 0 0)))
1963 (if (or (Math-lessp (nth 2 hours
) 0)
1964 (Math-lessp 1 (nth 3 hours
)))
1966 (car h
) "*Hours interval out of range"))
1967 (setq hours
(cons (nth 2 hours
)
1968 (math-sub (nth 3 hours
) (nth 2 hours
))))
1969 (if (Math-zerop (cdr hours
))
1971 (car h
) "*Degenerate hours interval")))
1972 ((or (and (eq (car-safe (car h
)) 'intv
)
1973 (Math-integerp (nth 2 (car h
)))
1974 (Math-integerp (nth 3 (car h
))))
1975 (and (integerp (car h
))
1976 (> (car h
) 1900) (< (car h
) 2100)))
1979 (car h
) "*Only one limit allowed in Holidays"))
1980 (setq limit
(calcFunc-vint (car h
) '(intv 3 1 2737)))
1981 (if (equal limit
'(vec))
1982 (math-reject-arg (car h
) "*Limit is out of range")))
1983 ((or (math-expr-contains (car h
) '(var y var-y
))
1984 (math-expr-contains (car h
) '(var m var-m
)))
1985 (setq exprs
(cons (car h
) exprs
)))
1987 (car h
) "*Holidays must contain a vector of holidays"))))
1988 (if (= (length weekdays
) 7)
1989 (math-reject-arg nil
"*Too many weekend days"))
1990 (setq math-holidays-cache
(list (list -
1) ; 0: days list
1991 (list -
1) ; 1: inverse-days list
1994 (or limit
'(intv 3 1 2737))
1995 nil
; 5: (lo.hi) expanded years
1997 hours
) ; 7: business hours
1998 math-holidays-cache-tag
(calc-var-value 'var-Holidays
))))
2000 (let ((year (calcFunc-year date
))
2001 (limits (nth 5 math-holidays-cache
))
2003 (or (eq (calcFunc-in year
(nth 4 math-holidays-cache
)) 1)
2005 (or (eq (car-safe date
) 'date
) (setq date
(list 'date date
)))
2006 (math-reject-arg date
"*Date is outside valid range")))
2008 (let ((days (nth 6 math-holidays-cache
)))
2010 (let ((math-sh-year nil
)) ; see below
2011 (setcar (nthcdr 6 math-holidays-cache
) nil
)
2012 (math-setup-add-holidays (cons 'vec
(cdr days
)))
2013 (setcar (nthcdr 2 math-holidays-cache
) (car days
))))
2014 (cond ((not (nth 2 math-holidays-cache
))
2018 (setcar (nthcdr 5 math-holidays-cache
) (cons year year
))
2019 (math-setup-year-holidays year
)
2021 ((< year
(car limits
))
2022 (message "Computing holidays, %d .. %d"
2023 year
(1- (car limits
)))
2024 (calc-set-command-flag 'clear-message
)
2025 (while (< year
(car limits
))
2026 (setcar limits
(1- (car limits
)))
2027 (math-setup-year-holidays (car limits
)))
2029 ((> year
(cdr limits
))
2030 (message "Computing holidays, %d .. %d"
2031 (1+ (cdr limits
)) year
)
2032 (calc-set-command-flag 'clear-message
)
2033 (while (> year
(cdr limits
))
2034 (setcdr limits
(1+ (cdr limits
)))
2035 (math-setup-year-holidays (cdr limits
)))
2040 (or done
(setq math-holidays-cache-tag t
))))))
2042 (defun math-setup-year-holidays (math-sh-year)
2043 (let ((exprs (nth 2 math-holidays-cache
)))
2045 (let* ((var-y math-sh-year
)
2047 (expr (math-evaluate-expr (car exprs
))))
2048 (if (math-expr-contains expr
'(var m var-m
))
2050 (while (<= (setq var-m
(1+ var-m
)) 12)
2051 (math-setup-add-holidays (math-evaluate-expr expr
))))
2052 (math-setup-add-holidays expr
)))
2053 (setq exprs
(cdr exprs
)))))
2055 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
2056 (cond ((eq (car-safe days
) 'vec
)
2057 (while (setq days
(cdr days
))
2058 (math-setup-add-holidays (car days
))))
2059 ((eq (car-safe days
) 'intv
)
2060 (let ((day (math-ceiling (nth 2 days
))))
2061 (or (eq (calcFunc-in day days
) 1)
2062 (setq day
(math-add day
1)))
2063 (while (eq (calcFunc-in day days
) 1)
2064 (math-setup-add-holidays day
)
2065 (setq day
(math-add day
1)))))
2066 ((eq (car-safe days
) 'date
)
2067 (math-setup-add-holidays (nth 1 days
)))
2070 (let ((b (math-to-business-day days math-sh-year
)))
2071 (or (cdr b
) ; don't register holidays twice!
2072 (let ((prev (car math-holidays-cache
))
2073 (iprev (nth 1 math-holidays-cache
)))
2074 (while (and (cdr prev
) (< (nth 1 prev
) days
))
2075 (setq prev
(cdr prev
) iprev
(cdr iprev
)))
2076 (setcdr prev
(cons days
(cdr prev
)))
2077 (setcdr iprev
(cons (car b
) (cdr iprev
)))
2078 (while (setq iprev
(cdr iprev
))
2079 (setcar iprev
(1- (car iprev
))))))))
2081 (math-reject-arg (list 'date days
) "*Invalid holiday value"))
2083 (math-reject-arg days
"*Holiday formula failed to evaluate"))))
2090 ;;; Build a standard deviation form. [X X X]
2091 (defun math-make-sdev (x sigma
)
2092 (if (memq (car-safe x
) '(date mod sdev intv vec
))
2093 (math-reject-arg x
'realp
))
2094 (if (memq (car-safe sigma
) '(date mod sdev intv vec
))
2095 (math-reject-arg sigma
'realp
))
2096 (if (or (Math-negp sigma
) (memq (car-safe sigma
) '(cplx polar
)))
2097 (setq sigma
(math-abs sigma
)))
2098 (if (and (Math-zerop sigma
) (Math-scalarp x
))
2100 (list 'sdev x sigma
)))
2101 (defun calcFunc-sdev (x sigma
)
2102 (math-make-sdev x sigma
))
2108 (defun math-normalize-mod (a)
2109 (let ((n (math-normalize (nth 1 a
)))
2110 (m (math-normalize (nth 2 a
))))
2111 (if (and (math-anglep n
) (math-anglep m
) (math-posp m
))
2113 (math-normalize (list 'calcFunc-makemod n m
)))))
2115 ;;; Build a modulo form. [N R R]
2116 (defun math-make-mod (n m
)
2117 (setq calc-previous-modulo m
)
2119 (cond ((not (Math-anglep m
))
2120 (math-reject-arg m
'anglep
))
2121 ((not (math-posp m
))
2122 (math-reject-arg m
'posp
))
2124 (if (or (Math-negp n
)
2125 (not (Math-lessp n m
)))
2126 (list 'mod
(math-mod n m
) m
)
2128 ((memq (car n
) '(+ -
/ vec neg
))
2131 (mapcar (function (lambda (x) (math-make-mod x m
)))
2133 ((and (eq (car n
) '*) (Math-anglep (nth 1 n
)))
2134 (math-mul (math-make-mod (nth 1 n
) m
) (nth 2 n
)))
2135 ((memq (car n
) '(* ^ var calcFunc-subscr
))
2136 (math-mul (math-make-mod 1 m
) n
))
2137 (t (math-reject-arg n
'anglep
)))))
2138 (defun calcFunc-makemod (n m
)
2139 (math-make-mod n m
))
2143 ;;;; Interval forms.
2145 ;;; Build an interval form. [X S X X]
2146 (defun math-make-intv (mask lo hi
)
2147 (if (memq (car-safe lo
) '(cplx polar mod sdev intv vec
))
2148 (math-reject-arg lo
'realp
))
2149 (if (memq (car-safe hi
) '(cplx polar mod sdev intv vec
))
2150 (math-reject-arg hi
'realp
))
2151 (or (eq (eq (car-safe lo
) 'date
) (eq (car-safe hi
) 'date
))
2152 (math-reject-arg (if (eq (car-safe lo
) 'date
) hi lo
) 'datep
))
2153 (if (and (or (Math-realp lo
) (eq (car lo
) 'date
))
2154 (or (Math-realp hi
) (eq (car hi
) 'date
)))
2155 (let ((cmp (math-compare lo hi
)))
2159 (list 'intv mask lo hi
))
2162 (list 'intv
2 lo lo
)
2163 (list 'intv mask lo lo
))
2164 (list 'intv mask lo hi
))))
2165 (list 'intv mask lo hi
)))
2166 (defun calcFunc-intv (mask lo hi
)
2167 (if (math-messy-integerp mask
) (setq mask
(math-trunc mask
)))
2168 (or (natnump mask
) (math-reject-arg mask
'fixnatnump
))
2169 (or (<= mask
3) (math-reject-arg mask
'range
))
2170 (math-make-intv mask lo hi
))
2172 (defun math-sort-intv (mask lo hi
)
2173 (if (Math-lessp hi lo
)
2174 (math-make-intv (aref [0 2 1 3] mask
) hi lo
)
2175 (math-make-intv mask lo hi
)))
2180 (defun math-combine-intervals (a am b bm c cm d dm
)
2182 (if (= (setq res
(math-compare a c
)) 1)
2185 (setq am
(or am cm
))))
2186 (if (= (setq res
(math-compare b d
)) -
1)
2189 (setq bm
(or bm dm
))))
2190 (math-make-intv (+ (if am
2 0) (if bm
1 0)) a b
)))
2193 (defun math-div-mod (a b m
) ; [R R R R] (Returns nil if no solution)
2194 (and (Math-integerp a
) (Math-integerp b
) (Math-integerp m
)
2195 (let ((u1 1) (u3 b
) (v1 0) (v3 m
))
2196 (while (not (eq v3
0)) ; See Knuth sec 4.5.2, exercise 15
2197 (let* ((q (math-idivmod u3 v3
))
2198 (t1 (math-sub u1
(math-mul v1
(car q
)))))
2199 (setq u1 v1 u3 v3 v1 t1 v3
(cdr q
))))
2200 (let ((q (math-idivmod a u3
)))
2202 (math-mod (math-mul (car q
) u1
) m
))))))
2204 (defun math-mod-intv (a b
)
2205 (let* ((q1 (math-floor (math-div (nth 2 a
) b
)))
2206 (q2 (math-floor (math-div (nth 3 a
) b
)))
2207 (m1 (math-sub (nth 2 a
) (math-mul q1 b
)))
2208 (m2 (math-sub (nth 3 a
) (math-mul q2 b
))))
2209 (cond ((equal q1 q2
)
2210 (math-sort-intv (nth 1 a
) m1 m2
))
2211 ((and (math-equal-int (math-sub q2 q1
) 1)
2213 (memq (nth 1 a
) '(0 2)))
2214 (math-make-intv (nth 1 a
) m1 b
))
2216 (math-make-intv 2 0 b
)))))
2218 ;; The variables math-exp-str and math-exp-pos are local to
2219 ;; math-read-exprs in math-aent.el, but are used by
2220 ;; math-read-angle-brackets, which is called (indirectly) by
2222 (defvar math-exp-str
)
2223 (defvar math-exp-pos
)
2225 (defun math-read-angle-brackets ()
2226 (let* ((last (or (math-check-for-commas t
) (length math-exp-str
)))
2227 (str (substring math-exp-str math-exp-pos last
))
2229 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str
)
2230 (let ((str1 (substring str
0 (1- (match-end 0))))
2231 (str2 (substring str
(match-end 0)))
2232 (calc-hashes-used 0))
2233 (setq str1
(math-read-expr (concat "[" str1
"]")))
2234 (if (eq (car-safe str1
) 'error
)
2236 (setq str2
(math-read-expr str2
))
2237 (if (eq (car-safe str2
) 'error
)
2239 (append '(calcFunc-lambda) (cdr str1
) (list str2
)))))
2240 (if (string-match "#" str
)
2241 (let ((calc-hashes-used 0))
2242 (and (setq str
(math-read-expr str
))
2243 (if (eq (car-safe str
) 'error
)
2245 (append '(calcFunc-lambda)
2246 (calc-invent-args calc-hashes-used
)
2248 (math-parse-date str
)))))
2250 (throw 'syntax res
))
2251 (if (eq (car-safe res
) 'error
)
2252 (throw 'syntax
(nth 2 res
)))
2253 (setq math-exp-pos
(1+ last
))
2257 (provide 'calc-forms
)
2259 ;;; calc-forms.el ends here