1 ;;; calc-forms.el --- data format conversion functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;; Declare functions which are defined elsewhere.
32 (declare-function calendar-current-time-zone
"cal-dst" ())
33 (declare-function calendar-absolute-from-gregorian
"calendar" (date))
34 (declare-function dst-in-effect
"cal-dst" (date))
40 (let ((time (current-time-string)))
41 (calc-enter-result 0 "time"
44 (string-to-number (substring time
11 13))
45 (string-to-number (substring time
14 16))
46 (string-to-number (substring time
17 19)))
47 (list 'hms
24 0 0))))))
49 (defun calc-to-hms (arg)
53 (if (eq calc-angle-mode
'rad
)
54 (calc-unary-op ">rad" 'calcFunc-rad arg
)
55 (calc-unary-op ">deg" 'calcFunc-deg arg
))
56 (calc-unary-op ">hms" 'calcFunc-hms arg
))))
58 (defun calc-from-hms (arg)
64 (defun calc-hms-notation (fmt)
65 (interactive "sHours-minutes-seconds format (hms, @ \\=' \", etc.): ")
67 (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt
)
69 (calc-change-mode 'calc-hms-format
70 (concat "%s" (math-match-substring fmt
1)
71 (math-match-substring fmt
2)
72 "%s" (math-match-substring fmt
3)
73 (math-match-substring fmt
4)
74 "%s" (math-match-substring fmt
5))
76 (setq-default calc-hms-format calc-hms-format
)) ; for minibuffer
77 (error "Bad hours-minutes-seconds format"))))
79 (defun calc-date-notation (fmt arg
)
80 (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP")
82 (if (string-match-p "\\`\\s-*\\'" fmt
)
84 (if (string-match "\\` *\\([0-9]\\|10\\|11\\) *\\'" fmt
)
85 (setq fmt
(nth (string-to-number fmt
) calc-standard-date-formats
)))
86 (or (string-match "[a-zA-Z]" fmt
)
87 (error "Bad date format specifier"))
89 (>= (setq arg
(prefix-numeric-value arg
)) 0)
91 (setq calc-standard-date-formats
92 (copy-sequence calc-standard-date-formats
))
93 (setcar (nthcdr arg calc-standard-date-formats
) fmt
))
94 (let ((case-fold-search nil
))
95 (and (not (string-match "<.*>" fmt
))
96 ;; Find time part to put in <...>
97 (string-match "\\`[^hHspPT]*\\([^ac-gi-lnoqrt-zAC-GI-OQRU-Z]*\\(bs\\|bm\\|bh\\|BS\\|BH\\|[hHmpPsST]\\)+[^ac-gi-lnoqrt-zAC-GI-OQRU-Z]*\\)[^hHspPT]*\\'" fmt
)
98 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
99 (regexp-quote (math-match-substring fmt
1))
100 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt
)
101 (setq fmt
(concat (substring fmt
0 (match-beginning 0))
103 (substring fmt
(match-beginning 0) (match-end 0))
105 (substring fmt
(match-end 0))))))
110 (let ((case-fold-search nil
))
111 (and (setq temp
(string-match ":[BS]S" fmt
))
113 (while (setq pos
(string-match "[<>a-zA-Z]" fmt
))
115 (setq lfmt
(cons (substring fmt
0 pos
) lfmt
)))
117 (cond ((= (aref fmt pos
) ?\
<)
118 (and time
(error "Nested <'s not allowed"))
119 (and lfmt
(setq fullfmt
(nconc lfmt fullfmt
)
122 ((= (aref fmt pos
) ?\
>)
123 (or time
(error "Misplaced > in format"))
124 (and lfmt
(setq fullfmt
(cons (nreverse lfmt
) fullfmt
)
128 (if (string-match "\\`[^a-zA-Z]*[bBZI][a-zA-Z]" fmt
)
129 (setq pos2
(1+ pos2
)))
130 (while (and (< pos2
(length fmt
))
131 (= (upcase (aref fmt pos2
))
132 (upcase (aref fmt
(1- pos2
)))))
133 (setq pos2
(1+ pos2
)))
134 (setq sym
(intern (substring fmt pos pos2
)))
135 (or (memq sym
'(Y YY BY YYY YYYY
137 aa AA aaa AAA aaaa AAAA
138 bb BB bbb BBB bbbb BBBB
139 M MM BM mmm Mmm Mmmm MMM MMMM
141 W www Www Wwww WWW WWWW
144 m mm bm s ss bs SS BS C
146 (and (eq sym
'X
) (not lfmt
) (not fullfmt
))
147 (error "Bad format code: %s" sym
))
148 (and (memq sym
'(bb BB bbb BBB bbbb BBBB
))
149 (setq lfmt
(cons 'b lfmt
)))
150 (setq lfmt
(cons sym lfmt
))))
151 (setq fmt
(substring fmt pos2
)))
153 (setq lfmt
(cons fmt lfmt
)))
155 (setq fullfmt
(cons (nreverse lfmt
) fullfmt
))
156 (setq fullfmt
(nconc lfmt fullfmt
))))
157 (calc-change-mode 'calc-date-format
(nreverse fullfmt
) t
))))
160 (defun calc-hms-mode ()
163 (calc-change-mode 'calc-angle-mode
'hms
)
164 (message "Angles measured in degrees-minutes-seconds")))
167 (defun calc-now (arg)
169 (calc-date-zero-args "now" 'calcFunc-now arg
))
171 (defun calc-date-part (arg)
172 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
173 (if (or (< arg
1) (> arg
9))
174 (error "Part code out of range"))
177 (nth arg
'(nil "year" "mnth" "day" "hour" "minu"
178 "sec" "wday" "yday" "hmst"))
179 (list (nth arg
'(nil calcFunc-year calcFunc-month
180 calcFunc-day calcFunc-hour
181 calcFunc-minute calcFunc-second
182 calcFunc-weekday calcFunc-yearday
186 (defun calc-date (arg)
188 (if (or (< arg
1) (> arg
6))
189 (error "Between one and six arguments are allowed"))
191 (calc-enter-result arg
"date" (cons 'calcFunc-date
(calc-top-list-n arg
)))))
193 (defun calc-julian (arg)
195 (calc-date-one-arg "juln" 'calcFunc-julian arg
))
197 (defun calc-unix-time (arg)
199 (calc-date-one-arg "unix" 'calcFunc-unixtime arg
))
201 (defun calc-time-zone (arg)
203 (calc-date-zero-args "zone" 'calcFunc-tzone arg
))
205 (defun calc-convert-time-zones (old &optional new
)
206 (interactive "sFrom time zone: ")
209 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv
(calc-top-list-n 3)))
210 (if (equal old
"") (setq old
"local"))
212 (setq new
(read-string (concat "From time zone: " old
214 (if (stringp old
) (setq old
(math-read-expr old
)))
215 (if (eq (car-safe old
) 'error
)
216 (error "Error in expression: %S" (nth 1 old
)))
217 (if (equal new
"") (setq new
"local"))
218 (if (stringp new
) (setq new
(math-read-expr new
)))
219 (if (eq (car-safe new
) 'error
)
220 (error "Error in expression: %S" (nth 1 new
)))
221 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
222 (calc-top-n 1) old new
)))))
224 (defun calc-new-week (arg)
226 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg
))
228 (defun calc-new-month (arg)
230 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg
))
232 (defun calc-new-year (arg)
234 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg
))
236 (defun calc-inc-month (arg)
238 (calc-date-one-arg "incm" 'calcFunc-incmonth arg
))
240 (defun calc-business-days-plus (arg)
243 (calc-binary-op "bus+" 'calcFunc-badd arg
)))
245 (defun calc-business-days-minus (arg)
248 (calc-binary-op "bus-" 'calcFunc-bsub arg
)))
250 (defun calc-date-zero-args (prefix func arg
)
253 (calc-enter-result 1 prefix
(list func
(calc-top-n 1)))
254 (calc-enter-result 0 prefix
(if arg
255 (list func
(prefix-numeric-value arg
))
258 (defun calc-date-one-arg (prefix func arg
)
261 (calc-enter-result 2 prefix
(cons func
(calc-top-list-n 2)))
262 (calc-enter-result 1 prefix
(if arg
263 (list func
(calc-top-n 1)
264 (prefix-numeric-value arg
))
265 (list func
(calc-top-n 1)))))))
268 ;;;; Hours-minutes-seconds forms.
270 (defun math-normalize-hms (a)
271 (let ((h (math-normalize (nth 1 a
)))
272 (m (math-normalize (nth 2 a
)))
273 (s (let ((calc-internal-prec (max (- calc-internal-prec
4) 3)))
274 (math-normalize (nth 3 a
)))))
277 (and (= h
0) (math-negp m
))
278 (and (= h
0) (= m
0) (math-negp s
)))
281 (setq s
(math-add s -
60)
284 (setq m
(math-add m -
60)
286 (if (not (Math-lessp -
60 s
))
287 (setq s
(math-add s
60)
289 (if (not (Math-lessp -
60 m
))
290 (setq m
(math-add m
60)
293 (setq s
(math-add s
60)
296 (setq m
(math-add m
60)
298 (if (not (Math-lessp s
60))
299 (setq s
(math-add s -
60)
301 (if (not (Math-lessp m
60))
302 (setq m
(math-add m -
60)
304 (if (and (eq (car-safe s
) 'float
)
305 (<= (+ (math-numdigs (nth 1 s
)) (nth 2 s
))
306 (- 2 calc-internal-prec
)))
310 ;;; Convert A from ANG or current angular mode to HMS format.
311 (defun math-to-hms (a &optional ang
) ; [X R] [Public]
312 (cond ((eq (car-safe a
) 'hms
) a
)
313 ((eq (car-safe a
) 'sdev
)
314 (math-make-sdev (math-to-hms (nth 1 a
))
315 (math-to-hms (nth 2 a
))))
316 ((not (Math-numberp a
))
317 (list 'calcFunc-hms a
))
319 (math-neg (math-to-hms (math-neg a
) ang
)))
321 (and (not math-simplifying-units
) calc-angle-mode
))
323 (math-to-hms (math-div a
(math-pi-over-180)) 'deg
))
324 ((memq (car-safe a
) '(cplx polar
)) a
)
326 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
327 ; (math-normalize a)))
329 (let* ((b (math-mul a
3600))
330 (hm (math-trunc (math-div b
60)))
331 (hmd (math-idivmod hm
60)))
335 (math-sub b
(math-mul hm
60))))))))
336 (defun calcFunc-hms (h &optional m s
)
337 (or (Math-realp h
) (math-reject-arg h
'realp
))
339 (or (Math-realp m
) (math-reject-arg m
'realp
))
341 (or (Math-realp s
) (math-reject-arg s
'realp
))
342 (if (and (not (Math-lessp m
0)) (Math-lessp m
60)
343 (not (Math-lessp s
0)) (Math-lessp s
60))
344 (math-add (math-to-hms h
)
346 (math-to-hms (math-add h
347 (math-add (math-div (or m
0) 60)
348 (math-div (or s
0) 3600)))
351 ;;; Convert A from HMS format to ANG or current angular mode.
352 (defun math-from-hms (a &optional ang
) ; [R X] [Public]
353 (cond ((not (eq (car-safe a
) 'hms
))
356 (if (eq (car-safe a
) 'sdev
)
357 (math-make-sdev (math-from-hms (nth 1 a
) ang
)
358 (math-from-hms (nth 2 a
) ang
))
360 (and (not math-simplifying-units
) calc-angle-mode
))
362 (list 'calcFunc-rad a
)
363 (list 'calcFunc-deg a
)))))
365 (math-neg (math-from-hms (math-neg a
) ang
)))
367 (and (not math-simplifying-units
) calc-angle-mode
))
369 (math-mul (math-from-hms a
'deg
) (math-pi-over-180)))
371 (math-add (math-div (math-add (math-div (nth 3 a
)
380 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
381 ;;; These versions are rewritten to use arbitrary-size integers.
383 ;;; A numerical date is the number of days since midnight on
384 ;;; the morning of December 31, 1 B.C. (Gregorian) or January 2, 1 A.D. (Julian).
385 ;;; Emacs's calendar refers to such a date as an absolute date, some Calc function
386 ;;; names also use that terminology. If the date is a non-integer, it represents
387 ;;; a specific date and time.
388 ;;; A "dt" is a list of the form, (year month day), corresponding to
389 ;;; an integer code, or (year month day hour minute second), corresponding
390 ;;; to a non-integer code.
392 (defun math-date-to-gregorian-dt (date)
393 "Return the day (YEAR MONTH DAY) in the Gregorian calendar.
394 DATE is the number of days since December 31, -1 in the Gregorian calendar."
397 (year (math-quotient (math-add date
(if (Math-lessp date
711859)
398 365 ; for speed, we take
399 -
108)) ; >1950 as a special case
400 (if (math-negp date
) 366 365)))
401 ; this result may be an overestimate
403 (while (Math-lessp date
(setq temp
(math-absolute-from-gregorian-dt year
1 1)))
404 (setq year
(math-add year -
1)))
405 (if (eq year
0) (setq year -
1))
406 (setq date
(1+ (math-sub date temp
)))
408 (if (math-leap-year-p year
)
409 [1 32 61 92 122 153 183 214 245 275 306 336 999]
410 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
411 (while (>= date
(aref temp month
))
412 (setq month
(1+ month
)))
413 (setq day
(1+ (- date
(aref temp
(1- month
)))))
414 (list year month day
)))
416 (defun math-date-to-julian-dt (date)
417 "Return the day (YEAR MONTH DAY) in the Julian calendar.
418 DATE is the number of days since December 31, -1 in the Gregorian calendar."
421 (year (math-quotient (math-add date
(if (Math-lessp date
711859)
422 367 ; for speed, we take
423 -
106)) ; >1950 as a special case
424 (if (math-negp date
) 366 365)))
425 ; this result may be an overestimate
427 (while (Math-lessp date
(setq temp
(math-absolute-from-julian-dt year
1 1)))
428 (setq year
(math-add year -
1)))
429 (if (eq year
0) (setq year -
1))
430 (setq date
(1+ (math-sub date temp
)))
432 (if (math-leap-year-p year t
)
433 [1 32 61 92 122 153 183 214 245 275 306 336 999]
434 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
435 (while (>= date
(aref temp month
))
436 (setq month
(1+ month
)))
437 (setq day
(1+ (- date
(aref temp
(1- month
)))))
438 (list year month day
)))
440 (defun math-date-to-dt (value)
441 "Return the day and time of VALUE.
442 The integer part of VALUE is the number of days since Dec 31, -1
443 in the Gregorian calendar and the remaining part determines the time."
444 (if (eq (car-safe value
) 'date
)
445 (setq value
(nth 1 value
)))
446 (or (math-realp value
)
447 (math-reject-arg value
'datep
))
448 (let* ((parts (math-date-parts value
))
451 (dt (if (and calc-gregorian-switch
454 (nth 3 calc-gregorian-switch
)
455 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))
457 (math-date-to-julian-dt date
)
458 (math-date-to-gregorian-dt date
))))
459 (if (math-integerp value
)
465 (math-add (% time
60) (nth 2 parts
)))))))
467 (defun math-date-to-iso-dt (date)
468 "Return the ISO8601 date (year week day) of DATE."
469 (unless (Math-integerp date
)
470 (setq date
(math-floor date
)))
471 (let* ((approx (nth 0 (math-date-to-gregorian-dt (math-sub date
3))))
472 (year (math-add approx
475 (while (>= (math-compare date
476 (math-absolute-from-iso-dt (setq y
(math-add y
1)) 1 1)) 0)
477 (setq sum
(+ sum
1)))
481 (math-add (car (math-idivmod
482 (math-sub date
(math-absolute-from-iso-dt year
1 1))
485 (let ((day (calcFunc-mod date
7)))
486 (if (= day
0) 7 day
)))))
488 (defun math-dt-to-date (dt)
489 (or (integerp (nth 1 dt
))
490 (math-reject-arg (nth 1 dt
) 'fixnump
))
491 (if (or (< (nth 1 dt
) 1) (> (nth 1 dt
) 12))
492 (math-reject-arg (nth 1 dt
) "Month value is out of range"))
493 (or (integerp (nth 2 dt
))
494 (math-reject-arg (nth 2 dt
) 'fixnump
))
495 (if (or (< (nth 2 dt
) 1) (> (nth 2 dt
) 31))
496 (math-reject-arg (nth 2 dt
) "Day value is out of range"))
497 (let ((date (math-absolute-from-dt (car dt
) (nth 1 dt
) (nth 2 dt
))))
499 (math-add (math-float date
)
500 (math-div (math-add (+ (* (nth 3 dt
) 3600)
506 (defun math-iso-dt-to-date (dt)
507 (let ((date (math-absolute-from-iso-dt (car dt
) (nth 1 dt
) (nth 2 dt
))))
509 (math-add (math-float date
)
510 (math-div (math-add (+ (* (nth 3 dt
) 3600)
516 (defun math-date-parts (value &optional offset
)
517 (let* ((date (math-floor value
))
518 (time (math-round (math-mul (math-sub value
(or offset date
)) 86400)
519 (and (> calc-internal-prec
12)
520 (- calc-internal-prec
12))))
521 (ftime (math-floor time
)))
524 (math-sub time ftime
))))
527 (defun math-this-year ()
528 (nth 5 (decode-time)))
530 (defun math-leap-year-p (year &optional julian
)
531 "Non-nil if YEAR is a leap year.
532 If JULIAN is non-nil, then use the criterion for leap years
533 in the Julian calendar, otherwise use the criterion in the
537 (= (math-imod (math-neg year
) 4) 1)
538 (= (math-imod year
4) 0))
540 (setq year
(math-sub -
1 year
)))
541 (setq year
(math-imod year
400))
542 (or (and (= (% year
4) 0) (/= (% year
100) 0))
545 (defun math-days-in-month (year month
)
546 (if (and (= month
2) (math-leap-year-p year
))
548 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month
))))
550 (defun math-day-in-year (year month day
&optional julian
)
551 "Return the number of days of the year up to YEAR MONTH DAY.
552 The count includes the given date.
553 If JULIAN is non-nil, use the Julian calendar, otherwise
554 use the Gregorian calendar."
555 (let ((day-of-year (+ day
(* 31 (1- month
)))))
558 (setq day-of-year
(- day-of-year
(/ (+ 23 (* 4 month
)) 10)))
559 (if (math-leap-year-p year julian
)
560 (setq day-of-year
(1+ day-of-year
)))))
563 (defun math-day-number (year month day
)
564 "Return the number of days of the year up to YEAR MONTH DAY.
565 The count includes the given date."
566 (if calc-gregorian-switch
567 (cond ((eq year
(nth 0 calc-gregorian-switch
))
569 (- (math-absolute-from-dt year month day
)
570 (math-absolute-from-dt year
1 1))))
571 ((Math-lessp year
(nth 0 calc-gregorian-switch
))
572 (math-day-in-year year month day t
))
574 (math-day-in-year year month day
)))
575 (math-day-in-year year month day
)))
577 (defun math-dt-before-p (dt1 dt2
)
578 "Non-nil if DT1 occurs before DT2.
579 A DT is a list of the form (YEAR MONTH DAY)."
580 (or (Math-lessp (nth 0 dt1
) (nth 0 dt2
))
581 (and (equal (nth 0 dt1
) (nth 0 dt2
))
582 (or (< (nth 1 dt1
) (nth 1 dt2
))
583 (and (= (nth 1 dt1
) (nth 1 dt2
))
584 (< (nth 2 dt1
) (nth 2 dt2
)))))))
586 (defun math-absolute-from-gregorian-dt (year month day
)
587 "Return the DATE of the day given by the Gregorian day YEAR MONTH DAY.
588 Recall that DATE is the number of days since December 31, -1
589 in the Gregorian calendar."
590 (if (eq year
0) (setq year -
1))
591 (let ((yearm1 (math-sub year
1)))
593 ;; Add the number of days of the year and the numbers of days
594 ;; in the previous years (leap year days to be added separately)
595 (math-add (math-day-in-year year month day
)
596 (math-add (math-mul 365 yearm1
)
597 ;; Add the number of Julian leap years
599 (math-quotient yearm1
4)
601 (math-quotient (math-sub 3 year
)
603 ;; Subtract the number of Julian leap years which are not
604 ;; Gregorian leap years. In C=4N+r centuries, there will
605 ;; be 3N+r of these days. The following will compute
607 (let* ((correction (math-mul (math-quotient yearm1
100) 3))
608 (res (math-idivmod correction
4)))
609 (math-add (if (= (cdr res
) 0)
614 (defun math-absolute-from-julian-dt (year month day
)
615 "Return the DATE of the day given by the Julian day YEAR MONTH DAY.
616 Recall that DATE is the number of days since December 31, -1
617 in the Gregorian calendar."
618 (if (eq year
0) (setq year -
1))
619 (let ((yearm1 (math-sub year
1)))
621 ;; Add the number of days of the year and the numbers of days
622 ;; in the previous years (leap year days to be added separately)
623 (math-add (math-day-in-year year month day
)
624 (math-add (math-mul 365 yearm1
)
625 ;; Add the number of Julian leap years
627 (math-quotient yearm1
4)
629 (math-quotient (math-sub 3 year
)
631 ;; Adjustment, since January 1, 1 (Julian) is absolute day -1
634 ;; calc-gregorian-switch is a customizable variable defined in calc.el
635 (defvar calc-gregorian-switch
)
637 (defun math-absolute-from-iso-dt (year week day
)
638 "Return the DATE of the day given by the iso8601 day YEAR WEEK DAY."
639 (let* ((janfour (math-absolute-from-gregorian-dt year
1 4))
640 (prevmon (math-sub janfour
641 (cdr (math-idivmod (math-sub janfour
1) 7)))))
643 (math-add prevmon
(* (1- week
) 7))
644 (if (zerop day
) 6 (1- day
)))))
646 (defun math-absolute-from-dt (year month day
)
647 "Return the DATE of the day given by the day YEAR MONTH DAY.
648 Recall that DATE is the number of days since December 31, -1
649 in the Gregorian calendar."
650 (if (and calc-gregorian-switch
651 ;; The next few lines determine if the given date
652 ;; occurs before the switch to the Gregorian calendar.
653 (math-dt-before-p (list year month day
) calc-gregorian-switch
))
654 (math-absolute-from-julian-dt year month day
)
655 (math-absolute-from-gregorian-dt year month day
)))
657 ;;; It is safe to redefine these in your init file to use a different
660 (defvar math-long-weekday-names
'( "Sunday" "Monday" "Tuesday" "Wednesday"
661 "Thursday" "Friday" "Saturday" ))
662 (defvar math-short-weekday-names
'( "Sun" "Mon" "Tue" "Wed"
665 (defvar math-long-month-names
'( "January" "February" "March" "April"
666 "May" "June" "July" "August"
667 "September" "October" "November" "December" ))
668 (defvar math-short-month-names
'( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
669 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
672 (defvar math-format-date-cache nil
)
674 ;; The variables math-fd-date, math-fd-dt, math-fd-year,
675 ;; math-fd-month, math-fd-day, math-fd-weekday, math-fd-hour,
676 ;; math-fd-minute, math-fd-second, math-fd-bc-flag are local
677 ;; to math-format-date, but are used by math-format-date-part,
678 ;; which is called by math-format-date.
679 (defvar math-fd-date
)
681 (defvar math-fd-year
)
682 (defvar math-fd-month
)
684 (defvar math-fd-weekday
)
685 (defvar math-fd-hour
)
686 (defvar math-fd-minute
)
687 (defvar math-fd-second
)
688 (defvar math-fd-bc-flag
)
689 (defvar math-fd-iso-dt
)
690 (defvar math-fd-isoyear
)
691 (defvar math-fd-isoweek
)
692 (defvar math-fd-isoweekday
)
694 (defun math-format-date (math-fd-date)
695 (if (eq (car-safe math-fd-date
) 'date
)
696 (setq math-fd-date
(nth 1 math-fd-date
)))
697 (let ((entry (list math-fd-date calc-internal-prec calc-date-format
)))
698 (or (cdr (assoc entry math-format-date-cache
))
699 (let* ((math-fd-dt nil
)
701 (calc-group-digits nil
)
702 (calc-leading-zeros nil
)
703 (calc-number-radix 10)
704 (calc-twos-complement-mode nil
)
705 math-fd-year math-fd-month math-fd-day math-fd-weekday
706 math-fd-hour math-fd-minute math-fd-second
707 math-fd-isoyear math-fd-isoweek math-fd-isoweekday
708 (math-fd-bc-flag nil
)
709 (fmt (apply 'concat
(mapcar 'math-format-date-part
711 (setq math-format-date-cache
(cons (cons entry fmt
)
712 math-format-date-cache
))
713 (and (setq math-fd-dt
(nthcdr 10 math-format-date-cache
))
714 (setcdr math-fd-dt nil
))
717 (defconst math-julian-date-beginning
'(float 17214225 -
1)
718 "The beginning of the Julian date calendar,
719 as measured in the number of days before December 31, 1 BC (Gregorian).")
721 (defconst math-julian-date-beginning-int
1721423
722 "The beginning of the Julian date calendar,
723 as measured in the integer number of days before December 31, 1 BC (Gregorian).")
725 (defun math-format-date-part (x)
729 (if (math-integerp math-fd-date
)
731 (apply 'concat
(mapcar 'math-format-date-part x
))))
735 (math-format-number math-fd-date
))
737 (math-format-number (math-floor math-fd-date
)))
740 (math-add math-fd-date math-julian-date-beginning
)))
742 (math-format-number (math-add
743 (math-floor math-fd-date
)
744 math-julian-date-beginning-int
)))
746 (math-format-number (nth 1 (math-date-parts math-fd-date
719164))))
747 ((memq x
'(IYYY Iww w
))
750 (setq math-fd-iso-dt
(math-date-to-iso-dt math-fd-date
)
751 math-fd-isoyear
(car math-fd-iso-dt
)
752 math-fd-isoweek
(nth 1 math-fd-iso-dt
)
753 math-fd-isoweekday
(nth 2 math-fd-iso-dt
)))
755 (let* ((neg (Math-negp math-fd-isoyear
))
756 (pyear (calcFunc-abs math-fd-isoyear
)))
757 (if (and (natnump pyear
) (< pyear
10000))
758 (concat (if neg
"-" "") (format "%04d" pyear
))
759 (concat (if neg
"-" "+") (math-format-number pyear
)))))
761 (concat "W" (format "%02d" math-fd-isoweek
)))
763 (format "%d" math-fd-isoweekday
)))))
767 (setq math-fd-dt
(math-date-to-dt math-fd-date
)
768 math-fd-year
(car math-fd-dt
)
769 math-fd-month
(nth 1 math-fd-dt
)
770 math-fd-day
(nth 2 math-fd-dt
)
771 math-fd-weekday
(math-mod (math-floor math-fd-date
) 7)
772 math-fd-hour
(nth 3 math-fd-dt
)
773 math-fd-minute
(nth 4 math-fd-dt
)
774 math-fd-second
(nth 5 math-fd-dt
))
775 (and (memq 'b calc-date-format
)
776 (math-negp math-fd-year
)
777 (setq math-fd-year
(math-neg math-fd-year
)
778 math-fd-bc-flag t
))))
780 (if (and (integerp math-fd-year
) (> math-fd-year
1940) (< math-fd-year
2040))
781 (format (cond ((eq x
'YY
) "%02d")
784 (% math-fd-year
100))
785 (if (and (natnump math-fd-year
) (< math-fd-year
100))
786 (format "+%d" math-fd-year
)
787 (math-format-number math-fd-year
))))
789 (math-format-number math-fd-year
))
791 (if (and (natnump math-fd-year
) (< math-fd-year
100))
792 (format "+%d" math-fd-year
)
793 (math-format-number math-fd-year
)))
795 (let* ((year (if (Math-negp math-fd-year
)
796 (math-add math-fd-year
1)
798 (neg (Math-negp year
))
799 (pyear (calcFunc-abs year
)))
800 (if (and (natnump pyear
) (< pyear
10000))
801 (concat (if neg
"-" "") (format "%04d" pyear
))
802 (concat (if neg
"-" "+") (math-format-number pyear
)))))
805 (and (not math-fd-bc-flag
) "ad"))
807 (and (not math-fd-bc-flag
) "AD"))
809 (and (not math-fd-bc-flag
) "ad "))
811 (and (not math-fd-bc-flag
) "AD "))
813 (and (not math-fd-bc-flag
) "a.d."))
815 (and (not math-fd-bc-flag
) "A.D."))
817 (and math-fd-bc-flag
"bc"))
819 (and math-fd-bc-flag
"BC"))
821 (and math-fd-bc-flag
" bc"))
823 (and math-fd-bc-flag
" BC"))
825 (and math-fd-bc-flag
"b.c."))
827 (and math-fd-bc-flag
"B.C."))
830 (format "%d" math-fd-month
))
832 (format "%02d" math-fd-month
))
834 (format "%2d" math-fd-month
))
836 (downcase (nth (1- math-fd-month
) math-short-month-names
)))
838 (nth (1- math-fd-month
) math-short-month-names
))
840 (upcase (nth (1- math-fd-month
) math-short-month-names
)))
842 (nth (1- math-fd-month
) math-long-month-names
))
844 (upcase (nth (1- math-fd-month
) math-long-month-names
)))
846 (format "%d" math-fd-day
))
848 (format "%02d" math-fd-day
))
850 (format "%2d" math-fd-day
))
852 (format "%d" math-fd-weekday
))
854 (downcase (nth math-fd-weekday math-short-weekday-names
)))
856 (nth math-fd-weekday math-short-weekday-names
))
858 (upcase (nth math-fd-weekday math-short-weekday-names
)))
860 (nth math-fd-weekday math-long-weekday-names
))
862 (upcase (nth math-fd-weekday math-long-weekday-names
)))
864 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
866 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
868 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day
)))
870 (and math-fd-hour
(format "%d" math-fd-hour
)))
872 (and math-fd-hour
(format "%02d" math-fd-hour
)))
874 (and math-fd-hour
(format "%2d" math-fd-hour
)))
876 (and math-fd-hour
(format "%d" (1+ (%
(+ math-fd-hour
11) 12)))))
878 (and math-fd-hour
(format "%02d" (1+ (%
(+ math-fd-hour
11) 12)))))
880 (and math-fd-hour
(format "%2d" (1+ (%
(+ math-fd-hour
11) 12)))))
882 (and math-fd-hour
(if (< math-fd-hour
12) "a" "p")))
884 (and math-fd-hour
(if (< math-fd-hour
12) "A" "P")))
886 (and math-fd-hour
(if (< math-fd-hour
12) "am" "pm")))
888 (and math-fd-hour
(if (< math-fd-hour
12) "AM" "PM")))
890 (and math-fd-hour
(if (< math-fd-hour
12) "a.m." "p.m.")))
892 (and math-fd-hour
(if (< math-fd-hour
12) "A.M." "P.M.")))
894 (and math-fd-minute
(format "%d" math-fd-minute
)))
896 (and math-fd-minute
(format "%02d" math-fd-minute
)))
898 (and math-fd-minute
(format "%2d" math-fd-minute
)))
900 (and math-fd-second
(not (math-zerop math-fd-second
))
902 ((memq x
'(s ss bs SS BS
))
904 (not (and (memq x
'(SS BS
)) (math-zerop math-fd-second
)))
905 (if (integerp math-fd-second
)
906 (format (cond ((memq x
'(ss SS
)) "%02d")
907 ((memq x
'(bs BS
)) "%2d")
910 (concat (if (Math-lessp math-fd-second
10)
911 (cond ((memq x
'(ss SS
)) "0")
912 ((memq x
'(bs BS
)) " ")
915 (let ((calc-float-format
916 (list 'fix
(min (- 12 calc-internal-prec
)
918 (math-format-number math-fd-second
))))))))
920 ;; The variable math-pd-str is local to math-parse-date and
921 ;; math-parse-standard-date, but is used by math-parse-date-word,
922 ;; which is called by math-parse-date and math-parse-standard-date.
925 (defun math-parse-date (math-pd-str)
927 (or (math-parse-standard-date math-pd-str t
)
928 (math-parse-standard-date math-pd-str nil
)
929 (and (string-match "W[0-9][0-9]" math-pd-str
)
930 (math-parse-iso-date math-pd-str
))
931 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str
)
932 (list 'date
(math-read-number (math-match-substring math-pd-str
1))))
933 (let ((case-fold-search t
)
934 (year nil
) (month nil
) (day nil
) (weekday nil
)
935 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
)
936 (a nil
) (b nil
) (c nil
) (bigyear nil
) temp
)
938 ;; Extract the time, if any.
939 (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
)
940 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str
))
941 (let ((ampm (math-match-substring math-pd-str
6)))
942 (setq hour
(string-to-number (math-match-substring math-pd-str
1))
943 minute
(math-match-substring math-pd-str
2)
944 second
(math-match-substring math-pd-str
4)
945 math-pd-str
(concat (substring math-pd-str
0 (match-beginning 0))
946 (substring math-pd-str
(match-end 0))))
947 (if (equal minute
"")
949 (setq minute
(string-to-number minute
)))
950 (if (equal second
"")
952 (setq second
(math-read-number second
)))
958 (not (eq second
0))))
959 (throw 'syntax
"Hour value is out of range"))
960 (setq ampm
(upcase (aref ampm
0)))
961 (if (memq ampm
'(?N ?M
))
962 (if (and (= hour
12) (= minute
0) (eq second
0))
963 (if (eq ampm ?M
) (setq hour
0))
965 "Time must be 12:00:00 in this context"))
966 (if (or (= hour
0) (> hour
12))
967 (throw 'syntax
"Hour value is out of range"))
968 (if (eq (= ampm ?A
) (= hour
12))
969 (setq hour
(%
(+ hour
12) 24)))))))
971 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
972 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str
)
974 (setq math-pd-str
(copy-sequence math-pd-str
))
975 (aset math-pd-str
(match-beginning 1) ?\
/)))
977 ;; Extract obvious month or weekday names.
978 (if (string-match "[a-zA-Z]" math-pd-str
)
980 (setq month
(math-parse-date-word math-long-month-names
))
981 (setq weekday
(math-parse-date-word math-long-weekday-names
))
982 (or month
(setq month
983 (math-parse-date-word math-short-month-names
)))
984 (or weekday
(math-parse-date-word math-short-weekday-names
))
986 (if (setq temp
(math-parse-date-word
987 '( "noon" "midnight" "mid" )))
988 (setq hour
(if (= temp
1) 12 0) minute
0 second
0)))
989 (or (math-parse-date-word '( "ad" "a.d." ))
990 (if (math-parse-date-word '( "bc" "b.c." ))
992 (if (string-match "[a-zA-Z]+" math-pd-str
)
993 (throw 'syntax
(format "Bad word in date: \"%s\""
994 (math-match-substring math-pd-str
0))))))
996 ;; If there is a huge number other than the year, ignore it.
997 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str
)
998 (setq temp
(concat (substring math-pd-str
0 (match-beginning 0))
999 (substring math-pd-str
(match-end 0))))
1001 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp
))
1002 (setq math-pd-str temp
))
1004 ;; If there is a number with a sign or a large number, it is a year.
1005 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str
)
1006 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str
))
1007 (setq year
(math-match-substring math-pd-str
1)
1008 math-pd-str
(concat (substring math-pd-str
0 (match-beginning 1))
1009 (substring math-pd-str
(match-end 1)))
1010 year
(math-read-number year
)
1013 ;; Collect remaining numbers.
1015 (while (string-match "[0-9]+" math-pd-str temp
)
1016 (and c
(throw 'syntax
"Too many numbers in date"))
1017 (setq c
(string-to-number (math-match-substring math-pd-str
0)))
1018 (or b
(setq b c c nil
))
1019 (or a
(setq a b b nil
))
1020 (setq temp
(match-end 0)))
1022 ;; Check that we have the right amount of information.
1023 (setq temp
(+ (if year
1 0) (if month
1 0) (if day
1 0)
1024 (if a
1 0) (if b
1 0) (if c
1 0)))
1026 (throw 'syntax
"Too many numbers in date")
1027 (if (or (< temp
2) (and year
(= temp
2)))
1028 (throw 'syntax
"Not enough numbers in date")
1029 (if (= temp
2) ; if year omitted, assume current year
1030 (setq year
(math-this-year)))))
1032 ;; A large number must be a year.
1034 (if (and a
(or (> a
31) (< a
1)))
1035 (setq year a a b b c c nil
)
1036 (if (and b
(or (> b
31) (< b
1)))
1037 (setq year b b c c nil
)
1038 (if (and c
(or (> c
31) (< c
1)))
1039 (setq year c c nil
)))))
1041 ;; A medium-large number must be a day.
1043 (if (and a
(> a
12))
1044 (setq day a a b b c c nil
)
1045 (if (and b
(> b
12))
1046 (setq day b b c c nil
)
1047 (if (and c
(> c
12))
1048 (setq day c c nil
)))))
1050 ;; We may know enough to sort it out now.
1052 (or month
(setq month a
))
1053 (if (and year month
)
1056 ;; Interpret order of numbers as same as for display format.
1057 (setq temp calc-date-format
)
1059 (cond ((not (symbolp (car temp
))))
1060 ((memq (car temp
) '(Y YY BY YYY YYYY
))
1061 (or year
(setq year a a b b c
)))
1062 ((memq (car temp
) '(M MM BM mmm Mmm Mmmm MMM MMMM
))
1063 (or month
(setq month a a b b c
)))
1064 ((memq (car temp
) '(D DD BD
))
1065 (or day
(setq day a a b b c
))))
1066 (setq temp
(cdr temp
)))
1068 ;; If display format was not complete, assume American style.
1069 (or month
(setq month a a b b c
))
1070 (or day
(setq day a a b b c
))
1071 (or year
(setq year a a b b c
))))
1074 (setq year
(math-neg (math-abs year
))))
1076 (math-parse-date-validate year bigyear month day
1077 hour minute second
)))))
1079 (defun math-parse-date-validate (year bigyear month day hour minute second
)
1080 (and (not bigyear
) (natnump year
) (< year
100)
1081 (setq year
(+ year
(if (< year
40) 2000 1900))))
1083 (throw 'syntax
"Year value is out of range"))
1084 (if (or (< month
1) (> month
12))
1085 (throw 'syntax
"Month value is out of range"))
1086 (if (or (< day
1) (> day
(math-days-in-month year month
)))
1087 (throw 'syntax
"Day value is out of range"))
1094 (not (eq second
0))))
1095 (throw 'syntax
"Hour value is out of range"))
1096 (if (or (< minute
0) (> minute
59))
1097 (throw 'syntax
"Minute value is out of range"))
1098 (if (or (math-negp second
) (not (Math-lessp second
60)))
1099 (throw 'syntax
"Seconds value is out of range"))))
1100 (list 'date
(math-dt-to-date (append (list year month day
)
1101 (and hour
(list hour minute second
))))))
1103 (defun math-parse-iso-date-validate (isoyear isoweek isoweekday hour minute second
)
1104 (if (or (< isoweek
1) (> isoweek
53))
1105 (throw 'syntax
"Week value is out of range"))
1106 (if (or (< isoweekday
1) (> isoweekday
7))
1107 (throw 'syntax
"Weekday value is out of range"))
1114 (not (eq second
0))))
1115 (throw 'syntax
"Hour value is out of range"))
1116 (if (or (< minute
0) (> minute
59))
1117 (throw 'syntax
"Minute value is out of range"))
1118 (if (or (math-negp second
) (not (Math-lessp second
60)))
1119 (throw 'syntax
"Seconds value is out of range"))))
1120 (list 'date
(math-iso-dt-to-date (append (list isoyear isoweek isoweekday
)
1121 (and hour
(list hour minute second
))))))
1123 (defun math-parse-date-word (names &optional front
)
1125 (while (and names
(not (string-match (if (equal (car names
) "Sep")
1127 (regexp-quote (car names
)))
1129 (setq names
(cdr names
)
1132 (or (not front
) (= (match-beginning 0) 0))
1134 (setq math-pd-str
(concat (substring math-pd-str
0 (match-beginning 0))
1136 (substring math-pd-str
(match-end 0))))
1139 (defun math-parse-standard-date (math-pd-str with-time
)
1140 (let ((case-fold-search t
)
1142 (fmt calc-date-format
) this next
(gnext nil
)
1143 (isoyear nil
) (isoweek nil
) (isoweekday nil
)
1144 (year nil
) (month nil
) (day nil
) (bigyear nil
) (yearday nil
)
1145 (hour nil
) (minute nil
) (second nil
) (bc-flag nil
))
1146 (while (and fmt okay
)
1147 (setq this
(car fmt
)
1148 fmt
(setq fmt
(or (cdr fmt
)
1153 (if (consp next
) (setq next
(car next
)))
1154 (or (cond ((listp this
)
1160 (if (and (<= (length this
) (length math-pd-str
))
1162 (substring math-pd-str
0 (length this
))))
1163 (setq math-pd-str
(substring math-pd-str
(length this
)))))
1166 ((memq this
'(n N j J
))
1167 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str
)
1168 (setq num
(math-match-substring math-pd-str
0)
1169 math-pd-str
(substring math-pd-str
(match-end 0))
1170 num
(math-date-to-dt (math-read-number num
))
1172 (if (memq this
'(n N
))
1174 (if (or (eq this
'j
)
1175 (math-integerp num
))
1176 math-julian-date-beginning-int
1177 math-julian-date-beginning
)))
1178 hour
(or (nth 3 num
) hour
)
1179 minute
(or (nth 4 num
) minute
)
1180 second
(or (nth 5 num
) second
)
1185 (and (string-match "\\`[-+]?[0-9]+" math-pd-str
)
1186 (setq num
(math-match-substring math-pd-str
0)
1187 math-pd-str
(substring math-pd-str
(match-end 0))
1188 num
(math-date-to-dt
1190 (math-div (math-read-number num
)
1198 ((memq this
'(mmm Mmm MMM
))
1199 (setq month
(math-parse-date-word math-short-month-names t
)))
1200 ((memq this
'(Mmmm MMMM
))
1201 (setq month
(math-parse-date-word math-long-month-names t
)))
1202 ((memq this
'(www Www WWW
))
1203 (math-parse-date-word math-short-weekday-names t
))
1204 ((memq this
'(Wwww WWWW
))
1205 (math-parse-date-word math-long-weekday-names t
))
1207 (if (string-match "\\`a" math-pd-str
)
1208 (setq hour
(if (= hour
12) 0 hour
)
1209 math-pd-str
(substring math-pd-str
1))
1210 (if (string-match "\\`p" math-pd-str
)
1211 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
1212 math-pd-str
(substring math-pd-str
1)))))
1213 ((memq this
'(pp PP pppp PPPP
))
1214 (if (string-match "\\`am\\|a\\.m\\." math-pd-str
)
1215 (setq hour
(if (= hour
12) 0 hour
)
1216 math-pd-str
(substring math-pd-str
(match-end 0)))
1217 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str
)
1218 (setq hour
(if (= hour
12) 12 (%
(+ hour
12) 24))
1219 math-pd-str
(substring math-pd-str
(match-end 0))))))
1220 ((memq this
'(Y YY BY YYY YYYY ZYYY
))
1221 (and (if (memq next
'(MM DD ddd hh HH mm ss SS
))
1222 (if (memq this
'(Y YY BYY
))
1223 (string-match "\\` *[0-9][0-9]" math-pd-str
)
1224 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str
))
1225 (string-match "\\`[-+]?[0-9]+" math-pd-str
))
1226 (setq year
(math-match-substring math-pd-str
0)
1227 bigyear
(or (eq this
'YYY
)
1228 (memq (aref math-pd-str
0) '(?\
+ ?\-
)))
1229 math-pd-str
(substring math-pd-str
(match-end 0))
1230 year
(math-read-number year
))
1231 (if (and (eq this
'ZYYY
) (eq year
0))
1232 (setq year
(math-sub year
1)
1236 (if (string-match "\\`[-+]?[0-9]+" math-pd-str
)
1237 (setq isoyear
(string-to-number (math-match-substring math-pd-str
0))
1238 math-pd-str
(substring math-pd-str
(match-end 0)))))
1240 (if (string-match "W\\([0-9][0-9]\\)" math-pd-str
)
1241 (setq isoweek
(string-to-number (math-match-substring math-pd-str
1))
1242 math-pd-str
(substring math-pd-str
3))))
1246 (if (eq (aref math-pd-str
0) ?T
)
1247 (setq math-pd-str
(substring math-pd-str
1))
1249 ((memq this
'(aa AA aaaa AAAA
))
1250 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str
)
1251 (setq math-pd-str
(substring math-pd-str
(match-end 0)))))
1252 ((memq this
'(aaa AAA
))
1253 (if (string-match "\\` *ad *" math-pd-str
)
1254 (setq math-pd-str
(substring math-pd-str
(match-end 0)))))
1255 ((memq this
'(bb BB bbb BBB bbbb BBBB
))
1256 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str
)
1257 (setq math-pd-str
(substring math-pd-str
(match-end 0))
1259 ((memq this
'(s ss bs SS BS
))
1260 (and (if (memq next
'(YY YYYY MM DD hh HH mm
))
1261 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str
)
1262 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str
))
1263 (setq second
(math-match-substring math-pd-str
0)
1264 math-pd-str
(substring math-pd-str
(match-end 0))
1265 second
(math-read-number second
))))
1267 (if (string-match "\\`:[0-9][0-9]" math-pd-str
)
1268 (setq math-pd-str
(substring math-pd-str
1))
1270 ((or (not (if (and (memq this
'(ddd MM DD hh HH mm
))
1271 (memq next
'(YY YYYY MM DD ddd
1274 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str
)
1275 (string-match "\\` *[0-9][0-9]" math-pd-str
))
1276 (string-match "\\` *[0-9]+" math-pd-str
)))
1277 (and (setq num
(string-to-number
1278 (math-match-substring math-pd-str
0))
1279 math-pd-str
(substring math-pd-str
(match-end 0)))
1283 (and (>= num
0) (< num
7)))
1285 (setq isoweekday num
))
1286 ((memq this
'(d ddd bdd
))
1288 ((memq this
'(M MM BM
))
1290 ((memq this
'(D DD BD
))
1292 ((memq this
'(h hh bh H HH BH
))
1294 ((memq this
'(m mm bm
))
1300 (setq month
1 day
1)))
1301 (if (and okay
(equal math-pd-str
""))
1303 (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second
)
1304 (and month day
(or (not (or hour minute second
))
1307 (or year
(setq year
(math-this-year)))
1308 (or second
(setq second
0))
1310 (setq year
(math-neg (math-abs year
))))
1311 (setq day
(math-parse-date-validate year bigyear month day
1312 hour minute second
))
1314 (setq day
(math-add day
(1- yearday
))))
1317 (defun math-parse-iso-date (math-pd-str)
1318 "Parse MATH-PD-STR as an ISO week date, or return nil."
1319 (let ((case-fold-search t
)
1320 (isoyear nil
) (isoweek nil
) (isoweekday nil
)
1321 (hour nil
) (minute nil
) (second nil
))
1322 ;; Extract the time, if any.
1323 (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
)
1325 (setq hour
(string-to-number (math-match-substring math-pd-str
1))
1326 minute
(math-match-substring math-pd-str
2)
1327 second
(math-match-substring math-pd-str
3)
1328 math-pd-str
(substring math-pd-str
0 (match-beginning 0)))
1329 (if (equal minute
"")
1331 (setq minute
(string-to-number minute
)))
1332 (if (equal second
"")
1334 (setq second
(math-read-number second
)))))
1335 ;; Next, the year, week and weekday
1336 (if (string-match "\\(-?[0-9]*\\)[^0-9]*W\\([0-9][0-9]\\)[^0-9]*\\([0-9]\\)[^0-9]*\\'" math-pd-str
)
1338 (setq isoyear
(string-to-number (math-match-substring math-pd-str
1))
1339 isoweek
(string-to-number (math-match-substring math-pd-str
2))
1340 isoweekday
(string-to-number (math-match-substring math-pd-str
3)))
1341 (math-parse-iso-date-validate isoyear isoweek isoweekday hour minute second
)))))
1343 (defun calcFunc-now (&optional zone
)
1344 (let ((date (let ((calc-date-format nil
))
1345 (math-parse-date (current-time-string)))))
1348 (math-add date
(math-div (math-sub (calcFunc-tzone nil date
)
1349 (calcFunc-tzone zone date
))
1352 (calc-record-why "*Unable to interpret current date from system")
1353 (append (list 'calcFunc-now
) (and zone
(list zone
))))))
1355 (defun calcFunc-year (date)
1356 (car (math-date-to-dt date
)))
1358 (defun calcFunc-month (date)
1359 (nth 1 (math-date-to-dt date
)))
1361 (defun calcFunc-day (date)
1362 (nth 2 (math-date-to-dt date
)))
1364 (defun calcFunc-weekday (date)
1365 (if (eq (car-safe date
) 'date
)
1366 (setq date
(nth 1 date
)))
1367 (or (math-realp date
)
1368 (math-reject-arg date
'datep
))
1369 (math-mod (math-floor date
) 7))
1371 (defun calcFunc-yearday (date)
1372 (let ((dt (math-date-to-dt date
)))
1373 (math-day-number (car dt
) (nth 1 dt
) (nth 2 dt
))))
1375 (defun calcFunc-hour (date)
1376 (if (eq (car-safe date
) 'hms
)
1378 (or (nth 3 (math-date-to-dt date
)) 0)))
1380 (defun calcFunc-minute (date)
1381 (if (eq (car-safe date
) 'hms
)
1383 (or (nth 4 (math-date-to-dt date
)) 0)))
1385 (defun calcFunc-second (date)
1386 (if (eq (car-safe date
) 'hms
)
1388 (or (nth 5 (math-date-to-dt date
)) 0)))
1390 (defun calcFunc-time (date)
1391 (let ((dt (math-date-to-dt date
)))
1393 (cons 'hms
(nthcdr 3 dt
))
1394 (list 'hms
0 0 0))))
1396 (defun calcFunc-date (date &optional month day hour minute second
)
1397 (and (math-messy-integerp month
) (setq month
(math-trunc month
)))
1398 (and month
(not (integerp month
)) (math-reject-arg month
'fixnump
))
1399 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1400 (and day
(not (integerp day
)) (math-reject-arg day
'fixnump
))
1401 (if (and (eq (car-safe hour
) 'hms
) (not minute
))
1402 (setq second
(nth 3 hour
)
1405 (and (math-messy-integerp hour
) (setq hour
(math-trunc hour
)))
1406 (and hour
(not (integerp hour
)) (math-reject-arg hour
'fixnump
))
1407 (and (math-messy-integerp minute
) (setq minute
(math-trunc minute
)))
1408 (and minute
(not (integerp minute
)) (math-reject-arg minute
'fixnump
))
1409 (and (math-messy-integerp second
) (setq second
(math-trunc second
)))
1410 (and second
(not (math-realp second
)) (math-reject-arg second
'realp
))
1413 (and (math-messy-integerp date
) (setq date
(math-trunc date
)))
1414 (and date
(not (math-integerp date
)) (math-reject-arg date
'integerp
))
1417 (list 'date
(math-dt-to-date (list date month day hour
1420 (list 'date
(math-dt-to-date (list date month day
))))
1421 (list 'date
(math-dt-to-date (list (math-this-year) date month
)))))
1422 (if (math-realp date
)
1424 (if (eq (car date
) 'date
)
1426 (math-reject-arg date
'datep
)))))
1428 (defun calcFunc-julian (date &optional zone
)
1429 (if (math-realp date
)
1430 (list 'date
(if (math-integerp date
)
1431 (math-sub date math-julian-date-beginning-int
)
1432 (setq date
(math-sub date math-julian-date-beginning
))
1433 (math-sub date
(math-div (calcFunc-tzone zone date
)
1435 (if (eq (car date
) 'date
)
1436 (math-add (nth 1 date
) (if (math-integerp (nth 1 date
))
1437 math-julian-date-beginning-int
1438 (math-add math-julian-date-beginning
1439 (math-div (calcFunc-tzone zone date
)
1441 (math-reject-arg date
'datep
))))
1443 (defun calcFunc-unixtime (date &optional zone
)
1444 (if (math-realp date
)
1446 (setq date
(math-add 719163 (math-div date
'(float 864 2))))
1447 (list 'date
(math-sub date
(math-div (calcFunc-tzone zone date
)
1449 (if (eq (car date
) 'date
)
1450 (math-add (nth 1 (math-date-parts (nth 1 date
) 719163))
1451 (calcFunc-tzone zone date
))
1452 (math-reject-arg date
'datep
))))
1455 ;;; Note: Longer names must appear before shorter names which are
1456 ;;; substrings of them.
1457 (defvar math-tzone-names
1459 ( "MEGT" -
1 "MET" "METDST" ) ; Middle Europe
1460 ( "METDST" -
1 -
1 ) ( "MET" -
1 0 )
1461 ( "MEGZ" -
1 "MEZ" "MESZ" ) ( "MEZ" -
1 0 ) ( "MESZ" -
1 -
1 )
1462 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1463 ( "WETDST" 0 -
1 ) ( "WET" 0 0 )
1464 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -
1 ) ; Britain
1465 ( "NGT" (float 35 -
1) "NST" "NDT" ) ; Newfoundland
1466 ( "NST" (float 35 -
1) 0 ) ( "NDT" (float 35 -
1) -
1 )
1467 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -
1 ) ; Atlantic
1468 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -
1 ) ; Eastern
1469 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -
1 ) ; Central
1470 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -
1 ) ; Mountain
1471 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -
1 ) ; Pacific
1472 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -
1 ) ; Yukon
1474 "No doc yet. See calc manual for now. ")
1476 (defvar var-TimeZone nil
)
1479 (defvar calendar-current-time-zone-cache
)
1481 (defvar math-calendar-tzinfo
1483 "Information about the timezone, retrieved from the calendar.")
1485 (defun math-get-calendar-tzinfo ()
1486 "Get information about the timezone from the calendar.
1487 The result should be a list of two items about the current time zone:
1488 first, the number of seconds difference from GMT
1489 second, the number of seconds offset for daylight savings."
1490 (if math-calendar-tzinfo
1491 math-calendar-tzinfo
1493 (let ((tzinfo (progn
1494 (calendar-current-time-zone)
1495 calendar-current-time-zone-cache
)))
1496 (setq math-calendar-tzinfo
1497 (list (* 60 (abs (nth 0 tzinfo
)))
1498 (* 60 (nth 1 tzinfo
)))))))
1500 (defun calcFunc-tzone (&optional zone date
)
1502 (cond ((math-realp zone
)
1503 (math-round (math-mul zone
3600)))
1504 ((eq (car zone
) 'hms
)
1505 (math-round (math-mul (math-from-hms zone
'deg
) 3600)))
1507 (math-add (calcFunc-tzone (nth 1 zone
) date
)
1508 (calcFunc-tzone (nth 2 zone
) date
)))
1510 (math-sub (calcFunc-tzone (nth 1 zone
) date
)
1511 (calcFunc-tzone (nth 2 zone
) date
)))
1512 ((eq (car zone
) 'var
)
1513 (let ((name (upcase (symbol-name (nth 1 zone
))))
1515 (if (setq found
(assoc name math-tzone-names
))
1516 (calcFunc-tzone (math-add (nth 1 found
)
1517 (if (integerp (nth 2 found
))
1520 (math-daylight-savings-adjust
1524 (if (equal name
"LOCAL")
1525 (calcFunc-tzone nil date
)
1526 (math-reject-arg zone
"*Unrecognized time zone name")))))
1527 (t (math-reject-arg zone
"*Expected a time zone")))
1528 (if (calc-var-value 'var-TimeZone
)
1529 (calcFunc-tzone (calc-var-value 'var-TimeZone
) date
)
1530 (let ((tzinfo (math-get-calendar-tzinfo)))
1532 (* (math-cal-daylight-savings-adjust date
) (nth 1 tzinfo
)))))))
1534 (defvar math-daylight-savings-hook
'math-std-daylight-savings
)
1536 (defun math-daylight-savings-adjust (date zone
&optional dt
)
1537 (or date
(setq date
(nth 1 (calcFunc-now))))
1539 (if (eq (car-safe date
) 'date
)
1542 (if (and date
(math-realp date
))
1543 (let ((zadj (assoc zone math-tzone-names
)))
1544 (if zadj
(setq bump -
1
1545 date
(math-sub date
(math-div (nth 1 zadj
)
1547 (math-reject-arg date
'datep
)))
1548 (setq date
(math-float date
))
1549 (or dt
(setq dt
(math-date-to-dt date
)))
1550 (and math-daylight-savings-hook
1551 (funcall math-daylight-savings-hook date dt zone bump
))))
1553 ;;; Based on part of dst-adjust-time in cal-dst.el
1554 ;;; For calcFunc-dst, when zone=nil
1555 (defun math-cal-daylight-savings-adjust (date)
1556 "Return -1 if DATE is using daylight saving, 0 otherwise."
1558 (unless date
(setq date
(calcFunc-now)))
1559 (let* ((dt (math-date-to-dt date
))
1564 (+ (nth 3 dt
) (/ (nth 4 dt
) 60.0)))
1569 (calendar-absolute-from-gregorian
1570 (list (nth 1 dt
) (nth 2 dt
) (nth 0 dt
)))
1571 (/ (round (* 60 time
)) 60.0 24.0))))
1572 (if (dst-in-effect rounded-abs-date
)
1576 (defun calcFunc-dsadj (date &optional zone
)
1578 (or (eq (car-safe zone
) 'var
)
1579 (math-reject-arg zone
"*Time zone variable expected"))
1580 (setq zone
(calc-var-value 'var-TimeZone
)))
1583 (setq zone
(and (eq (car-safe zone
) 'var
)
1584 (upcase (symbol-name (nth 1 zone
)))))
1585 (let ((zadj (assoc zone math-tzone-names
)))
1586 (or zadj
(math-reject-arg zone
"*Unrecognized time zone name"))
1587 (if (integerp (nth 2 zadj
))
1589 (math-daylight-savings-adjust date zone
))))
1590 (math-cal-daylight-savings-adjust date
)))
1592 ;; (defun calcFunc-dsadj (date &optional zone)
1594 ;; (or (eq (car-safe zone) 'var)
1595 ;; (math-reject-arg zone "*Time zone variable expected"))
1596 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1599 ;; (calc-var-value 'var-TimeZone)))))
1600 ;; (setq zone (and (eq (car-safe zone) 'var)
1601 ;; (upcase (symbol-name (nth 1 zone)))))
1602 ;; (let ((zadj (assoc zone math-tzone-names)))
1603 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1604 ;; (if (integerp (nth 2 zadj))
1606 ;; (math-daylight-savings-adjust date zone))))
1608 (defun calcFunc-tzconv (date z1 z2
)
1609 (if (math-realp date
)
1610 (nth 1 (calcFunc-tzconv (list 'date date
) z1 z2
))
1611 (calcFunc-unixtime (calcFunc-unixtime date z1
) z2
)))
1613 (defun math-std-daylight-savings (date dt zone bump
)
1614 "Standard North American daylight saving algorithm.
1615 Before 2007, this uses `math-std-daylight-savings-old', where
1616 daylight saving began on the first Sunday of April at 2 a.m.,
1617 and ended on the last Sunday of October at 2 a.m.
1618 As of 2007, this uses `math-std-daylight-savings-new', where
1619 daylight saving begins on the second Sunday of March at 2 a.m.,
1620 and ends on the first Sunday of November at 2 a.m."
1621 (if (< (car dt
) 2007)
1622 (math-std-daylight-savings-old date dt zone bump
)
1623 (math-std-daylight-savings-new date dt zone bump
)))
1625 (defun math-std-daylight-savings-new (date dt zone bump
)
1626 "Standard North American daylight saving algorithm as of 2007.
1627 This implements the rules for the U.S. and Canada.
1628 Daylight saving begins on the second Sunday of March at 2 a.m.,
1629 and ends on the first Sunday of November at 2 a.m."
1630 (cond ((< (nth 1 dt
) 3) 0)
1632 (let ((sunday (math-prev-weekday-in-month date dt
14 0)))
1633 (cond ((< (nth 2 dt
) sunday
) 0)
1634 ((= (nth 2 dt
) sunday
)
1635 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1637 ((< (nth 1 dt
) 11) -
1)
1639 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1640 (cond ((< (nth 2 dt
) sunday
) -
1)
1641 ((= (nth 2 dt
) sunday
)
1642 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1646 (defun math-std-daylight-savings-old (date dt zone bump
)
1647 "Standard North American daylight saving algorithm before 2007.
1648 This implements the rules for the U.S. and Canada.
1649 Daylight saving begins on the first Sunday of April at 2 a.m.,
1650 and ends on the last Sunday of October at 2 a.m."
1651 (cond ((< (nth 1 dt
) 4) 0)
1653 (let ((sunday (math-prev-weekday-in-month date dt
7 0)))
1654 (cond ((< (nth 2 dt
) sunday
) 0)
1655 ((= (nth 2 dt
) sunday
)
1656 (if (>= (nth 3 dt
) (+ 3 bump
)) -
1 0))
1658 ((< (nth 1 dt
) 10) -
1)
1660 (let ((sunday (math-prev-weekday-in-month date dt
31 0)))
1661 (cond ((< (nth 2 dt
) sunday
) -
1)
1662 ((= (nth 2 dt
) sunday
)
1663 (if (>= (nth 3 dt
) (+ 2 bump
)) 0 -
1))
1667 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1668 ;;; day of the given month.
1669 (defun math-prev-weekday-in-month (date dt day wday
)
1670 (or day
(setq day
(nth 2 dt
)))
1671 (if (> day
(math-days-in-month (car dt
) (nth 1 dt
)))
1672 (setq day
(math-days-in-month (car dt
) (nth 1 dt
))))
1673 (let ((zeroth (math-sub (math-floor date
) (nth 2 dt
))))
1674 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day
))) zeroth
)))
1676 (defun calcFunc-pwday (date &optional day weekday
)
1677 (if (eq (car-safe date
) 'date
)
1678 (setq date
(nth 1 date
)))
1679 (or (math-realp date
)
1680 (math-reject-arg date
'datep
))
1681 (if (math-messy-integerp day
) (setq day
(math-trunc day
)))
1682 (or (integerp day
) (math-reject-arg day
'fixnump
))
1683 (if (= day
0) (setq day
31))
1684 (and (or (< day
7) (> day
31)) (math-reject-arg day
'range
))
1685 (math-prev-weekday-in-month date
(math-date-to-dt date
) day
(or weekday
0)))
1688 (defun calcFunc-newweek (date &optional weekday
)
1689 (if (eq (car-safe date
) 'date
)
1690 (setq date
(nth 1 date
)))
1691 (or (math-realp date
)
1692 (math-reject-arg date
'datep
))
1693 (or weekday
(setq weekday
0))
1694 (and (math-messy-integerp weekday
) (setq weekday
(math-trunc weekday
)))
1695 (or (integerp weekday
) (math-reject-arg weekday
'fixnump
))
1696 (and (or (< weekday
0) (> weekday
6)) (math-reject-arg weekday
'range
))
1697 (setq date
(math-floor date
))
1698 (list 'date
(math-sub date
(calcFunc-weekday (math-sub date weekday
)))))
1700 (defun calcFunc-newmonth (date &optional day
)
1701 (or day
(setq day
1))
1702 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1703 (or (integerp day
) (math-reject-arg day
'fixnump
))
1704 (and (or (< day
0) (> day
31)) (math-reject-arg day
'range
))
1705 (let* ((dt (math-date-to-dt date
))
1706 (dim (math-days-in-month (car dt
) (nth 1 dt
)))
1707 (julian (if calc-gregorian-switch
1708 (math-date-to-dt (math-sub
1709 (or (nth 3 calc-gregorian-switch
)
1710 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))
1712 (if (or (= day
0) (> day dim
))
1714 (setq day
(1- day
)))
1715 ;; Adjust if this occurs near the switch to the Gregorian calendar
1716 (if calc-gregorian-switch
1718 ((and (math-dt-before-p (list (car dt
) (nth 1 dt
) 1) calc-gregorian-switch
)
1719 (math-dt-before-p julian
(list (car dt
) (nth 1 dt
) 1)))
1720 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the month
1722 (math-dt-to-date (list (car calc-gregorian-switch
)
1723 (nth 1 calc-gregorian-switch
)
1724 (if (> (+ (nth 2 calc-gregorian-switch
) day
) dim
)
1726 (+ (nth 2 calc-gregorian-switch
) day
))))))
1727 ((and (eq (car dt
) (car calc-gregorian-switch
))
1728 (= (nth 1 dt
) (nth 1 calc-gregorian-switch
)))
1729 ;; In this case, the switch to the Gregorian calendar occurs in the given month
1730 (if (< (+ (nth 2 julian
) day
) (nth 2 calc-gregorian-switch
))
1731 ;; If the DAYth day occurs before the switch, use it
1732 (list 'date
(math-dt-to-date (list (car dt
) (nth 1 dt
) (1+ day
))))
1733 ;; Otherwise do some computations
1734 (let ((tm (+ day
(- (nth 2 calc-gregorian-switch
) (nth 2 julian
)))))
1735 (list 'date
(math-dt-to-date
1739 (if (> tm dim
) dim tm
)))))))
1740 ((and (eq (car dt
) (car julian
))
1741 (= (nth 1 dt
) (nth 1 julian
)))
1742 ;; In this case, the current month is truncated because of the switch
1743 ;; to the Gregorian calendar
1744 (list 'date
(math-dt-to-date
1747 (if (>= day
(nth 2 julian
))
1752 (list 'date
(math-add (math-dt-to-date (list (car dt
) (nth 1 dt
) 1)) day
))))
1753 (list 'date
(math-add (math-dt-to-date (list (car dt
) (nth 1 dt
) 1)) day
)))))
1755 (defun calcFunc-newyear (date &optional day
)
1756 (if (eq (car-safe date
) 'date
) (setq date
(nth 1 date
)))
1757 (or day
(setq day
1))
1758 (and (math-messy-integerp day
) (setq day
(math-trunc day
)))
1759 (or (integerp day
) (math-reject-arg day
'fixnump
))
1760 (let* ((dt (math-date-to-dt date
))
1761 (gregbeg (if calc-gregorian-switch
1762 (or (nth 3 calc-gregorian-switch
)
1763 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch
))))
1764 (julianend (if calc-gregorian-switch
(math-sub gregbeg
1)))
1765 (julian (if calc-gregorian-switch
1766 (math-date-to-dt julianend
))))
1767 (if (and (>= day
0) (<= day
366))
1768 (let ((max (if (math-leap-year-p (car dt
)) 366 365)))
1769 (if (or (= day
0) (> day max
)) (setq day max
))
1770 (if calc-gregorian-switch
1771 ;; Now to break this down into cases
1773 ((and (math-dt-before-p (list (car dt
) 1 1) calc-gregorian-switch
)
1774 (math-dt-before-p julian
(list (car dt
) 1 1)))
1775 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the year
1776 (list 'date
(math-min (math-add gregbeg
(1- day
))
1777 (math-dt-to-date (list (car calc-gregorian-switch
) 12 31)))))
1778 ((eq (car dt
) (car julian
))
1779 ;; In this case, the switch to the Gregorian calendar occurs in the given year
1780 (if (Math-lessp (car julian
) (car calc-gregorian-switch
))
1781 ;; Here, the last Julian day is the last day of the year.
1782 (list 'date
(math-min (math-add (math-dt-to-date (list (car dt
) 1 1)) (1- day
))
1784 ;; Otherwise, just make sure the date doesn't go past the end of the year
1785 (list 'date
(math-min (math-add (math-dt-to-date (list (car dt
) 1 1)) (1- day
))
1786 (math-dt-to-date (list (car dt
) 12 31))))))
1788 (list 'date
(math-add (math-dt-to-date (list (car dt
) 1 1))
1790 (list 'date
(math-add (math-dt-to-date (list (car dt
) 1 1))
1792 (if (and (>= day -
12) (<= day -
1))
1793 (if (and calc-gregorian-switch
1794 (math-dt-before-p (list (car dt
) (- day
) 1) calc-gregorian-switch
)
1795 (math-dt-before-p julian
(list (car dt
) (- day
) 1)))
1796 (list 'date gregbeg
)
1797 (list 'date
(math-dt-to-date (list (car dt
) (- day
) 1))))
1798 (math-reject-arg day
'range
)))))
1800 (defun calcFunc-incmonth (date &optional step
)
1801 (or step
(setq step
1))
1802 (and (math-messy-integerp step
) (setq step
(math-trunc step
)))
1803 (or (math-integerp step
) (math-reject-arg step
'integerp
))
1804 (let* ((dt (math-date-to-dt date
))
1806 (month (math-add (1- (nth 1 dt
)) step
))
1807 (extra (calcFunc-idiv month
12))
1809 (setq month
(1+ (math-sub month
(math-mul extra
12)))
1810 year
(math-add year extra
)
1811 day
(min day
(math-days-in-month year month
)))
1812 (and (math-posp (car dt
)) (not (math-posp year
))
1813 (setq year
(math-sub year
1))) ; did we go past the year zero?
1814 (and (math-negp (car dt
)) (not (math-negp year
))
1815 (setq year
(math-add year
1)))
1816 (list 'date
(math-dt-to-date
1817 (cons year
(cons month
(cons day
(cdr (cdr (cdr dt
))))))))))
1819 (defun calcFunc-incyear (date &optional step
)
1820 (calcFunc-incmonth date
(math-mul (or step
1) 12)))
1824 (defun calcFunc-bsub (a b
)
1825 (or (eq (car-safe a
) 'date
)
1826 (math-reject-arg a
'datep
))
1827 (if (eq (car-safe b
) 'date
)
1828 (if (math-lessp (nth 1 a
) (nth 1 b
))
1829 (math-neg (calcFunc-bsub b a
))
1830 (math-setup-holidays b
)
1831 (let* ((da (math-to-business-day a
))
1832 (db (math-to-business-day b
)))
1833 (math-add (math-sub (car da
) (car db
))
1834 (if (and (cdr db
) (not (cdr da
))) 1 0))))
1835 (calcFunc-badd a
(math-neg b
))))
1837 (defvar math-holidays-cache nil
)
1838 (defvar math-holidays-cache-tag t
)
1839 (defun calcFunc-badd (a b
)
1840 (if (eq (car-safe b
) 'date
)
1841 (if (eq (car-safe a
) 'date
)
1842 (math-reject-arg nil
"*Invalid combination in date arithmetic")
1843 (calcFunc-badd b a
))
1844 (if (eq (car-safe a
) 'date
)
1848 (let* ((d (math-to-business-day a
))
1849 (bb (math-add (car d
)
1850 (if (and (cdr d
) (Math-posp b
))
1851 (math-sub b
1) b
))))
1852 (or (math-from-business-day bb
)
1853 (calcFunc-badd a b
))))
1854 (if (eq (car-safe b
) 'hms
)
1855 (let ((hours (nth 7 math-holidays-cache
)))
1856 (setq b
(math-div (math-from-hms b
'deg
) 24))
1858 (setq b
(math-div b
(cdr hours
))))
1859 (calcFunc-badd a b
))
1860 (math-reject-arg nil
"*Invalid combination in date arithmetic")))
1861 (math-reject-arg a
'datep
))))
1863 (defun calcFunc-holiday (a)
1864 (if (cdr (math-to-business-day a
)) 1 0))
1866 ;;; Compute the number of business days since Jan 1, 1 AD.
1868 (defun math-to-business-day (date &optional need-year
)
1869 (if (eq (car-safe date
) 'date
)
1870 (setq date
(nth 1 date
)))
1871 (or (Math-realp date
)
1872 (math-reject-arg date
'datep
))
1873 (let* ((day (math-floor date
))
1874 (time (math-sub date day
))
1875 (dt (math-date-to-dt day
))
1878 (or (not need-year
) (eq (car dt
) need-year
)
1879 (math-reject-arg (list 'date day
) "*Generated holiday has wrong year"))
1880 (math-setup-holidays date
)
1881 (let ((days (car math-holidays-cache
)))
1882 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1883 (setq delta
(1+ delta
)))
1884 (and days
(= day
(car days
))
1886 (let* ((weekdays (nth 3 math-holidays-cache
))
1887 (weeks (1- (/ (+ day
6) 7)))
1888 (wkday (- day
1 (* weeks
7))))
1889 (setq delta
(+ delta
(* weeks
(length weekdays
))))
1890 (while (and weekdays
(< (car weekdays
) wkday
))
1891 (setq weekdays
(cdr weekdays
)
1893 (and weekdays
(eq wkday
(car weekdays
))
1895 (let ((hours (nth 7 math-holidays-cache
)))
1898 (setq time
(math-div (math-sub time
(car hours
)) (cdr hours
)))
1899 (if (Math-lessp time
0) (setq time
0))
1900 (or (Math-lessp time
1)
1903 (math-div 1 (math-mul 86400 (cdr hours
)))))))))
1904 (cons (math-add (math-sub day delta
) time
) holiday
)))
1907 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1908 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1910 (defun math-from-business-day (num)
1911 (let* ((day (math-floor num
))
1912 (time (math-sub num day
)))
1914 (math-reject-arg nil
"*Date is outside valid range"))
1915 (math-setup-holidays)
1916 (let ((days (nth 1 math-holidays-cache
))
1918 (while (and (setq days
(cdr days
)) (< (car days
) day
))
1919 (setq delta
(1+ delta
)))
1920 (setq day
(+ day delta
)))
1921 (let* ((weekdays (nth 3 math-holidays-cache
))
1922 (bweek (- 7 (length weekdays
)))
1923 (weeks (1- (/ (+ day
(1- bweek
)) bweek
)))
1924 (wkday (- day
1 (* weeks bweek
)))
1926 (setq day
(+ day
(* weeks
(length weekdays
))))
1927 (while (if (memq w weekdays
)
1929 (> (setq wkday
(1- wkday
)) 0))
1931 (let ((hours (nth 7 math-holidays-cache
)))
1933 (setq time
(math-add (math-mul time
(cdr hours
)) (car hours
)))))
1934 (and (not (math-setup-holidays day
))
1935 (list 'date
(math-add day time
))))))
1937 ;; The variable math-sh-year is local to math-setup-holidays
1938 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1939 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1940 (defvar math-sh-year
)
1942 (defun math-setup-holidays (&optional date
)
1943 (or (eq (calc-var-value 'var-Holidays
) math-holidays-cache-tag
)
1944 (let ((h (calc-var-value 'var-Holidays
))
1945 (wdnames '( (sun .
0) (mon .
1) (tue .
2) (wed .
3)
1946 (thu .
4) (fri .
5) (sat .
6) ))
1947 (days nil
) (weekdays nil
) (exprs nil
) (limit nil
) (hours nil
))
1948 (or (math-vectorp h
)
1949 (math-reject-arg h
"*Holidays variable must be a vector"))
1950 (while (setq h
(cdr h
))
1951 (cond ((or (and (eq (car-safe (car h
)) 'date
)
1952 (integerp (nth 1 (car h
))))
1953 (and (eq (car-safe (car h
)) 'intv
)
1954 (eq (car-safe (nth 2 (car h
))) 'date
))
1955 (eq (car-safe (car h
)) 'vec
))
1956 (setq days
(cons (car h
) days
)))
1957 ((and (eq (car-safe (car h
)) 'var
)
1958 (assq (nth 1 (car h
)) wdnames
))
1959 (setq weekdays
(cons (cdr (assq (nth 1 (car h
)) wdnames
))
1961 ((and (eq (car-safe (car h
)) 'intv
)
1962 (eq (car-safe (nth 2 (car h
))) 'hms
)
1963 (eq (car-safe (nth 3 (car h
))) 'hms
))
1966 (car h
) "*Only one hours interval allowed in Holidays"))
1967 (setq hours
(math-div (car h
) '(hms 24 0 0)))
1968 (if (or (Math-lessp (nth 2 hours
) 0)
1969 (Math-lessp 1 (nth 3 hours
)))
1971 (car h
) "*Hours interval out of range"))
1972 (setq hours
(cons (nth 2 hours
)
1973 (math-sub (nth 3 hours
) (nth 2 hours
))))
1974 (if (Math-zerop (cdr hours
))
1976 (car h
) "*Degenerate hours interval")))
1977 ((or (and (eq (car-safe (car h
)) 'intv
)
1978 (Math-integerp (nth 2 (car h
)))
1979 (Math-integerp (nth 3 (car h
))))
1980 (and (integerp (car h
))
1981 (> (car h
) 1900) (< (car h
) 2100)))
1984 (car h
) "*Only one limit allowed in Holidays"))
1985 (setq limit
(calcFunc-vint (car h
) '(intv 3 1 2737)))
1986 (if (equal limit
'(vec))
1987 (math-reject-arg (car h
) "*Limit is out of range")))
1988 ((or (math-expr-contains (car h
) '(var y var-y
))
1989 (math-expr-contains (car h
) '(var m var-m
)))
1990 (setq exprs
(cons (car h
) exprs
)))
1992 (car h
) "*Holidays must contain a vector of holidays"))))
1993 (if (= (length weekdays
) 7)
1994 (math-reject-arg nil
"*Too many weekend days"))
1995 (setq math-holidays-cache
(list (list -
1) ; 0: days list
1996 (list -
1) ; 1: inverse-days list
1999 (or limit
'(intv 3 1 2737))
2000 nil
; 5: (lo.hi) expanded years
2002 hours
) ; 7: business hours
2003 math-holidays-cache-tag
(calc-var-value 'var-Holidays
))))
2005 (let ((year (calcFunc-year date
))
2006 (limits (nth 5 math-holidays-cache
))
2008 (or (eq (calcFunc-in year
(nth 4 math-holidays-cache
)) 1)
2010 (or (eq (car-safe date
) 'date
) (setq date
(list 'date date
)))
2011 (math-reject-arg date
"*Date is outside valid range")))
2013 (let ((days (nth 6 math-holidays-cache
)))
2015 (let ((math-sh-year nil
)) ; see below
2016 (setcar (nthcdr 6 math-holidays-cache
) nil
)
2017 (math-setup-add-holidays (cons 'vec
(cdr days
)))
2018 (setcar (nthcdr 2 math-holidays-cache
) (car days
))))
2019 (cond ((not (nth 2 math-holidays-cache
))
2023 (setcar (nthcdr 5 math-holidays-cache
) (cons year year
))
2024 (math-setup-year-holidays year
)
2026 ((< year
(car limits
))
2027 (message "Computing holidays, %d .. %d"
2028 year
(1- (car limits
)))
2029 (calc-set-command-flag 'clear-message
)
2030 (while (< year
(car limits
))
2031 (setcar limits
(1- (car limits
)))
2032 (math-setup-year-holidays (car limits
)))
2034 ((> year
(cdr limits
))
2035 (message "Computing holidays, %d .. %d"
2036 (1+ (cdr limits
)) year
)
2037 (calc-set-command-flag 'clear-message
)
2038 (while (> year
(cdr limits
))
2039 (setcdr limits
(1+ (cdr limits
)))
2040 (math-setup-year-holidays (cdr limits
)))
2045 (or done
(setq math-holidays-cache-tag t
))))))
2047 (defun math-setup-year-holidays (math-sh-year)
2048 (let ((exprs (nth 2 math-holidays-cache
)))
2050 (let* ((var-y math-sh-year
)
2052 (expr (math-evaluate-expr (car exprs
))))
2053 (if (math-expr-contains expr
'(var m var-m
))
2055 (while (<= (setq var-m
(1+ var-m
)) 12)
2056 (math-setup-add-holidays (math-evaluate-expr expr
))))
2057 (math-setup-add-holidays expr
)))
2058 (setq exprs
(cdr exprs
)))))
2060 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
2061 (cond ((eq (car-safe days
) 'vec
)
2062 (while (setq days
(cdr days
))
2063 (math-setup-add-holidays (car days
))))
2064 ((eq (car-safe days
) 'intv
)
2065 (let ((day (math-ceiling (nth 2 days
))))
2066 (or (eq (calcFunc-in day days
) 1)
2067 (setq day
(math-add day
1)))
2068 (while (eq (calcFunc-in day days
) 1)
2069 (math-setup-add-holidays day
)
2070 (setq day
(math-add day
1)))))
2071 ((eq (car-safe days
) 'date
)
2072 (math-setup-add-holidays (nth 1 days
)))
2075 (let ((b (math-to-business-day days math-sh-year
)))
2076 (or (cdr b
) ; don't register holidays twice!
2077 (let ((prev (car math-holidays-cache
))
2078 (iprev (nth 1 math-holidays-cache
)))
2079 (while (and (cdr prev
) (< (nth 1 prev
) days
))
2080 (setq prev
(cdr prev
) iprev
(cdr iprev
)))
2081 (setcdr prev
(cons days
(cdr prev
)))
2082 (setcdr iprev
(cons (car b
) (cdr iprev
)))
2083 (while (setq iprev
(cdr iprev
))
2084 (setcar iprev
(1- (car iprev
))))))))
2086 (math-reject-arg (list 'date days
) "*Invalid holiday value"))
2088 (math-reject-arg days
"*Holiday formula failed to evaluate"))))
2095 ;;; Build a standard deviation form. [X X X]
2096 (defun math-make-sdev (x sigma
)
2097 (if (memq (car-safe x
) '(date mod sdev intv vec
))
2098 (math-reject-arg x
'realp
))
2099 (if (memq (car-safe sigma
) '(date mod sdev intv vec
))
2100 (math-reject-arg sigma
'realp
))
2101 (if (or (Math-negp sigma
) (memq (car-safe sigma
) '(cplx polar
)))
2102 (setq sigma
(math-abs sigma
)))
2103 (if (and (Math-zerop sigma
) (Math-scalarp x
))
2105 (list 'sdev x sigma
)))
2106 (defun calcFunc-sdev (x sigma
)
2107 (math-make-sdev x sigma
))
2113 (defun math-normalize-mod (a)
2114 (let ((n (math-normalize (nth 1 a
)))
2115 (m (math-normalize (nth 2 a
))))
2116 (if (and (math-anglep n
) (math-anglep m
) (math-posp m
))
2118 (math-normalize (list 'calcFunc-makemod n m
)))))
2120 ;;; Build a modulo form. [N R R]
2121 (defun math-make-mod (n m
)
2122 (setq calc-previous-modulo m
)
2124 (cond ((not (Math-anglep m
))
2125 (math-reject-arg m
'anglep
))
2126 ((not (math-posp m
))
2127 (math-reject-arg m
'posp
))
2129 (if (or (Math-negp n
)
2130 (not (Math-lessp n m
)))
2131 (list 'mod
(math-mod n m
) m
)
2133 ((memq (car n
) '(+ -
/ vec neg
))
2136 (mapcar (function (lambda (x) (math-make-mod x m
)))
2138 ((and (eq (car n
) '*) (Math-anglep (nth 1 n
)))
2139 (math-mul (math-make-mod (nth 1 n
) m
) (nth 2 n
)))
2140 ((memq (car n
) '(* ^ var calcFunc-subscr
))
2141 (math-mul (math-make-mod 1 m
) n
))
2142 (t (math-reject-arg n
'anglep
)))))
2143 (defun calcFunc-makemod (n m
)
2144 (math-make-mod n m
))
2148 ;;;; Interval forms.
2150 ;;; Build an interval form. [X S X X]
2151 (defun math-make-intv (mask lo hi
)
2152 (if (memq (car-safe lo
) '(cplx polar mod sdev intv vec
))
2153 (math-reject-arg lo
'realp
))
2154 (if (memq (car-safe hi
) '(cplx polar mod sdev intv vec
))
2155 (math-reject-arg hi
'realp
))
2156 (or (eq (eq (car-safe lo
) 'date
) (eq (car-safe hi
) 'date
))
2157 (math-reject-arg (if (eq (car-safe lo
) 'date
) hi lo
) 'datep
))
2158 (if (and (or (Math-realp lo
) (eq (car lo
) 'date
))
2159 (or (Math-realp hi
) (eq (car hi
) 'date
)))
2160 (let ((cmp (math-compare lo hi
)))
2164 (list 'intv mask lo hi
))
2167 (list 'intv
2 lo lo
)
2168 (list 'intv mask lo lo
))
2169 (list 'intv mask lo hi
))))
2170 (list 'intv mask lo hi
)))
2171 (defun calcFunc-intv (mask lo hi
)
2172 (if (math-messy-integerp mask
) (setq mask
(math-trunc mask
)))
2173 (or (natnump mask
) (math-reject-arg mask
'fixnatnump
))
2174 (or (<= mask
3) (math-reject-arg mask
'range
))
2175 (math-make-intv mask lo hi
))
2177 (defun math-sort-intv (mask lo hi
)
2178 (if (Math-lessp hi lo
)
2179 (math-make-intv (aref [0 2 1 3] mask
) hi lo
)
2180 (math-make-intv mask lo hi
)))
2185 (defun math-combine-intervals (a am b bm c cm d dm
)
2187 (if (= (setq res
(math-compare a c
)) 1)
2190 (setq am
(or am cm
))))
2191 (if (= (setq res
(math-compare b d
)) -
1)
2194 (setq bm
(or bm dm
))))
2195 (math-make-intv (+ (if am
2 0) (if bm
1 0)) a b
)))
2198 (defun math-div-mod (a b m
) ; [R R R R] (Returns nil if no solution)
2199 (and (Math-integerp a
) (Math-integerp b
) (Math-integerp m
)
2200 (let ((u1 1) (u3 b
) (v1 0) (v3 m
))
2201 (while (not (eq v3
0)) ; See Knuth sec 4.5.2, exercise 15
2202 (let* ((q (math-idivmod u3 v3
))
2203 (t1 (math-sub u1
(math-mul v1
(car q
)))))
2204 (setq u1 v1 u3 v3 v1 t1 v3
(cdr q
))))
2205 (let ((q (math-idivmod a u3
)))
2207 (math-mod (math-mul (car q
) u1
) m
))))))
2209 (defun math-mod-intv (a b
)
2210 (let* ((q1 (math-floor (math-div (nth 2 a
) b
)))
2211 (q2 (math-floor (math-div (nth 3 a
) b
)))
2212 (m1 (math-sub (nth 2 a
) (math-mul q1 b
)))
2213 (m2 (math-sub (nth 3 a
) (math-mul q2 b
))))
2214 (cond ((equal q1 q2
)
2215 (math-sort-intv (nth 1 a
) m1 m2
))
2216 ((and (math-equal-int (math-sub q2 q1
) 1)
2218 (memq (nth 1 a
) '(0 2)))
2219 (math-make-intv (nth 1 a
) m1 b
))
2221 (math-make-intv 2 0 b
)))))
2223 ;; The variables math-exp-str and math-exp-pos are local to
2224 ;; math-read-exprs in math-aent.el, but are used by
2225 ;; math-read-angle-brackets, which is called (indirectly) by
2227 (defvar math-exp-str
)
2228 (defvar math-exp-pos
)
2230 (defun math-read-angle-brackets ()
2231 (let* ((last (or (math-check-for-commas t
) (length math-exp-str
)))
2232 (str (substring math-exp-str math-exp-pos last
))
2234 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str
)
2235 (let ((str1 (substring str
0 (1- (match-end 0))))
2236 (str2 (substring str
(match-end 0)))
2237 (calc-hashes-used 0))
2238 (setq str1
(math-read-expr (concat "[" str1
"]")))
2239 (if (eq (car-safe str1
) 'error
)
2241 (setq str2
(math-read-expr str2
))
2242 (if (eq (car-safe str2
) 'error
)
2244 (append '(calcFunc-lambda) (cdr str1
) (list str2
)))))
2245 (if (string-match "#" str
)
2246 (let ((calc-hashes-used 0))
2247 (and (setq str
(math-read-expr str
))
2248 (if (eq (car-safe str
) 'error
)
2250 (append '(calcFunc-lambda)
2251 (calc-invent-args calc-hashes-used
)
2253 (math-parse-date str
)))))
2255 (throw 'syntax res
))
2256 (if (eq (car-safe res
) 'error
)
2257 (throw 'syntax
(nth 2 res
)))
2258 (setq math-exp-pos
(1+ last
))
2262 (provide 'calc-forms
)
2264 ;;; calc-forms.el ends here