]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-forms.el
Merge from trunk.
[gnu-emacs] / lisp / calc / calc-forms.el
1 ;;; calc-forms.el --- data format conversion functions for Calc
2
3 ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; This file is autoloaded from calc-ext.el.
28
29 (require 'calc-ext)
30 (require 'calc-macs)
31
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))
36
37
38 (defun calc-time ()
39 (interactive)
40 (calc-wrapper
41 (let ((time (current-time-string)))
42 (calc-enter-result 0 "time"
43 (list 'mod
44 (list 'hms
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))))))
49
50 (defun calc-to-hms (arg)
51 (interactive "P")
52 (calc-wrapper
53 (if (calc-is-inverse)
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))))
58
59 (defun calc-from-hms (arg)
60 (interactive "P")
61 (calc-invert-func)
62 (calc-to-hms arg))
63
64
65 (defun calc-hms-notation (fmt)
66 (interactive "sHours-minutes-seconds format (hms, @ ' \", etc.): ")
67 (calc-wrapper
68 (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt)
69 (progn
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))
76 t)
77 (setq-default calc-hms-format calc-hms-format)) ; for minibuffer
78 (error "Bad hours-minutes-seconds format"))))
79
80 (defun calc-date-notation (fmt arg)
81 (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP")
82 (calc-wrapper
83 (if (string-match-p "\\`\\s-*\\'" fmt)
84 (setq fmt "1"))
85 (if (string-match "\\` *[0-9] *\\'" fmt)
86 (setq fmt (nth (string-to-number fmt) calc-standard-date-formats)))
87 (or (string-match "[a-zA-Z]" fmt)
88 (error "Bad date format specifier"))
89 (and arg
90 (>= (setq arg (prefix-numeric-value arg)) 0)
91 (<= arg 9)
92 (setq calc-standard-date-formats
93 (copy-sequence calc-standard-date-formats))
94 (setcar (nthcdr arg calc-standard-date-formats) fmt))
95 (let ((case-fold-search nil))
96 (and (not (string-match "<.*>" fmt))
97 (string-match "\\`[^hHspP]*\\([^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*[bBhHmpPsS]+[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*\\)[^hHspP]*\\'" fmt)
98 (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
99 (regexp-quote (math-match-substring fmt 1))
100 "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt)
101 (setq fmt (concat (substring fmt 0 (match-beginning 0))
102 "<"
103 (substring fmt (match-beginning 0) (match-end 0))
104 ">"
105 (substring fmt (match-end 0))))))
106 (let ((lfmt nil)
107 (fullfmt nil)
108 (time nil)
109 pos pos2 sym temp)
110 (let ((case-fold-search nil))
111 (and (setq temp (string-match ":[BS]S" fmt))
112 (aset fmt temp ?C)))
113 (while (setq pos (string-match "[<>a-zA-Z]" fmt))
114 (if (> pos 0)
115 (setq lfmt (cons (substring fmt 0 pos) lfmt)))
116 (setq pos2 (1+ pos))
117 (cond ((= (aref fmt pos) ?\<)
118 (and time (error "Nested <'s not allowed"))
119 (and lfmt (setq fullfmt (nconc lfmt fullfmt)
120 lfmt nil))
121 (setq time t))
122 ((= (aref fmt pos) ?\>)
123 (or time (error "Misplaced > in format"))
124 (and lfmt (setq fullfmt (cons (nreverse lfmt) fullfmt)
125 lfmt nil))
126 (setq time nil))
127 (t
128 (if (string-match "\\`[^a-zA-Z]*[bB][a-zA-Z]" fmt)
129 (setq pos2 (1+ pos2)))
130 (while (and (< pos2 (length fmt))
131 (= (upcase (aref fmt pos2))
132 (upcase (aref fmt (1- pos2)))))
133 (setq pos2 (1+ pos2)))
134 (setq sym (intern (substring fmt pos pos2)))
135 (or (memq sym '(Y YY BY YYY YYYY
136 aa AA aaa AAA aaaa AAAA
137 bb BB bbb BBB bbbb BBBB
138 M MM BM mmm Mmm Mmmm MMM MMMM
139 D DD BD d ddd bdd
140 W www Www Wwww WWW WWWW
141 h hh bh H HH BH
142 p P pp PP pppp PPPP
143 m mm bm s ss bss SS BS C
144 N n J j U b))
145 (and (eq sym 'X) (not lfmt) (not fullfmt))
146 (error "Bad format code: %s" sym))
147 (and (memq sym '(bb BB bbb BBB bbbb BBBB))
148 (setq lfmt (cons 'b lfmt)))
149 (setq lfmt (cons sym lfmt))))
150 (setq fmt (substring fmt pos2)))
151 (or (equal fmt "")
152 (setq lfmt (cons fmt lfmt)))
153 (and lfmt (if time
154 (setq fullfmt (cons (nreverse lfmt) fullfmt))
155 (setq fullfmt (nconc lfmt fullfmt))))
156 (calc-change-mode 'calc-date-format (nreverse fullfmt) t))))
157
158
159 (defun calc-hms-mode ()
160 (interactive)
161 (calc-wrapper
162 (calc-change-mode 'calc-angle-mode 'hms)
163 (message "Angles measured in degrees-minutes-seconds")))
164
165
166 (defun calc-now (arg)
167 (interactive "P")
168 (calc-date-zero-args "now" 'calcFunc-now arg))
169
170 (defun calc-date-part (arg)
171 (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
172 (if (or (< arg 1) (> arg 9))
173 (error "Part code out of range"))
174 (calc-wrapper
175 (calc-enter-result 1
176 (nth arg '(nil "year" "mnth" "day" "hour" "minu"
177 "sec" "wday" "yday" "hmst"))
178 (list (nth arg '(nil calcFunc-year calcFunc-month
179 calcFunc-day calcFunc-hour
180 calcFunc-minute calcFunc-second
181 calcFunc-weekday calcFunc-yearday
182 calcFunc-time))
183 (calc-top-n 1)))))
184
185 (defun calc-date (arg)
186 (interactive "p")
187 (if (or (< arg 1) (> arg 6))
188 (error "Between one and six arguments are allowed"))
189 (calc-wrapper
190 (calc-enter-result arg "date" (cons 'calcFunc-date (calc-top-list-n arg)))))
191
192 (defun calc-julian (arg)
193 (interactive "P")
194 (calc-date-one-arg "juln" 'calcFunc-julian arg))
195
196 (defun calc-unix-time (arg)
197 (interactive "P")
198 (calc-date-one-arg "unix" 'calcFunc-unixtime arg))
199
200 (defun calc-time-zone (arg)
201 (interactive "P")
202 (calc-date-zero-args "zone" 'calcFunc-tzone arg))
203
204 (defun calc-convert-time-zones (old &optional new)
205 (interactive "sFrom time zone: ")
206 (calc-wrapper
207 (if (equal old "$")
208 (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv (calc-top-list-n 3)))
209 (if (equal old "") (setq old "local"))
210 (or new
211 (setq new (read-string (concat "From time zone: " old
212 ", to zone: "))))
213 (if (stringp old) (setq old (math-read-expr old)))
214 (if (eq (car-safe old) 'error)
215 (error "Error in expression: %S" (nth 1 old)))
216 (if (equal new "") (setq new "local"))
217 (if (stringp new) (setq new (math-read-expr new)))
218 (if (eq (car-safe new) 'error)
219 (error "Error in expression: %S" (nth 1 new)))
220 (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
221 (calc-top-n 1) old new)))))
222
223 (defun calc-new-week (arg)
224 (interactive "P")
225 (calc-date-one-arg "nwwk" 'calcFunc-newweek arg))
226
227 (defun calc-new-month (arg)
228 (interactive "P")
229 (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg))
230
231 (defun calc-new-year (arg)
232 (interactive "P")
233 (calc-date-one-arg "nwyr" 'calcFunc-newyear arg))
234
235 (defun calc-inc-month (arg)
236 (interactive "p")
237 (calc-date-one-arg "incm" 'calcFunc-incmonth arg))
238
239 (defun calc-business-days-plus (arg)
240 (interactive "P")
241 (calc-wrapper
242 (calc-binary-op "bus+" 'calcFunc-badd arg)))
243
244 (defun calc-business-days-minus (arg)
245 (interactive "P")
246 (calc-wrapper
247 (calc-binary-op "bus-" 'calcFunc-bsub arg)))
248
249 (defun calc-date-zero-args (prefix func arg)
250 (calc-wrapper
251 (if (consp arg)
252 (calc-enter-result 1 prefix (list func (calc-top-n 1)))
253 (calc-enter-result 0 prefix (if arg
254 (list func (prefix-numeric-value arg))
255 (list func))))))
256
257 (defun calc-date-one-arg (prefix func arg)
258 (calc-wrapper
259 (if (consp arg)
260 (calc-enter-result 2 prefix (cons func (calc-top-list-n 2)))
261 (calc-enter-result 1 prefix (if arg
262 (list func (calc-top-n 1)
263 (prefix-numeric-value arg))
264 (list func (calc-top-n 1)))))))
265
266
267 ;;;; Hours-minutes-seconds forms.
268
269 (defun math-normalize-hms (a)
270 (let ((h (math-normalize (nth 1 a)))
271 (m (math-normalize (nth 2 a)))
272 (s (let ((calc-internal-prec (max (- calc-internal-prec 4) 3)))
273 (math-normalize (nth 3 a)))))
274 (if (math-negp h)
275 (progn
276 (if (math-posp s)
277 (setq s (math-add s -60)
278 m (math-add m 1)))
279 (if (math-posp m)
280 (setq m (math-add m -60)
281 h (math-add h 1)))
282 (if (not (Math-lessp -60 s))
283 (setq s (math-add s 60)
284 m (math-add m -1)))
285 (if (not (Math-lessp -60 m))
286 (setq m (math-add m 60)
287 h (math-add h -1))))
288 (if (math-negp s)
289 (setq s (math-add s 60)
290 m (math-add m -1)))
291 (if (math-negp m)
292 (setq m (math-add m 60)
293 h (math-add h -1)))
294 (if (not (Math-lessp s 60))
295 (setq s (math-add s -60)
296 m (math-add m 1)))
297 (if (not (Math-lessp m 60))
298 (setq m (math-add m -60)
299 h (math-add h 1))))
300 (if (and (eq (car-safe s) 'float)
301 (<= (+ (math-numdigs (nth 1 s)) (nth 2 s))
302 (- 2 calc-internal-prec)))
303 (setq s 0))
304 (list 'hms h m s)))
305
306 ;;; Convert A from ANG or current angular mode to HMS format.
307 (defun math-to-hms (a &optional ang) ; [X R] [Public]
308 (cond ((eq (car-safe a) 'hms) a)
309 ((eq (car-safe a) 'sdev)
310 (math-make-sdev (math-to-hms (nth 1 a))
311 (math-to-hms (nth 2 a))))
312 ((not (Math-numberp a))
313 (list 'calcFunc-hms a))
314 ((math-negp a)
315 (math-neg (math-to-hms (math-neg a) ang)))
316 ((eq (or ang calc-angle-mode) 'rad)
317 (math-to-hms (math-div a (math-pi-over-180)) 'deg))
318 ((memq (car-safe a) '(cplx polar)) a)
319 (t
320 ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
321 ; (math-normalize a)))
322 (math-normalize
323 (let* ((b (math-mul a 3600))
324 (hm (math-trunc (math-div b 60)))
325 (hmd (math-idivmod hm 60)))
326 (list 'hms
327 (car hmd)
328 (cdr hmd)
329 (math-sub b (math-mul hm 60))))))))
330 (defun calcFunc-hms (h &optional m s)
331 (or (Math-realp h) (math-reject-arg h 'realp))
332 (or m (setq m 0))
333 (or (Math-realp m) (math-reject-arg m 'realp))
334 (or s (setq s 0))
335 (or (Math-realp s) (math-reject-arg s 'realp))
336 (if (and (not (Math-lessp m 0)) (Math-lessp m 60)
337 (not (Math-lessp s 0)) (Math-lessp s 60))
338 (math-add (math-to-hms h)
339 (list 'hms 0 m s))
340 (math-to-hms (math-add h
341 (math-add (math-div (or m 0) 60)
342 (math-div (or s 0) 3600)))
343 'deg)))
344
345 ;;; Convert A from HMS format to ANG or current angular mode.
346 (defun math-from-hms (a &optional ang) ; [R X] [Public]
347 (cond ((not (eq (car-safe a) 'hms))
348 (if (Math-numberp a)
349 a
350 (if (eq (car-safe a) 'sdev)
351 (math-make-sdev (math-from-hms (nth 1 a) ang)
352 (math-from-hms (nth 2 a) ang))
353 (if (eq (or ang calc-angle-mode) 'rad)
354 (list 'calcFunc-rad a)
355 (list 'calcFunc-deg a)))))
356 ((math-negp a)
357 (math-neg (math-from-hms (math-neg a) ang)))
358 ((eq (or ang calc-angle-mode) 'rad)
359 (math-mul (math-from-hms a 'deg) (math-pi-over-180)))
360 (t
361 (math-add (math-div (math-add (math-div (nth 3 a)
362 '(float 6 1))
363 (nth 2 a))
364 60)
365 (nth 1 a)))))
366
367 ;;;; Date forms.
368
369
370 ;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
371 ;;; These versions are rewritten to use arbitrary-size integers.
372
373 ;;; A numerical date is the number of days since midnight on
374 ;;; the morning of December 31, 1 B.C. (Gregorian) or January 2, 1 A.D. (Julian).
375 ;;; Emacs's calendar refers to such a date as an absolute date, some Calc function
376 ;;; names also use that terminology. If the date is a non-integer, it represents
377 ;;; a specific date and time.
378 ;;; A "dt" is a list of the form, (year month day), corresponding to
379 ;;; an integer code, or (year month day hour minute second), corresponding
380 ;;; to a non-integer code.
381
382 (defun math-date-to-gregorian-dt (date)
383 "Return the day (YEAR MONTH DAY) in the Gregorian calendar.
384 DATE is the number of days since December 31, -1 in the Gregorian calendar."
385 (let* ((month 1)
386 day
387 (year (math-quotient (math-add date (if (Math-lessp date 711859)
388 365 ; for speed, we take
389 -108)) ; >1950 as a special case
390 (if (math-negp date) 366 365)))
391 ; this result may be an overestimate
392 temp)
393 (while (Math-lessp date (setq temp (math-absolute-from-gregorian-dt year 1 1)))
394 (setq year (math-add year -1)))
395 (if (eq year 0) (setq year -1))
396 (setq date (1+ (math-sub date temp)))
397 (setq temp
398 (if (math-leap-year-p year)
399 [1 32 61 92 122 153 183 214 245 275 306 336 999]
400 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
401 (while (>= date (aref temp month))
402 (setq month (1+ month)))
403 (setq day (1+ (- date (aref temp (1- month)))))
404 (list year month day)))
405
406 (defun math-date-to-julian-dt (date)
407 "Return the day (YEAR MONTH DAY) in the Julian calendar.
408 DATE is the number of days since December 31, -1 in the Gregorian calendar."
409 (let* ((month 1)
410 day
411 (year (math-quotient (math-add date (if (Math-lessp date 711859)
412 367 ; for speed, we take
413 -106)) ; >1950 as a special case
414 (if (math-negp date) 366 365)))
415 ; this result may be an overestimate
416 temp)
417 (while (Math-lessp date (setq temp (math-absolute-from-julian-dt year 1 1)))
418 (setq year (math-add year -1)))
419 (if (eq year 0) (setq year -1))
420 (setq date (1+ (math-sub date temp)))
421 (setq temp
422 (if (math-leap-year-p year t)
423 [1 32 61 92 122 153 183 214 245 275 306 336 999]
424 [1 32 60 91 121 152 182 213 244 274 305 335 999]))
425 (while (>= date (aref temp month))
426 (setq month (1+ month)))
427 (setq day (1+ (- date (aref temp (1- month)))))
428 (list year month day)))
429
430 (defun math-date-to-dt (value)
431 "Return the day and time of VALUE.
432 The integer part of VALUE is the number of days since Dec 31, -1
433 in the Gregorian calendar and the remaining part determines the time."
434 (if (eq (car-safe value) 'date)
435 (setq value (nth 1 value)))
436 (or (math-realp value)
437 (math-reject-arg value 'datep))
438 (let* ((parts (math-date-parts value))
439 (date (car parts))
440 (time (nth 1 parts))
441 (dt (if (and calc-gregorian-switch
442 (Math-lessp value
443 (or
444 (nth 3 calc-gregorian-switch)
445 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))
446 ))
447 (math-date-to-julian-dt date)
448 (math-date-to-gregorian-dt date))))
449 (if (math-integerp value)
450 dt
451 (append dt
452 (list
453 (/ time 3600)
454 (% (/ time 60) 60)
455 (math-add (% time 60) (nth 2 parts)))))))
456
457 (defun math-dt-to-date (dt)
458 (or (integerp (nth 1 dt))
459 (math-reject-arg (nth 1 dt) 'fixnump))
460 (if (or (< (nth 1 dt) 1) (> (nth 1 dt) 12))
461 (math-reject-arg (nth 1 dt) "Month value is out of range"))
462 (or (integerp (nth 2 dt))
463 (math-reject-arg (nth 2 dt) 'fixnump))
464 (if (or (< (nth 2 dt) 1) (> (nth 2 dt) 31))
465 (math-reject-arg (nth 2 dt) "Day value is out of range"))
466 (let ((date (math-absolute-from-dt (car dt) (nth 1 dt) (nth 2 dt))))
467 (if (nth 3 dt)
468 (math-add (math-float date)
469 (math-div (math-add (+ (* (nth 3 dt) 3600)
470 (* (nth 4 dt) 60))
471 (nth 5 dt))
472 '(float 864 2)))
473 date)))
474
475 (defun math-date-parts (value &optional offset)
476 (let* ((date (math-floor value))
477 (time (math-round (math-mul (math-sub value (or offset date)) 86400)
478 (and (> calc-internal-prec 12)
479 (- calc-internal-prec 12))))
480 (ftime (math-floor time)))
481 (list date
482 ftime
483 (math-sub time ftime))))
484
485
486 (defun math-this-year ()
487 (nth 5 (decode-time)))
488
489 (defun math-leap-year-p (year &optional julian)
490 "Non-nil if YEAR is a leap year.
491 If JULIAN is non-nil, then use the criterion for leap years
492 in the Julian calendar, otherwise use the criterion in the
493 Gregorian calendar."
494 (if julian
495 (if (math-negp year)
496 (= (math-imod (math-neg year) 4) 1)
497 (= (math-imod year 4) 0))
498 (if (math-negp year)
499 (setq year (math-sub -1 year)))
500 (setq year (math-imod year 400))
501 (or (and (= (% year 4) 0) (/= (% year 100) 0))
502 (= year 0))))
503
504 (defun math-days-in-month (year month)
505 (if (and (= month 2) (math-leap-year-p year))
506 29
507 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
508
509 (defun math-day-in-year (year month day &optional julian)
510 "Return the number of days of the year up to YEAR MONTH DAY.
511 The count includes the given date.
512 If JULIAN is non-nil, use the Julian calendar, otherwise
513 use the Gregorian calendar."
514 (let ((day-of-year (+ day (* 31 (1- month)))))
515 (if (> month 2)
516 (progn
517 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
518 (if (math-leap-year-p year julian)
519 (setq day-of-year (1+ day-of-year)))))
520 day-of-year))
521
522 (defun math-day-number (year month day)
523 "Return the number of days of the year up to YEAR MONTH DAY.
524 The count includes the given date."
525 (if calc-gregorian-switch
526 (cond ((eq year (nth 0 calc-gregorian-switch))
527 (1+
528 (- (math-absolute-from-dt year month day)
529 (math-absolute-from-dt year 1 1))))
530 ((Math-lessp year (nth 0 calc-gregorian-switch))
531 (math-day-in-year year month day t))
532 (t
533 (math-day-in-year year month day)))
534 (math-day-in-year year month day)))
535
536 (defun math-dt-before-p (dt1 dt2)
537 "Non-nil if DT1 occurs before DT2.
538 A DT is a list of the form (YEAR MONTH DAY)."
539 (or (Math-lessp (nth 0 dt1) (nth 0 dt2))
540 (and (equal (nth 0 dt1) (nth 0 dt2))
541 (or (< (nth 1 dt1) (nth 1 dt2))
542 (and (= (nth 1 dt1) (nth 1 dt2))
543 (< (nth 2 dt1) (nth 2 dt2)))))))
544
545 (defun math-absolute-from-gregorian-dt (year month day)
546 "Return the DATE of the day given by the Gregorian day YEAR MONTH DAY.
547 Recall that DATE is the number of days since December 31, -1
548 in the Gregorian calendar."
549 (if (eq year 0) (setq year -1))
550 (let ((yearm1 (math-sub year 1)))
551 (math-sub
552 ;; Add the number of days of the year and the numbers of days
553 ;; in the previous years (leap year days to be added separately)
554 (math-add (math-day-in-year year month day)
555 (math-add (math-mul 365 yearm1)
556 ;; Add the number of Julian leap years
557 (if (math-posp year)
558 (math-quotient yearm1 4)
559 (math-sub 365
560 (math-quotient (math-sub 3 year)
561 4)))))
562 ;; Subtract the number of Julian leap years which are not
563 ;; Gregorian leap years. In C=4N+r centuries, there will
564 ;; be 3N+r of these days. The following will compute
565 ;; 3N+r.
566 (let* ((correction (math-mul (math-quotient yearm1 100) 3))
567 (res (math-idivmod correction 4)))
568 (math-add (if (= (cdr res) 0)
569 0
570 1)
571 (car res))))))
572
573 (defun math-absolute-from-julian-dt (year month day)
574 "Return the DATE of the day given by the Julian day YEAR MONTH DAY.
575 Recall that DATE is the number of days since December 31, -1
576 in the Gregorian calendar."
577 (if (eq year 0) (setq year -1))
578 (let ((yearm1 (math-sub year 1)))
579 (math-sub
580 ;; Add the number of days of the year and the numbers of days
581 ;; in the previous years (leap year days to be added separately)
582 (math-add (math-day-in-year year month day)
583 (math-add (math-mul 365 yearm1)
584 ;; Add the number of Julian leap years
585 (if (math-posp year)
586 (math-quotient yearm1 4)
587 (math-sub 365
588 (math-quotient (math-sub 3 year)
589 4)))))
590 ;; Adjustment, since January 1, 1 (Julian) is absolute day -1
591 2)))
592
593 ;; calc-gregorian-switch is a customizable variable defined in calc.el
594 (defvar calc-gregorian-switch)
595
596
597 (defun math-absolute-from-dt (year month day)
598 "Return the DATE of the day given by the day YEAR MONTH DAY.
599 Recall that DATE is the number of days since December 31, -1
600 in the Gregorian calendar."
601 (if (and calc-gregorian-switch
602 ;; The next few lines determine if the given date
603 ;; occurs before the switch to the Gregorian calendar.
604 (math-dt-before-p (list year month day) calc-gregorian-switch))
605 (math-absolute-from-julian-dt year month day)
606 (math-absolute-from-gregorian-dt year month day)))
607
608 ;;; It is safe to redefine these in your init file to use a different
609 ;;; language.
610
611 (defvar math-long-weekday-names '( "Sunday" "Monday" "Tuesday" "Wednesday"
612 "Thursday" "Friday" "Saturday" ))
613 (defvar math-short-weekday-names '( "Sun" "Mon" "Tue" "Wed"
614 "Thu" "Fri" "Sat" ))
615
616 (defvar math-long-month-names '( "January" "February" "March" "April"
617 "May" "June" "July" "August"
618 "September" "October" "November" "December" ))
619 (defvar math-short-month-names '( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
620 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
621
622
623 (defvar math-format-date-cache nil)
624
625 ;; The variables math-fd-date, math-fd-dt, math-fd-year,
626 ;; math-fd-month, math-fd-day, math-fd-weekday, math-fd-hour,
627 ;; math-fd-minute, math-fd-second, math-fd-bc-flag are local
628 ;; to math-format-date, but are used by math-format-date-part,
629 ;; which is called by math-format-date.
630 (defvar math-fd-date)
631 (defvar math-fd-dt)
632 (defvar math-fd-year)
633 (defvar math-fd-month)
634 (defvar math-fd-day)
635 (defvar math-fd-weekday)
636 (defvar math-fd-hour)
637 (defvar math-fd-minute)
638 (defvar math-fd-second)
639 (defvar math-fd-bc-flag)
640
641 (defun math-format-date (math-fd-date)
642 (if (eq (car-safe math-fd-date) 'date)
643 (setq math-fd-date (nth 1 math-fd-date)))
644 (let ((entry (list math-fd-date calc-internal-prec calc-date-format)))
645 (or (cdr (assoc entry math-format-date-cache))
646 (let* ((math-fd-dt nil)
647 (calc-group-digits nil)
648 (calc-leading-zeros nil)
649 (calc-number-radix 10)
650 (calc-twos-complement-mode nil)
651 math-fd-year math-fd-month math-fd-day math-fd-weekday
652 math-fd-hour math-fd-minute math-fd-second
653 (math-fd-bc-flag nil)
654 (fmt (apply 'concat (mapcar 'math-format-date-part
655 calc-date-format))))
656 (setq math-format-date-cache (cons (cons entry fmt)
657 math-format-date-cache))
658 (and (setq math-fd-dt (nthcdr 10 math-format-date-cache))
659 (setcdr math-fd-dt nil))
660 fmt))))
661
662 (defconst math-julian-date-beginning '(float 17214225 -1)
663 "The beginning of the Julian date calendar,
664 as measured in the number of days before December 31, 1 BC (Gregorian).")
665
666 (defconst math-julian-date-beginning-int 1721423
667 "The beginning of the Julian date calendar,
668 as measured in the integer number of days before December 31, 1 BC (Gregorian).")
669
670 (defun math-format-date-part (x)
671 (cond ((stringp x)
672 x)
673 ((listp x)
674 (if (math-integerp math-fd-date)
675 ""
676 (apply 'concat (mapcar 'math-format-date-part x))))
677 ((eq x 'X)
678 "")
679 ((eq x 'N)
680 (math-format-number math-fd-date))
681 ((eq x 'n)
682 (math-format-number (math-floor math-fd-date)))
683 ((eq x 'J)
684 (math-format-number
685 (math-add math-fd-date math-julian-date-beginning)))
686 ((eq x 'j)
687 (math-format-number (math-add
688 (math-floor math-fd-date)
689 math-julian-date-beginning-int)))
690 ((eq x 'U)
691 (math-format-number (nth 1 (math-date-parts math-fd-date 719164))))
692 ((progn
693 (or math-fd-dt
694 (progn
695 (setq math-fd-dt (math-date-to-dt math-fd-date)
696 math-fd-year (car math-fd-dt)
697 math-fd-month (nth 1 math-fd-dt)
698 math-fd-day (nth 2 math-fd-dt)
699 math-fd-weekday (math-mod (math-floor math-fd-date) 7)
700 math-fd-hour (nth 3 math-fd-dt)
701 math-fd-minute (nth 4 math-fd-dt)
702 math-fd-second (nth 5 math-fd-dt))
703 (and (memq 'b calc-date-format)
704 (math-negp math-fd-year)
705 (setq math-fd-year (math-neg math-fd-year)
706 math-fd-bc-flag t))))
707 (memq x '(Y YY BY)))
708 (if (and (integerp math-fd-year) (> math-fd-year 1940) (< math-fd-year 2040))
709 (format (cond ((eq x 'YY) "%02d")
710 ((eq x 'BYY) "%2d")
711 (t "%d"))
712 (% math-fd-year 100))
713 (if (and (natnump math-fd-year) (< math-fd-year 100))
714 (format "+%d" math-fd-year)
715 (math-format-number math-fd-year))))
716 ((eq x 'YYY)
717 (math-format-number math-fd-year))
718 ((eq x 'YYYY)
719 (if (and (natnump math-fd-year) (< math-fd-year 100))
720 (format "+%d" math-fd-year)
721 (math-format-number math-fd-year)))
722 ((eq x 'b) "")
723 ((eq x 'aa)
724 (and (not math-fd-bc-flag) "ad"))
725 ((eq x 'AA)
726 (and (not math-fd-bc-flag) "AD"))
727 ((eq x 'aaa)
728 (and (not math-fd-bc-flag) "ad "))
729 ((eq x 'AAA)
730 (and (not math-fd-bc-flag) "AD "))
731 ((eq x 'aaaa)
732 (and (not math-fd-bc-flag) "a.d."))
733 ((eq x 'AAAA)
734 (and (not math-fd-bc-flag) "A.D."))
735 ((eq x 'bb)
736 (and math-fd-bc-flag "bc"))
737 ((eq x 'BB)
738 (and math-fd-bc-flag "BC"))
739 ((eq x 'bbb)
740 (and math-fd-bc-flag " bc"))
741 ((eq x 'BBB)
742 (and math-fd-bc-flag " BC"))
743 ((eq x 'bbbb)
744 (and math-fd-bc-flag "b.c."))
745 ((eq x 'BBBB)
746 (and math-fd-bc-flag "B.C."))
747 ((eq x 'M)
748 (format "%d" math-fd-month))
749 ((eq x 'MM)
750 (format "%02d" math-fd-month))
751 ((eq x 'BM)
752 (format "%2d" math-fd-month))
753 ((eq x 'mmm)
754 (downcase (nth (1- math-fd-month) math-short-month-names)))
755 ((eq x 'Mmm)
756 (nth (1- math-fd-month) math-short-month-names))
757 ((eq x 'MMM)
758 (upcase (nth (1- math-fd-month) math-short-month-names)))
759 ((eq x 'Mmmm)
760 (nth (1- math-fd-month) math-long-month-names))
761 ((eq x 'MMMM)
762 (upcase (nth (1- math-fd-month) math-long-month-names)))
763 ((eq x 'D)
764 (format "%d" math-fd-day))
765 ((eq x 'DD)
766 (format "%02d" math-fd-day))
767 ((eq x 'BD)
768 (format "%2d" math-fd-day))
769 ((eq x 'W)
770 (format "%d" math-fd-weekday))
771 ((eq x 'www)
772 (downcase (nth math-fd-weekday math-short-weekday-names)))
773 ((eq x 'Www)
774 (nth math-fd-weekday math-short-weekday-names))
775 ((eq x 'WWW)
776 (upcase (nth math-fd-weekday math-short-weekday-names)))
777 ((eq x 'Wwww)
778 (nth math-fd-weekday math-long-weekday-names))
779 ((eq x 'WWWW)
780 (upcase (nth math-fd-weekday math-long-weekday-names)))
781 ((eq x 'd)
782 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day)))
783 ((eq x 'ddd)
784 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day)))
785 ((eq x 'bdd)
786 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day)))
787 ((eq x 'h)
788 (and math-fd-hour (format "%d" math-fd-hour)))
789 ((eq x 'hh)
790 (and math-fd-hour (format "%02d" math-fd-hour)))
791 ((eq x 'bh)
792 (and math-fd-hour (format "%2d" math-fd-hour)))
793 ((eq x 'H)
794 (and math-fd-hour (format "%d" (1+ (% (+ math-fd-hour 11) 12)))))
795 ((eq x 'HH)
796 (and math-fd-hour (format "%02d" (1+ (% (+ math-fd-hour 11) 12)))))
797 ((eq x 'BH)
798 (and math-fd-hour (format "%2d" (1+ (% (+ math-fd-hour 11) 12)))))
799 ((eq x 'p)
800 (and math-fd-hour (if (< math-fd-hour 12) "a" "p")))
801 ((eq x 'P)
802 (and math-fd-hour (if (< math-fd-hour 12) "A" "P")))
803 ((eq x 'pp)
804 (and math-fd-hour (if (< math-fd-hour 12) "am" "pm")))
805 ((eq x 'PP)
806 (and math-fd-hour (if (< math-fd-hour 12) "AM" "PM")))
807 ((eq x 'pppp)
808 (and math-fd-hour (if (< math-fd-hour 12) "a.m." "p.m.")))
809 ((eq x 'PPPP)
810 (and math-fd-hour (if (< math-fd-hour 12) "A.M." "P.M.")))
811 ((eq x 'm)
812 (and math-fd-minute (format "%d" math-fd-minute)))
813 ((eq x 'mm)
814 (and math-fd-minute (format "%02d" math-fd-minute)))
815 ((eq x 'bm)
816 (and math-fd-minute (format "%2d" math-fd-minute)))
817 ((eq x 'C)
818 (and math-fd-second (not (math-zerop math-fd-second))
819 ":"))
820 ((memq x '(s ss bs SS BS))
821 (and math-fd-second
822 (not (and (memq x '(SS BS)) (math-zerop math-fd-second)))
823 (if (integerp math-fd-second)
824 (format (cond ((memq x '(ss SS)) "%02d")
825 ((memq x '(bs BS)) "%2d")
826 (t "%d"))
827 math-fd-second)
828 (concat (if (Math-lessp math-fd-second 10)
829 (cond ((memq x '(ss SS)) "0")
830 ((memq x '(bs BS)) " ")
831 (t ""))
832 "")
833 (let ((calc-float-format
834 (list 'fix (min (- 12 calc-internal-prec)
835 0))))
836 (math-format-number math-fd-second))))))))
837
838 ;; The variable math-pd-str is local to math-parse-date and
839 ;; math-parse-standard-date, but is used by math-parse-date-word,
840 ;; which is called by math-parse-date and math-parse-standard-date.
841 (defvar math-pd-str)
842
843 (defun math-parse-date (math-pd-str)
844 (catch 'syntax
845 (or (math-parse-standard-date math-pd-str t)
846 (math-parse-standard-date math-pd-str nil)
847 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str)
848 (list 'date (math-read-number (math-match-substring math-pd-str 1))))
849 (let ((case-fold-search t)
850 (year nil) (month nil) (day nil) (weekday nil)
851 (hour nil) (minute nil) (second nil) (bc-flag nil)
852 (a nil) (b nil) (c nil) (bigyear nil) temp)
853
854 ;; Extract the time, if any.
855 (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)
856 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str))
857 (let ((ampm (math-match-substring math-pd-str 6)))
858 (setq hour (string-to-number (math-match-substring math-pd-str 1))
859 minute (math-match-substring math-pd-str 2)
860 second (math-match-substring math-pd-str 4)
861 math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
862 (substring math-pd-str (match-end 0))))
863 (if (equal minute "")
864 (setq minute 0)
865 (setq minute (string-to-number minute)))
866 (if (equal second "")
867 (setq second 0)
868 (setq second (math-read-number second)))
869 (if (equal ampm "")
870 (if (> hour 23)
871 (throw 'syntax "Hour value out of range"))
872 (setq ampm (upcase (aref ampm 0)))
873 (if (memq ampm '(?N ?M))
874 (if (and (= hour 12) (= minute 0) (eq second 0))
875 (if (eq ampm ?M) (setq hour 0))
876 (throw 'syntax
877 "Time must be 12:00:00 in this context"))
878 (if (or (= hour 0) (> hour 12))
879 (throw 'syntax "Hour value out of range"))
880 (if (eq (= ampm ?A) (= hour 12))
881 (setq hour (% (+ hour 12) 24)))))))
882
883 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
884 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str)
885 (progn
886 (setq math-pd-str (copy-sequence math-pd-str))
887 (aset math-pd-str (match-beginning 1) ?\/)))
888
889 ;; Extract obvious month or weekday names.
890 (if (string-match "[a-zA-Z]" math-pd-str)
891 (progn
892 (setq month (math-parse-date-word math-long-month-names))
893 (setq weekday (math-parse-date-word math-long-weekday-names))
894 (or month (setq month
895 (math-parse-date-word math-short-month-names)))
896 (or weekday (math-parse-date-word math-short-weekday-names))
897 (or hour
898 (if (setq temp (math-parse-date-word
899 '( "noon" "midnight" "mid" )))
900 (setq hour (if (= temp 1) 12 0) minute 0 second 0)))
901 (or (math-parse-date-word '( "ad" "a.d." ))
902 (if (math-parse-date-word '( "bc" "b.c." ))
903 (setq bc-flag t)))
904 (if (string-match "[a-zA-Z]+" math-pd-str)
905 (throw 'syntax (format "Bad word in date: \"%s\""
906 (math-match-substring math-pd-str 0))))))
907
908 ;; If there is a huge number other than the year, ignore it.
909 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str)
910 (setq temp (concat (substring math-pd-str 0 (match-beginning 0))
911 (substring math-pd-str (match-end 0))))
912 (string-match
913 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp))
914 (setq math-pd-str temp))
915
916 ;; If there is a number with a sign or a large number, it is a year.
917 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str)
918 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str))
919 (setq year (math-match-substring math-pd-str 1)
920 math-pd-str (concat (substring math-pd-str 0 (match-beginning 1))
921 (substring math-pd-str (match-end 1)))
922 year (math-read-number year)
923 bigyear t))
924
925 ;; Collect remaining numbers.
926 (setq temp 0)
927 (while (string-match "[0-9]+" math-pd-str temp)
928 (and c (throw 'syntax "Too many numbers in date"))
929 (setq c (string-to-number (math-match-substring math-pd-str 0)))
930 (or b (setq b c c nil))
931 (or a (setq a b b nil))
932 (setq temp (match-end 0)))
933
934 ;; Check that we have the right amount of information.
935 (setq temp (+ (if year 1 0) (if month 1 0) (if day 1 0)
936 (if a 1 0) (if b 1 0) (if c 1 0)))
937 (if (> temp 3)
938 (throw 'syntax "Too many numbers in date")
939 (if (or (< temp 2) (and year (= temp 2)))
940 (throw 'syntax "Not enough numbers in date")
941 (if (= temp 2) ; if year omitted, assume current year
942 (setq year (math-this-year)))))
943
944 ;; A large number must be a year.
945 (or year
946 (if (and a (or (> a 31) (< a 1)))
947 (setq year a a b b c c nil)
948 (if (and b (or (> b 31) (< b 1)))
949 (setq year b b c c nil)
950 (if (and c (or (> c 31) (< c 1)))
951 (setq year c c nil)))))
952
953 ;; A medium-large number must be a day.
954 (if year
955 (if (and a (> a 12))
956 (setq day a a b b c c nil)
957 (if (and b (> b 12))
958 (setq day b b c c nil)
959 (if (and c (> c 12))
960 (setq day c c nil)))))
961
962 ;; We may know enough to sort it out now.
963 (if (and year day)
964 (or month (setq month a))
965 (if (and year month)
966 (setq day a)
967
968 ;; Interpret order of numbers as same as for display format.
969 (setq temp calc-date-format)
970 (while temp
971 (cond ((not (symbolp (car temp))))
972 ((memq (car temp) '(Y YY BY YYY YYYY))
973 (or year (setq year a a b b c)))
974 ((memq (car temp) '(M MM BM mmm Mmm Mmmm MMM MMMM))
975 (or month (setq month a a b b c)))
976 ((memq (car temp) '(D DD BD))
977 (or day (setq day a a b b c))))
978 (setq temp (cdr temp)))
979
980 ;; If display format was not complete, assume American style.
981 (or month (setq month a a b b c))
982 (or day (setq day a a b b c))
983 (or year (setq year a a b b c))))
984
985 (if bc-flag
986 (setq year (math-neg (math-abs year))))
987
988 (math-parse-date-validate year bigyear month day
989 hour minute second)))))
990
991 (defun math-parse-date-validate (year bigyear month day hour minute second)
992 (and (not bigyear) (natnump year) (< year 100)
993 (setq year (+ year (if (< year 40) 2000 1900))))
994 (if (eq year 0)
995 (throw 'syntax "Year value is out of range"))
996 (if (or (< month 1) (> month 12))
997 (throw 'syntax "Month value is out of range"))
998 (if (or (< day 1) (> day (math-days-in-month year month)))
999 (throw 'syntax "Day value is out of range"))
1000 (and hour
1001 (progn
1002 (if (or (< hour 0) (> hour 23))
1003 (throw 'syntax "Hour value is out of range"))
1004 (if (or (< minute 0) (> minute 59))
1005 (throw 'syntax "Minute value is out of range"))
1006 (if (or (math-negp second) (not (Math-lessp second 60)))
1007 (throw 'syntax "Seconds value is out of range"))))
1008 (list 'date (math-dt-to-date (append (list year month day)
1009 (and hour (list hour minute second))))))
1010
1011 (defun math-parse-date-word (names &optional front)
1012 (let ((n 1))
1013 (while (and names (not (string-match (if (equal (car names) "Sep")
1014 "Sept?"
1015 (regexp-quote (car names)))
1016 math-pd-str)))
1017 (setq names (cdr names)
1018 n (1+ n)))
1019 (and names
1020 (or (not front) (= (match-beginning 0) 0))
1021 (progn
1022 (setq math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
1023 (if front "" " ")
1024 (substring math-pd-str (match-end 0))))
1025 n))))
1026
1027 (defun math-parse-standard-date (math-pd-str with-time)
1028 (let ((case-fold-search t)
1029 (okay t) num
1030 (fmt calc-date-format) this next (gnext nil)
1031 (year nil) (month nil) (day nil) (bigyear nil) (yearday nil)
1032 (hour nil) (minute nil) (second nil) (bc-flag nil))
1033 (while (and fmt okay)
1034 (setq this (car fmt)
1035 fmt (setq fmt (or (cdr fmt)
1036 (prog1
1037 gnext
1038 (setq gnext nil))))
1039 next (car fmt))
1040 (if (consp next) (setq next (car next)))
1041 (or (cond ((listp this)
1042 (or (not with-time)
1043 (not this)
1044 (setq gnext fmt
1045 fmt this)))
1046 ((stringp this)
1047 (if (and (<= (length this) (length math-pd-str))
1048 (equal this
1049 (substring math-pd-str 0 (length this))))
1050 (setq math-pd-str (substring math-pd-str (length this)))))
1051 ((eq this 'X)
1052 t)
1053 ((memq this '(n N j J))
1054 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str)
1055 (setq num (math-match-substring math-pd-str 0)
1056 math-pd-str (substring math-pd-str (match-end 0))
1057 num (math-date-to-dt (math-read-number num))
1058 num (math-sub num
1059 (if (memq this '(n N))
1060 0
1061 (if (or (eq this 'j)
1062 (math-integerp num))
1063 math-julian-date-beginning-int
1064 math-julian-date-beginning)))
1065 hour (or (nth 3 num) hour)
1066 minute (or (nth 4 num) minute)
1067 second (or (nth 5 num) second)
1068 year (car num)
1069 month (nth 1 num)
1070 day (nth 2 num))))
1071 ((eq this 'U)
1072 (and (string-match "\\`[-+]?[0-9]+" math-pd-str)
1073 (setq num (math-match-substring math-pd-str 0)
1074 math-pd-str (substring math-pd-str (match-end 0))
1075 num (math-date-to-dt
1076 (math-add 719164
1077 (math-div (math-read-number num)
1078 '(float 864 2))))
1079 hour (nth 3 num)
1080 minute (nth 4 num)
1081 second (nth 5 num)
1082 year (car num)
1083 month (nth 1 num)
1084 day (nth 2 num))))
1085 ((memq this '(mmm Mmm MMM))
1086 (setq month (math-parse-date-word math-short-month-names t)))
1087 ((memq this '(Mmmm MMMM))
1088 (setq month (math-parse-date-word math-long-month-names t)))
1089 ((memq this '(www Www WWW))
1090 (math-parse-date-word math-short-weekday-names t))
1091 ((memq this '(Wwww WWWW))
1092 (math-parse-date-word math-long-weekday-names t))
1093 ((memq this '(p P))
1094 (if (string-match "\\`a" math-pd-str)
1095 (setq hour (if (= hour 12) 0 hour)
1096 math-pd-str (substring math-pd-str 1))
1097 (if (string-match "\\`p" math-pd-str)
1098 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
1099 math-pd-str (substring math-pd-str 1)))))
1100 ((memq this '(pp PP pppp PPPP))
1101 (if (string-match "\\`am\\|a\\.m\\." math-pd-str)
1102 (setq hour (if (= hour 12) 0 hour)
1103 math-pd-str (substring math-pd-str (match-end 0)))
1104 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str)
1105 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
1106 math-pd-str (substring math-pd-str (match-end 0))))))
1107 ((memq this '(Y YY BY YYY YYYY))
1108 (and (if (memq next '(MM DD ddd hh HH mm ss SS))
1109 (if (memq this '(Y YY BYY))
1110 (string-match "\\` *[0-9][0-9]" math-pd-str)
1111 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str))
1112 (string-match "\\`[-+]?[0-9]+" math-pd-str))
1113 (setq year (math-match-substring math-pd-str 0)
1114 bigyear (or (eq this 'YYY)
1115 (memq (aref math-pd-str 0) '(?\+ ?\-)))
1116 math-pd-str (substring math-pd-str (match-end 0))
1117 year (math-read-number year))))
1118 ((eq this 'b)
1119 t)
1120 ((memq this '(aa AA aaaa AAAA))
1121 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str)
1122 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1123 ((memq this '(aaa AAA))
1124 (if (string-match "\\` *ad *" math-pd-str)
1125 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1126 ((memq this '(bb BB bbb BBB bbbb BBBB))
1127 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str)
1128 (setq math-pd-str (substring math-pd-str (match-end 0))
1129 bc-flag t)))
1130 ((memq this '(s ss bs SS BS))
1131 (and (if (memq next '(YY YYYY MM DD hh HH mm))
1132 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str)
1133 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str))
1134 (setq second (math-match-substring math-pd-str 0)
1135 math-pd-str (substring math-pd-str (match-end 0))
1136 second (math-read-number second))))
1137 ((eq this 'C)
1138 (if (string-match "\\`:[0-9][0-9]" math-pd-str)
1139 (setq math-pd-str (substring math-pd-str 1))
1140 t))
1141 ((or (not (if (and (memq this '(ddd MM DD hh HH mm))
1142 (memq next '(YY YYYY MM DD ddd
1143 hh HH mm ss SS)))
1144 (if (eq this 'ddd)
1145 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str)
1146 (string-match "\\` *[0-9][0-9]" math-pd-str))
1147 (string-match "\\` *[0-9]+" math-pd-str)))
1148 (and (setq num (string-to-number
1149 (math-match-substring math-pd-str 0))
1150 math-pd-str (substring math-pd-str (match-end 0)))
1151 nil))
1152 nil)
1153 ((eq this 'W)
1154 (and (>= num 0) (< num 7)))
1155 ((memq this '(d ddd bdd))
1156 (setq yearday num))
1157 ((memq this '(M MM BM))
1158 (setq month num))
1159 ((memq this '(D DD BD))
1160 (setq day num))
1161 ((memq this '(h hh bh H HH BH))
1162 (setq hour num))
1163 ((memq this '(m mm bm))
1164 (setq minute num)))
1165 (setq okay nil)))
1166 (if yearday
1167 (if (and month day)
1168 (setq yearday nil)
1169 (setq month 1 day 1)))
1170 (if (and okay (equal math-pd-str ""))
1171 (and month day (or (not (or hour minute second))
1172 (and hour minute))
1173 (progn
1174 (or year (setq year (math-this-year)))
1175 (or second (setq second 0))
1176 (if bc-flag
1177 (setq year (math-neg (math-abs year))))
1178 (setq day (math-parse-date-validate year bigyear month day
1179 hour minute second))
1180 (if yearday
1181 (setq day (math-add day (1- yearday))))
1182 day)))))
1183
1184
1185 (defun calcFunc-now (&optional zone)
1186 (let ((date (let ((calc-date-format nil))
1187 (math-parse-date (current-time-string)))))
1188 (if (consp date)
1189 (if zone
1190 (math-add date (math-div (math-sub (calcFunc-tzone nil date)
1191 (calcFunc-tzone zone date))
1192 '(float 864 2)))
1193 date)
1194 (calc-record-why "*Unable to interpret current date from system")
1195 (append (list 'calcFunc-now) (and zone (list zone))))))
1196
1197 (defun calcFunc-year (date)
1198 (car (math-date-to-dt date)))
1199
1200 (defun calcFunc-month (date)
1201 (nth 1 (math-date-to-dt date)))
1202
1203 (defun calcFunc-day (date)
1204 (nth 2 (math-date-to-dt date)))
1205
1206 (defun calcFunc-weekday (date)
1207 (if (eq (car-safe date) 'date)
1208 (setq date (nth 1 date)))
1209 (or (math-realp date)
1210 (math-reject-arg date 'datep))
1211 (math-mod (math-floor date) 7))
1212
1213 (defun calcFunc-yearday (date)
1214 (let ((dt (math-date-to-dt date)))
1215 (math-day-number (car dt) (nth 1 dt) (nth 2 dt))))
1216
1217 (defun calcFunc-hour (date)
1218 (if (eq (car-safe date) 'hms)
1219 (nth 1 date)
1220 (or (nth 3 (math-date-to-dt date)) 0)))
1221
1222 (defun calcFunc-minute (date)
1223 (if (eq (car-safe date) 'hms)
1224 (nth 2 date)
1225 (or (nth 4 (math-date-to-dt date)) 0)))
1226
1227 (defun calcFunc-second (date)
1228 (if (eq (car-safe date) 'hms)
1229 (nth 3 date)
1230 (or (nth 5 (math-date-to-dt date)) 0)))
1231
1232 (defun calcFunc-time (date)
1233 (let ((dt (math-date-to-dt date)))
1234 (if (nth 3 dt)
1235 (cons 'hms (nthcdr 3 dt))
1236 (list 'hms 0 0 0))))
1237
1238 (defun calcFunc-date (date &optional month day hour minute second)
1239 (and (math-messy-integerp month) (setq month (math-trunc month)))
1240 (and month (not (integerp month)) (math-reject-arg month 'fixnump))
1241 (and (math-messy-integerp day) (setq day (math-trunc day)))
1242 (and day (not (integerp day)) (math-reject-arg day 'fixnump))
1243 (if (and (eq (car-safe hour) 'hms) (not minute))
1244 (setq second (nth 3 hour)
1245 minute (nth 2 hour)
1246 hour (nth 1 hour)))
1247 (and (math-messy-integerp hour) (setq hour (math-trunc hour)))
1248 (and hour (not (integerp hour)) (math-reject-arg hour 'fixnump))
1249 (and (math-messy-integerp minute) (setq minute (math-trunc minute)))
1250 (and minute (not (integerp minute)) (math-reject-arg minute 'fixnump))
1251 (and (math-messy-integerp second) (setq second (math-trunc second)))
1252 (and second (not (math-realp second)) (math-reject-arg second 'realp))
1253 (if month
1254 (progn
1255 (and (math-messy-integerp date) (setq date (math-trunc date)))
1256 (and date (not (math-integerp date)) (math-reject-arg date 'integerp))
1257 (if day
1258 (if hour
1259 (list 'date (math-dt-to-date (list date month day hour
1260 (or minute 0)
1261 (or second 0))))
1262 (list 'date (math-dt-to-date (list date month day))))
1263 (list 'date (math-dt-to-date (list (math-this-year) date month)))))
1264 (if (math-realp date)
1265 (list 'date date)
1266 (if (eq (car date) 'date)
1267 (nth 1 date)
1268 (math-reject-arg date 'datep)))))
1269
1270 (defun calcFunc-julian (date &optional zone)
1271 (if (math-realp date)
1272 (list 'date (if (math-integerp date)
1273 (math-sub date math-julian-date-beginning-int)
1274 (setq date (math-sub date math-julian-date-beginning))
1275 (math-sub date (math-div (calcFunc-tzone zone date)
1276 '(float 864 2)))))
1277 (if (eq (car date) 'date)
1278 (math-add (nth 1 date) (if (math-integerp (nth 1 date))
1279 math-julian-date-beginning-int
1280 (math-add math-julian-date-beginning
1281 (math-div (calcFunc-tzone zone date)
1282 '(float 864 2)))))
1283 (math-reject-arg date 'datep))))
1284
1285 (defun calcFunc-unixtime (date &optional zone)
1286 (if (math-realp date)
1287 (progn
1288 (setq date (math-add 719164 (math-div date '(float 864 2))))
1289 (list 'date (math-sub date (math-div (calcFunc-tzone zone date)
1290 '(float 864 2)))))
1291 (if (eq (car date) 'date)
1292 (math-add (nth 1 (math-date-parts (nth 1 date) 719164))
1293 (calcFunc-tzone zone date))
1294 (math-reject-arg date 'datep))))
1295
1296
1297 ;;; Note: Longer names must appear before shorter names which are
1298 ;;; substrings of them.
1299 (defvar math-tzone-names
1300 '(( "UTC" 0 0)
1301 ( "MEGT" -1 "MET" "METDST" ) ; Middle Europe
1302 ( "METDST" -1 -1 ) ( "MET" -1 0 )
1303 ( "MEGZ" -1 "MEZ" "MESZ" ) ( "MEZ" -1 0 ) ( "MESZ" -1 -1 )
1304 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1305 ( "WETDST" 0 -1 ) ( "WET" 0 0 )
1306 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -1 ) ; Britain
1307 ( "NGT" (float 35 -1) "NST" "NDT" ) ; Newfoundland
1308 ( "NST" (float 35 -1) 0 ) ( "NDT" (float 35 -1) -1 )
1309 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -1 ) ; Atlantic
1310 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -1 ) ; Eastern
1311 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -1 ) ; Central
1312 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -1 ) ; Mountain
1313 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -1 ) ; Pacific
1314 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -1 ) ; Yukon
1315 )
1316 "No doc yet. See calc manual for now. ")
1317
1318 (defvar var-TimeZone nil)
1319
1320 ;; From cal-dst
1321 (defvar calendar-current-time-zone-cache)
1322
1323 (defvar math-calendar-tzinfo
1324 nil
1325 "Information about the timezone, retrieved from the calendar.")
1326
1327 (defun math-get-calendar-tzinfo ()
1328 "Get information about the timezone from the calendar.
1329 The result should be a list of two items about the current time zone:
1330 first, the number of seconds difference from GMT
1331 second, the number of seconds offset for daylight savings."
1332 (if math-calendar-tzinfo
1333 math-calendar-tzinfo
1334 (require 'cal-dst)
1335 (let ((tzinfo (progn
1336 (calendar-current-time-zone)
1337 calendar-current-time-zone-cache)))
1338 (setq math-calendar-tzinfo
1339 (list (* 60 (abs (nth 0 tzinfo)))
1340 (* 60 (nth 1 tzinfo)))))))
1341
1342 (defun calcFunc-tzone (&optional zone date)
1343 (if zone
1344 (cond ((math-realp zone)
1345 (math-round (math-mul zone 3600)))
1346 ((eq (car zone) 'hms)
1347 (math-round (math-mul (math-from-hms zone 'deg) 3600)))
1348 ((eq (car zone) '+)
1349 (math-add (calcFunc-tzone (nth 1 zone) date)
1350 (calcFunc-tzone (nth 2 zone) date)))
1351 ((eq (car zone) '-)
1352 (math-sub (calcFunc-tzone (nth 1 zone) date)
1353 (calcFunc-tzone (nth 2 zone) date)))
1354 ((eq (car zone) 'var)
1355 (let ((name (upcase (symbol-name (nth 1 zone))))
1356 found)
1357 (if (setq found (assoc name math-tzone-names))
1358 (calcFunc-tzone (math-add (nth 1 found)
1359 (if (integerp (nth 2 found))
1360 (nth 2 found)
1361 (or
1362 (math-daylight-savings-adjust
1363 date (car found))
1364 0)))
1365 date)
1366 (if (equal name "LOCAL")
1367 (calcFunc-tzone nil date)
1368 (math-reject-arg zone "*Unrecognized time zone name")))))
1369 (t (math-reject-arg zone "*Expected a time zone")))
1370 (if (calc-var-value 'var-TimeZone)
1371 (calcFunc-tzone (calc-var-value 'var-TimeZone) date)
1372 (let ((tzinfo (math-get-calendar-tzinfo)))
1373 (+ (nth 0 tzinfo)
1374 (* (math-cal-daylight-savings-adjust date) (nth 1 tzinfo)))))))
1375
1376 (defvar math-daylight-savings-hook 'math-std-daylight-savings)
1377
1378 (defun math-daylight-savings-adjust (date zone &optional dt)
1379 (or date (setq date (nth 1 (calcFunc-now))))
1380 (let (bump)
1381 (if (eq (car-safe date) 'date)
1382 (setq bump 0
1383 date (nth 1 date))
1384 (if (and date (math-realp date))
1385 (let ((zadj (assoc zone math-tzone-names)))
1386 (if zadj (setq bump -1
1387 date (math-sub date (math-div (nth 1 zadj)
1388 '(float 24 0))))))
1389 (math-reject-arg date 'datep)))
1390 (setq date (math-float date))
1391 (or dt (setq dt (math-date-to-dt date)))
1392 (and math-daylight-savings-hook
1393 (funcall math-daylight-savings-hook date dt zone bump))))
1394
1395 ;;; Based on part of dst-adjust-time in cal-dst.el
1396 ;;; For calcFunc-dst, when zone=nil
1397 (defun math-cal-daylight-savings-adjust (date)
1398 "Return -1 if DATE is using daylight saving, 0 otherwise."
1399 (require 'cal-dst)
1400 (unless date (setq date (calcFunc-now)))
1401 (let* ((dt (math-date-to-dt date))
1402 (time (cond
1403 ((nth 3 dt)
1404 (nth 3 dt))
1405 ((nth 4 dt)
1406 (+ (nth 3 dt) (/ (nth 4 dt) 60.0)))
1407 (t
1408 0)))
1409 (rounded-abs-date
1410 (+
1411 (calendar-absolute-from-gregorian
1412 (list (nth 1 dt) (nth 2 dt) (nth 0 dt)))
1413 (/ (round (* 60 time)) 60.0 24.0))))
1414 (if (dst-in-effect rounded-abs-date)
1415 -1
1416 0)))
1417
1418 (defun calcFunc-dsadj (date &optional zone)
1419 (if zone
1420 (or (eq (car-safe zone) 'var)
1421 (math-reject-arg zone "*Time zone variable expected"))
1422 (setq zone (calc-var-value 'var-TimeZone)))
1423 (if zone
1424 (progn
1425 (setq zone (and (eq (car-safe zone) 'var)
1426 (upcase (symbol-name (nth 1 zone)))))
1427 (let ((zadj (assoc zone math-tzone-names)))
1428 (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1429 (if (integerp (nth 2 zadj))
1430 (nth 2 zadj)
1431 (math-daylight-savings-adjust date zone))))
1432 (math-cal-daylight-savings-adjust date)))
1433
1434 ;; (defun calcFunc-dsadj (date &optional zone)
1435 ;; (if zone
1436 ;; (or (eq (car-safe zone) 'var)
1437 ;; (math-reject-arg zone "*Time zone variable expected"))
1438 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1439 ;; (progn
1440 ;; (calcFunc-tzone)
1441 ;; (calc-var-value 'var-TimeZone)))))
1442 ;; (setq zone (and (eq (car-safe zone) 'var)
1443 ;; (upcase (symbol-name (nth 1 zone)))))
1444 ;; (let ((zadj (assoc zone math-tzone-names)))
1445 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1446 ;; (if (integerp (nth 2 zadj))
1447 ;; (nth 2 zadj)
1448 ;; (math-daylight-savings-adjust date zone))))
1449
1450 (defun calcFunc-tzconv (date z1 z2)
1451 (if (math-realp date)
1452 (nth 1 (calcFunc-tzconv (list 'date date) z1 z2))
1453 (calcFunc-unixtime (calcFunc-unixtime date z1) z2)))
1454
1455 (defun math-std-daylight-savings (date dt zone bump)
1456 "Standard North American daylight saving algorithm.
1457 Before 2007, this uses `math-std-daylight-savings-old', where
1458 daylight saving began on the first Sunday of April at 2 a.m.,
1459 and ended on the last Sunday of October at 2 a.m.
1460 As of 2007, this uses `math-std-daylight-savings-new', where
1461 daylight saving begins on the second Sunday of March at 2 a.m.,
1462 and ends on the first Sunday of November at 2 a.m."
1463 (if (< (car dt) 2007)
1464 (math-std-daylight-savings-old date dt zone bump)
1465 (math-std-daylight-savings-new date dt zone bump)))
1466
1467 (defun math-std-daylight-savings-new (date dt zone bump)
1468 "Standard North American daylight saving algorithm as of 2007.
1469 This implements the rules for the U.S. and Canada.
1470 Daylight saving begins on the second Sunday of March at 2 a.m.,
1471 and ends on the first Sunday of November at 2 a.m."
1472 (cond ((< (nth 1 dt) 3) 0)
1473 ((= (nth 1 dt) 3)
1474 (let ((sunday (math-prev-weekday-in-month date dt 14 0)))
1475 (cond ((< (nth 2 dt) sunday) 0)
1476 ((= (nth 2 dt) sunday)
1477 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1478 (t -1))))
1479 ((< (nth 1 dt) 11) -1)
1480 ((= (nth 1 dt) 11)
1481 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1482 (cond ((< (nth 2 dt) sunday) -1)
1483 ((= (nth 2 dt) sunday)
1484 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1485 (t 0))))
1486 (t 0)))
1487
1488 (defun math-std-daylight-savings-old (date dt zone bump)
1489 "Standard North American daylight saving algorithm before 2007.
1490 This implements the rules for the U.S. and Canada.
1491 Daylight saving begins on the first Sunday of April at 2 a.m.,
1492 and ends on the last Sunday of October at 2 a.m."
1493 (cond ((< (nth 1 dt) 4) 0)
1494 ((= (nth 1 dt) 4)
1495 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1496 (cond ((< (nth 2 dt) sunday) 0)
1497 ((= (nth 2 dt) sunday)
1498 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1499 (t -1))))
1500 ((< (nth 1 dt) 10) -1)
1501 ((= (nth 1 dt) 10)
1502 (let ((sunday (math-prev-weekday-in-month date dt 31 0)))
1503 (cond ((< (nth 2 dt) sunday) -1)
1504 ((= (nth 2 dt) sunday)
1505 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1506 (t 0))))
1507 (t 0)))
1508
1509 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1510 ;;; day of the given month.
1511 (defun math-prev-weekday-in-month (date dt day wday)
1512 (or day (setq day (nth 2 dt)))
1513 (if (> day (math-days-in-month (car dt) (nth 1 dt)))
1514 (setq day (math-days-in-month (car dt) (nth 1 dt))))
1515 (let ((zeroth (math-sub (math-floor date) (nth 2 dt))))
1516 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day))) zeroth)))
1517
1518 (defun calcFunc-pwday (date &optional day weekday)
1519 (if (eq (car-safe date) 'date)
1520 (setq date (nth 1 date)))
1521 (or (math-realp date)
1522 (math-reject-arg date 'datep))
1523 (if (math-messy-integerp day) (setq day (math-trunc day)))
1524 (or (integerp day) (math-reject-arg day 'fixnump))
1525 (if (= day 0) (setq day 31))
1526 (and (or (< day 7) (> day 31)) (math-reject-arg day 'range))
1527 (math-prev-weekday-in-month date (math-date-to-dt date) day (or weekday 0)))
1528
1529
1530 (defun calcFunc-newweek (date &optional weekday)
1531 (if (eq (car-safe date) 'date)
1532 (setq date (nth 1 date)))
1533 (or (math-realp date)
1534 (math-reject-arg date 'datep))
1535 (or weekday (setq weekday 0))
1536 (and (math-messy-integerp weekday) (setq weekday (math-trunc weekday)))
1537 (or (integerp weekday) (math-reject-arg weekday 'fixnump))
1538 (and (or (< weekday 0) (> weekday 6)) (math-reject-arg weekday 'range))
1539 (setq date (math-floor date))
1540 (list 'date (math-sub date (calcFunc-weekday (math-sub date weekday)))))
1541
1542 (defun calcFunc-newmonth (date &optional day)
1543 (or day (setq day 1))
1544 (and (math-messy-integerp day) (setq day (math-trunc day)))
1545 (or (integerp day) (math-reject-arg day 'fixnump))
1546 (and (or (< day 0) (> day 31)) (math-reject-arg day 'range))
1547 (let* ((dt (math-date-to-dt date))
1548 (dim (math-days-in-month (car dt) (nth 1 dt)))
1549 (julian (if calc-gregorian-switch
1550 (math-date-to-dt (math-sub
1551 (or (nth 3 calc-gregorian-switch)
1552 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))
1553 1)))))
1554 (if (or (= day 0) (> day dim))
1555 (setq day (1- dim))
1556 (setq day (1- day)))
1557 ;; Adjust if this occurs near the switch to the Gregorian calendar
1558 (if calc-gregorian-switch
1559 (cond
1560 ((and (math-dt-before-p (list (car dt) (nth 1 dt) 1) calc-gregorian-switch)
1561 (math-dt-before-p julian (list (car dt) (nth 1 dt) 1)))
1562 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the month
1563 (list 'date
1564 (math-dt-to-date (list (car calc-gregorian-switch)
1565 (nth 1 calc-gregorian-switch)
1566 (if (> (+ (nth 2 calc-gregorian-switch) day) dim)
1567 dim
1568 (+ (nth 2 calc-gregorian-switch) day))))))
1569 ((and (eq (car dt) (car calc-gregorian-switch))
1570 (= (nth 1 dt) (nth 1 calc-gregorian-switch)))
1571 ;; In this case, the switch to the Gregorian calendar occurs in the given month
1572 (if (< (+ (nth 2 julian) day) (nth 2 calc-gregorian-switch))
1573 ;; If the DAYth day occurs before the switch, use it
1574 (list 'date (math-dt-to-date (list (car dt) (nth 1 dt) (1+ day))))
1575 ;; Otherwise do some computations
1576 (let ((tm (+ day (- (nth 2 calc-gregorian-switch) (nth 2 julian)))))
1577 (list 'date (math-dt-to-date
1578 (list (car dt)
1579 (nth 1 dt)
1580 ;;
1581 (if (> tm dim) dim tm)))))))
1582 ((and (eq (car dt) (car julian))
1583 (= (nth 1 dt) (nth 1 julian)))
1584 ;; In this case, the current month is truncated because of the switch
1585 ;; to the Gregorian calendar
1586 (list 'date (math-dt-to-date
1587 (list (car dt)
1588 (nth 1 dt)
1589 (if (>= day (nth 2 julian))
1590 (nth 2 julian)
1591 (1+ day))))))
1592 (t
1593 ;; The default
1594 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) day))))
1595 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1)) day)))))
1596
1597 (defun calcFunc-newyear (date &optional day)
1598 (if (eq (car-safe date) 'date) (setq date (nth 1 date)))
1599 (or day (setq day 1))
1600 (and (math-messy-integerp day) (setq day (math-trunc day)))
1601 (or (integerp day) (math-reject-arg day 'fixnump))
1602 (let* ((dt (math-date-to-dt date))
1603 (gregbeg (if calc-gregorian-switch
1604 (or (nth 3 calc-gregorian-switch)
1605 (apply 'math-absolute-from-gregorian-dt calc-gregorian-switch))))
1606 (julianend (if calc-gregorian-switch (math-sub gregbeg 1)))
1607 (julian (if calc-gregorian-switch
1608 (math-date-to-dt julianend))))
1609 (if (and (>= day 0) (<= day 366))
1610 (let ((max (if (math-leap-year-p (car dt)) 366 365)))
1611 (if (or (= day 0) (> day max)) (setq day max))
1612 (if calc-gregorian-switch
1613 ;; Now to break this down into cases
1614 (cond
1615 ((and (math-dt-before-p (list (car dt) 1 1) calc-gregorian-switch)
1616 (math-dt-before-p julian (list (car dt) 1 1)))
1617 ;; In this case, CALC-GREGORIAN-SWITCH is the first day of the year
1618 (list 'date (math-min (math-add gregbeg (1- day))
1619 (math-dt-to-date (list (car calc-gregorian-switch) 12 31)))))
1620 ((eq (car dt) (car julian))
1621 ;; In this case, the switch to the Gregorian calendar occurs in the given year
1622 (if (Math-lessp (car julian) (car calc-gregorian-switch))
1623 ;; Here, the last Julian day is the last day of the year.
1624 (list 'date (math-min (math-add (math-dt-to-date (list (car dt) 1 1)) (1- day))
1625 julianend))
1626 ;; Otherwise, just make sure the date doesn't go past the end of the year
1627 (list 'date (math-min (math-add (math-dt-to-date (list (car dt) 1 1)) (1- day))
1628 (math-dt-to-date (list (car dt) 12 31))))))
1629 (t
1630 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1631 (1- day)))))
1632 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1633 (1- day)))))
1634 (if (and (>= day -12) (<= day -1))
1635 (if (and calc-gregorian-switch
1636 (math-dt-before-p (list (car dt) (- day) 1) calc-gregorian-switch)
1637 (math-dt-before-p julian (list (car dt) (- day) 1)))
1638 (list 'date gregbeg)
1639 (list 'date (math-dt-to-date (list (car dt) (- day) 1))))
1640 (math-reject-arg day 'range)))))
1641
1642 (defun calcFunc-incmonth (date &optional step)
1643 (or step (setq step 1))
1644 (and (math-messy-integerp step) (setq step (math-trunc step)))
1645 (or (math-integerp step) (math-reject-arg step 'integerp))
1646 (let* ((dt (math-date-to-dt date))
1647 (year (car dt))
1648 (month (math-add (1- (nth 1 dt)) step))
1649 (extra (calcFunc-idiv month 12))
1650 (day (nth 2 dt)))
1651 (setq month (1+ (math-sub month (math-mul extra 12)))
1652 year (math-add year extra)
1653 day (min day (math-days-in-month year month)))
1654 (and (math-posp (car dt)) (not (math-posp year))
1655 (setq year (math-sub year 1))) ; did we go past the year zero?
1656 (and (math-negp (car dt)) (not (math-negp year))
1657 (setq year (math-add year 1)))
1658 (list 'date (math-dt-to-date
1659 (cons year (cons month (cons day (cdr (cdr (cdr dt))))))))))
1660
1661 (defun calcFunc-incyear (date &optional step)
1662 (calcFunc-incmonth date (math-mul (or step 1) 12)))
1663
1664
1665
1666 (defun calcFunc-bsub (a b)
1667 (or (eq (car-safe a) 'date)
1668 (math-reject-arg a 'datep))
1669 (if (eq (car-safe b) 'date)
1670 (if (math-lessp (nth 1 a) (nth 1 b))
1671 (math-neg (calcFunc-bsub b a))
1672 (math-setup-holidays b)
1673 (let* ((da (math-to-business-day a))
1674 (db (math-to-business-day b)))
1675 (math-add (math-sub (car da) (car db))
1676 (if (and (cdr db) (not (cdr da))) 1 0))))
1677 (calcFunc-badd a (math-neg b))))
1678
1679 (defvar math-holidays-cache nil)
1680 (defvar math-holidays-cache-tag t)
1681 (defun calcFunc-badd (a b)
1682 (if (eq (car-safe b) 'date)
1683 (if (eq (car-safe a) 'date)
1684 (math-reject-arg nil "*Invalid combination in date arithmetic")
1685 (calcFunc-badd b a))
1686 (if (eq (car-safe a) 'date)
1687 (if (Math-realp b)
1688 (if (Math-zerop b)
1689 a
1690 (let* ((d (math-to-business-day a))
1691 (bb (math-add (car d)
1692 (if (and (cdr d) (Math-posp b))
1693 (math-sub b 1) b))))
1694 (or (math-from-business-day bb)
1695 (calcFunc-badd a b))))
1696 (if (eq (car-safe b) 'hms)
1697 (let ((hours (nth 7 math-holidays-cache)))
1698 (setq b (math-div (math-from-hms b 'deg) 24))
1699 (if hours
1700 (setq b (math-div b (cdr hours))))
1701 (calcFunc-badd a b))
1702 (math-reject-arg nil "*Invalid combination in date arithmetic")))
1703 (math-reject-arg a 'datep))))
1704
1705 (defun calcFunc-holiday (a)
1706 (if (cdr (math-to-business-day a)) 1 0))
1707
1708 ;;; Compute the number of business days since Jan 1, 1 AD.
1709
1710 (defun math-to-business-day (date &optional need-year)
1711 (if (eq (car-safe date) 'date)
1712 (setq date (nth 1 date)))
1713 (or (Math-realp date)
1714 (math-reject-arg date 'datep))
1715 (let* ((day (math-floor date))
1716 (time (math-sub date day))
1717 (dt (math-date-to-dt day))
1718 (delta 0)
1719 (holiday nil))
1720 (or (not need-year) (eq (car dt) need-year)
1721 (math-reject-arg (list 'date day) "*Generated holiday has wrong year"))
1722 (math-setup-holidays date)
1723 (let ((days (car math-holidays-cache)))
1724 (while (and (setq days (cdr days)) (< (car days) day))
1725 (setq delta (1+ delta)))
1726 (and days (= day (car days))
1727 (setq holiday t)))
1728 (let* ((weekdays (nth 3 math-holidays-cache))
1729 (weeks (1- (/ (+ day 6) 7)))
1730 (wkday (- day 1 (* weeks 7))))
1731 (setq delta (+ delta (* weeks (length weekdays))))
1732 (while (and weekdays (< (car weekdays) wkday))
1733 (setq weekdays (cdr weekdays)
1734 delta (1+ delta)))
1735 (and weekdays (eq wkday (car weekdays))
1736 (setq holiday t)))
1737 (let ((hours (nth 7 math-holidays-cache)))
1738 (if hours
1739 (progn
1740 (setq time (math-div (math-sub time (car hours)) (cdr hours)))
1741 (if (Math-lessp time 0) (setq time 0))
1742 (or (Math-lessp time 1)
1743 (setq time
1744 (math-sub 1
1745 (math-div 1 (math-mul 86400 (cdr hours)))))))))
1746 (cons (math-add (math-sub day delta) time) holiday)))
1747
1748
1749 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1750 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1751
1752 (defun math-from-business-day (num)
1753 (let* ((day (math-floor num))
1754 (time (math-sub num day)))
1755 (or (integerp day)
1756 (math-reject-arg nil "*Date is outside valid range"))
1757 (math-setup-holidays)
1758 (let ((days (nth 1 math-holidays-cache))
1759 (delta 0))
1760 (while (and (setq days (cdr days)) (< (car days) day))
1761 (setq delta (1+ delta)))
1762 (setq day (+ day delta)))
1763 (let* ((weekdays (nth 3 math-holidays-cache))
1764 (bweek (- 7 (length weekdays)))
1765 (weeks (1- (/ (+ day (1- bweek)) bweek)))
1766 (wkday (- day 1 (* weeks bweek)))
1767 (w 0))
1768 (setq day (+ day (* weeks (length weekdays))))
1769 (while (if (memq w weekdays)
1770 (setq day (1+ day))
1771 (> (setq wkday (1- wkday)) 0))
1772 (setq w (1+ w)))
1773 (let ((hours (nth 7 math-holidays-cache)))
1774 (if hours
1775 (setq time (math-add (math-mul time (cdr hours)) (car hours)))))
1776 (and (not (math-setup-holidays day))
1777 (list 'date (math-add day time))))))
1778
1779 ;; The variable math-sh-year is local to math-setup-holidays
1780 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1781 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1782 (defvar math-sh-year)
1783
1784 (defun math-setup-holidays (&optional date)
1785 (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag)
1786 (let ((h (calc-var-value 'var-Holidays))
1787 (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3)
1788 (thu . 4) (fri . 5) (sat . 6) ))
1789 (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil))
1790 (or (math-vectorp h)
1791 (math-reject-arg h "*Holidays variable must be a vector"))
1792 (while (setq h (cdr h))
1793 (cond ((or (and (eq (car-safe (car h)) 'date)
1794 (integerp (nth 1 (car h))))
1795 (and (eq (car-safe (car h)) 'intv)
1796 (eq (car-safe (nth 2 (car h))) 'date))
1797 (eq (car-safe (car h)) 'vec))
1798 (setq days (cons (car h) days)))
1799 ((and (eq (car-safe (car h)) 'var)
1800 (assq (nth 1 (car h)) wdnames))
1801 (setq weekdays (cons (cdr (assq (nth 1 (car h)) wdnames))
1802 weekdays)))
1803 ((and (eq (car-safe (car h)) 'intv)
1804 (eq (car-safe (nth 2 (car h))) 'hms)
1805 (eq (car-safe (nth 3 (car h))) 'hms))
1806 (if hours
1807 (math-reject-arg
1808 (car h) "*Only one hours interval allowed in Holidays"))
1809 (setq hours (math-div (car h) '(hms 24 0 0)))
1810 (if (or (Math-lessp (nth 2 hours) 0)
1811 (Math-lessp 1 (nth 3 hours)))
1812 (math-reject-arg
1813 (car h) "*Hours interval out of range"))
1814 (setq hours (cons (nth 2 hours)
1815 (math-sub (nth 3 hours) (nth 2 hours))))
1816 (if (Math-zerop (cdr hours))
1817 (math-reject-arg
1818 (car h) "*Degenerate hours interval")))
1819 ((or (and (eq (car-safe (car h)) 'intv)
1820 (Math-integerp (nth 2 (car h)))
1821 (Math-integerp (nth 3 (car h))))
1822 (and (integerp (car h))
1823 (> (car h) 1900) (< (car h) 2100)))
1824 (if limit
1825 (math-reject-arg
1826 (car h) "*Only one limit allowed in Holidays"))
1827 (setq limit (calcFunc-vint (car h) '(intv 3 1 2737)))
1828 (if (equal limit '(vec))
1829 (math-reject-arg (car h) "*Limit is out of range")))
1830 ((or (math-expr-contains (car h) '(var y var-y))
1831 (math-expr-contains (car h) '(var m var-m)))
1832 (setq exprs (cons (car h) exprs)))
1833 (t (math-reject-arg
1834 (car h) "*Holidays must contain a vector of holidays"))))
1835 (if (= (length weekdays) 7)
1836 (math-reject-arg nil "*Too many weekend days"))
1837 (setq math-holidays-cache (list (list -1) ; 0: days list
1838 (list -1) ; 1: inverse-days list
1839 nil ; 2: exprs
1840 (sort weekdays '<)
1841 (or limit '(intv 3 1 2737))
1842 nil ; 5: (lo.hi) expanded years
1843 (cons exprs days)
1844 hours) ; 7: business hours
1845 math-holidays-cache-tag (calc-var-value 'var-Holidays))))
1846 (if date
1847 (let ((year (calcFunc-year date))
1848 (limits (nth 5 math-holidays-cache))
1849 (done nil))
1850 (or (eq (calcFunc-in year (nth 4 math-holidays-cache)) 1)
1851 (progn
1852 (or (eq (car-safe date) 'date) (setq date (list 'date date)))
1853 (math-reject-arg date "*Date is outside valid range")))
1854 (unwind-protect
1855 (let ((days (nth 6 math-holidays-cache)))
1856 (if days
1857 (let ((math-sh-year nil)) ; see below
1858 (setcar (nthcdr 6 math-holidays-cache) nil)
1859 (math-setup-add-holidays (cons 'vec (cdr days)))
1860 (setcar (nthcdr 2 math-holidays-cache) (car days))))
1861 (cond ((not (nth 2 math-holidays-cache))
1862 (setq done t)
1863 nil)
1864 ((not limits)
1865 (setcar (nthcdr 5 math-holidays-cache) (cons year year))
1866 (math-setup-year-holidays year)
1867 (setq done t))
1868 ((< year (car limits))
1869 (message "Computing holidays, %d .. %d"
1870 year (1- (car limits)))
1871 (calc-set-command-flag 'clear-message)
1872 (while (< year (car limits))
1873 (setcar limits (1- (car limits)))
1874 (math-setup-year-holidays (car limits)))
1875 (setq done t))
1876 ((> year (cdr limits))
1877 (message "Computing holidays, %d .. %d"
1878 (1+ (cdr limits)) year)
1879 (calc-set-command-flag 'clear-message)
1880 (while (> year (cdr limits))
1881 (setcdr limits (1+ (cdr limits)))
1882 (math-setup-year-holidays (cdr limits)))
1883 (setq done t))
1884 (t
1885 (setq done t)
1886 nil)))
1887 (or done (setq math-holidays-cache-tag t))))))
1888
1889 (defun math-setup-year-holidays (math-sh-year)
1890 (let ((exprs (nth 2 math-holidays-cache)))
1891 (while exprs
1892 (let* ((var-y math-sh-year)
1893 (var-m nil)
1894 (expr (math-evaluate-expr (car exprs))))
1895 (if (math-expr-contains expr '(var m var-m))
1896 (let ((var-m 0))
1897 (while (<= (setq var-m (1+ var-m)) 12)
1898 (math-setup-add-holidays (math-evaluate-expr expr))))
1899 (math-setup-add-holidays expr)))
1900 (setq exprs (cdr exprs)))))
1901
1902 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
1903 (cond ((eq (car-safe days) 'vec)
1904 (while (setq days (cdr days))
1905 (math-setup-add-holidays (car days))))
1906 ((eq (car-safe days) 'intv)
1907 (let ((day (math-ceiling (nth 2 days))))
1908 (or (eq (calcFunc-in day days) 1)
1909 (setq day (math-add day 1)))
1910 (while (eq (calcFunc-in day days) 1)
1911 (math-setup-add-holidays day)
1912 (setq day (math-add day 1)))))
1913 ((eq (car-safe days) 'date)
1914 (math-setup-add-holidays (nth 1 days)))
1915 ((eq days 0))
1916 ((integerp days)
1917 (let ((b (math-to-business-day days math-sh-year)))
1918 (or (cdr b) ; don't register holidays twice!
1919 (let ((prev (car math-holidays-cache))
1920 (iprev (nth 1 math-holidays-cache)))
1921 (while (and (cdr prev) (< (nth 1 prev) days))
1922 (setq prev (cdr prev) iprev (cdr iprev)))
1923 (setcdr prev (cons days (cdr prev)))
1924 (setcdr iprev (cons (car b) (cdr iprev)))
1925 (while (setq iprev (cdr iprev))
1926 (setcar iprev (1- (car iprev))))))))
1927 ((Math-realp days)
1928 (math-reject-arg (list 'date days) "*Invalid holiday value"))
1929 (t
1930 (math-reject-arg days "*Holiday formula failed to evaluate"))))
1931
1932
1933
1934
1935 ;;;; Error forms.
1936
1937 ;;; Build a standard deviation form. [X X X]
1938 (defun math-make-sdev (x sigma)
1939 (if (memq (car-safe x) '(date mod sdev intv vec))
1940 (math-reject-arg x 'realp))
1941 (if (memq (car-safe sigma) '(date mod sdev intv vec))
1942 (math-reject-arg sigma 'realp))
1943 (if (or (Math-negp sigma) (memq (car-safe sigma) '(cplx polar)))
1944 (setq sigma (math-abs sigma)))
1945 (if (and (Math-zerop sigma) (Math-scalarp x))
1946 x
1947 (list 'sdev x sigma)))
1948 (defun calcFunc-sdev (x sigma)
1949 (math-make-sdev x sigma))
1950
1951
1952
1953 ;;;; Modulo forms.
1954
1955 (defun math-normalize-mod (a)
1956 (let ((n (math-normalize (nth 1 a)))
1957 (m (math-normalize (nth 2 a))))
1958 (if (and (math-anglep n) (math-anglep m) (math-posp m))
1959 (math-make-mod n m)
1960 (math-normalize (list 'calcFunc-makemod n m)))))
1961
1962 ;;; Build a modulo form. [N R R]
1963 (defun math-make-mod (n m)
1964 (setq calc-previous-modulo m)
1965 (and n
1966 (cond ((not (Math-anglep m))
1967 (math-reject-arg m 'anglep))
1968 ((not (math-posp m))
1969 (math-reject-arg m 'posp))
1970 ((Math-anglep n)
1971 (if (or (Math-negp n)
1972 (not (Math-lessp n m)))
1973 (list 'mod (math-mod n m) m)
1974 (list 'mod n m)))
1975 ((memq (car n) '(+ - / vec neg))
1976 (math-normalize
1977 (cons (car n)
1978 (mapcar (function (lambda (x) (math-make-mod x m)))
1979 (cdr n)))))
1980 ((and (eq (car n) '*) (Math-anglep (nth 1 n)))
1981 (math-mul (math-make-mod (nth 1 n) m) (nth 2 n)))
1982 ((memq (car n) '(* ^ var calcFunc-subscr))
1983 (math-mul (math-make-mod 1 m) n))
1984 (t (math-reject-arg n 'anglep)))))
1985 (defun calcFunc-makemod (n m)
1986 (math-make-mod n m))
1987
1988
1989
1990 ;;;; Interval forms.
1991
1992 ;;; Build an interval form. [X S X X]
1993 (defun math-make-intv (mask lo hi)
1994 (if (memq (car-safe lo) '(cplx polar mod sdev intv vec))
1995 (math-reject-arg lo 'realp))
1996 (if (memq (car-safe hi) '(cplx polar mod sdev intv vec))
1997 (math-reject-arg hi 'realp))
1998 (or (eq (eq (car-safe lo) 'date) (eq (car-safe hi) 'date))
1999 (math-reject-arg (if (eq (car-safe lo) 'date) hi lo) 'datep))
2000 (if (and (or (Math-realp lo) (eq (car lo) 'date))
2001 (or (Math-realp hi) (eq (car hi) 'date)))
2002 (let ((cmp (math-compare lo hi)))
2003 (if (= cmp 0)
2004 (if (= mask 3)
2005 lo
2006 (list 'intv mask lo hi))
2007 (if (> cmp 0)
2008 (if (= mask 3)
2009 (list 'intv 2 lo lo)
2010 (list 'intv mask lo lo))
2011 (list 'intv mask lo hi))))
2012 (list 'intv mask lo hi)))
2013 (defun calcFunc-intv (mask lo hi)
2014 (if (math-messy-integerp mask) (setq mask (math-trunc mask)))
2015 (or (natnump mask) (math-reject-arg mask 'fixnatnump))
2016 (or (<= mask 3) (math-reject-arg mask 'range))
2017 (math-make-intv mask lo hi))
2018
2019 (defun math-sort-intv (mask lo hi)
2020 (if (Math-lessp hi lo)
2021 (math-make-intv (aref [0 2 1 3] mask) hi lo)
2022 (math-make-intv mask lo hi)))
2023
2024
2025
2026
2027 (defun math-combine-intervals (a am b bm c cm d dm)
2028 (let (res)
2029 (if (= (setq res (math-compare a c)) 1)
2030 (setq a c am cm)
2031 (if (= res 0)
2032 (setq am (or am cm))))
2033 (if (= (setq res (math-compare b d)) -1)
2034 (setq b d bm dm)
2035 (if (= res 0)
2036 (setq bm (or bm dm))))
2037 (math-make-intv (+ (if am 2 0) (if bm 1 0)) a b)))
2038
2039
2040 (defun math-div-mod (a b m) ; [R R R R] (Returns nil if no solution)
2041 (and (Math-integerp a) (Math-integerp b) (Math-integerp m)
2042 (let ((u1 1) (u3 b) (v1 0) (v3 m))
2043 (while (not (eq v3 0)) ; See Knuth sec 4.5.2, exercise 15
2044 (let* ((q (math-idivmod u3 v3))
2045 (t1 (math-sub u1 (math-mul v1 (car q)))))
2046 (setq u1 v1 u3 v3 v1 t1 v3 (cdr q))))
2047 (let ((q (math-idivmod a u3)))
2048 (and (eq (cdr q) 0)
2049 (math-mod (math-mul (car q) u1) m))))))
2050
2051 (defun math-mod-intv (a b)
2052 (let* ((q1 (math-floor (math-div (nth 2 a) b)))
2053 (q2 (math-floor (math-div (nth 3 a) b)))
2054 (m1 (math-sub (nth 2 a) (math-mul q1 b)))
2055 (m2 (math-sub (nth 3 a) (math-mul q2 b))))
2056 (cond ((equal q1 q2)
2057 (math-sort-intv (nth 1 a) m1 m2))
2058 ((and (math-equal-int (math-sub q2 q1) 1)
2059 (math-zerop m2)
2060 (memq (nth 1 a) '(0 2)))
2061 (math-make-intv (nth 1 a) m1 b))
2062 (t
2063 (math-make-intv 2 0 b)))))
2064
2065 ;; The variables math-exp-str and math-exp-pos are local to
2066 ;; math-read-exprs in math-aent.el, but are used by
2067 ;; math-read-angle-brackets, which is called (indirectly) by
2068 ;; math-read-exprs.
2069 (defvar math-exp-str)
2070 (defvar math-exp-pos)
2071
2072 (defun math-read-angle-brackets ()
2073 (let* ((last (or (math-check-for-commas t) (length math-exp-str)))
2074 (str (substring math-exp-str math-exp-pos last))
2075 (res
2076 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str)
2077 (let ((str1 (substring str 0 (1- (match-end 0))))
2078 (str2 (substring str (match-end 0)))
2079 (calc-hashes-used 0))
2080 (setq str1 (math-read-expr (concat "[" str1 "]")))
2081 (if (eq (car-safe str1) 'error)
2082 str1
2083 (setq str2 (math-read-expr str2))
2084 (if (eq (car-safe str2) 'error)
2085 str2
2086 (append '(calcFunc-lambda) (cdr str1) (list str2)))))
2087 (if (string-match "#" str)
2088 (let ((calc-hashes-used 0))
2089 (and (setq str (math-read-expr str))
2090 (if (eq (car-safe str) 'error)
2091 str
2092 (append '(calcFunc-lambda)
2093 (calc-invent-args calc-hashes-used)
2094 (list str)))))
2095 (math-parse-date str)))))
2096 (if (stringp res)
2097 (throw 'syntax res))
2098 (if (eq (car-safe res) 'error)
2099 (throw 'syntax (nth 2 res)))
2100 (setq math-exp-pos (1+ last))
2101 (math-read-token)
2102 res))
2103
2104 (provide 'calc-forms)
2105
2106 ;;; calc-forms.el ends here