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