Merge from emacs--rel--22
[bpt/emacs.git] / 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 3, 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 (defconst math-julian-date-beginning '(float 17214235 -1)
548 "The beginning of the Julian calendar,
549 as measured in the number of days before January 1 of the year 1AD.")
550
551 (defconst math-julian-date-beginning-int 1721424
552 "The beginning of the Julian calendar,
553 as measured in the integer number of days before January 1 of the year 1AD.")
554
555 (defun math-format-date-part (x)
556 (cond ((stringp x)
557 x)
558 ((listp x)
559 (if (math-integerp math-fd-date)
560 ""
561 (apply 'concat (mapcar 'math-format-date-part x))))
562 ((eq x 'X)
563 "")
564 ((eq x 'N)
565 (math-format-number math-fd-date))
566 ((eq x 'n)
567 (math-format-number (math-floor math-fd-date)))
568 ((eq x 'J)
569 (math-format-number
570 (math-add math-fd-date math-julian-date-beginning)))
571 ((eq x 'j)
572 (math-format-number (math-add
573 (math-floor math-fd-date)
574 math-julian-date-beginning-int)))
575 ((eq x 'U)
576 (math-format-number (nth 1 (math-date-parts math-fd-date 719164))))
577 ((progn
578 (or math-fd-dt
579 (progn
580 (setq math-fd-dt (math-date-to-dt math-fd-date)
581 math-fd-year (car math-fd-dt)
582 math-fd-month (nth 1 math-fd-dt)
583 math-fd-day (nth 2 math-fd-dt)
584 math-fd-weekday (math-mod
585 (math-add (math-floor math-fd-date) 6) 7)
586 math-fd-hour (nth 3 math-fd-dt)
587 math-fd-minute (nth 4 math-fd-dt)
588 math-fd-second (nth 5 math-fd-dt))
589 (and (memq 'b calc-date-format)
590 (math-negp math-fd-year)
591 (setq math-fd-year (math-neg math-fd-year)
592 math-fd-bc-flag t))))
593 (memq x '(Y YY BY)))
594 (if (and (integerp math-fd-year) (> math-fd-year 1940) (< math-fd-year 2040))
595 (format (cond ((eq x 'YY) "%02d")
596 ((eq x 'BYY) "%2d")
597 (t "%d"))
598 (% math-fd-year 100))
599 (if (and (natnump math-fd-year) (< math-fd-year 100))
600 (format "+%d" math-fd-year)
601 (math-format-number math-fd-year))))
602 ((eq x 'YYY)
603 (math-format-number math-fd-year))
604 ((eq x 'YYYY)
605 (if (and (natnump math-fd-year) (< math-fd-year 100))
606 (format "+%d" math-fd-year)
607 (math-format-number math-fd-year)))
608 ((eq x 'b) "")
609 ((eq x 'aa)
610 (and (not math-fd-bc-flag) "ad"))
611 ((eq x 'AA)
612 (and (not math-fd-bc-flag) "AD"))
613 ((eq x 'aaa)
614 (and (not math-fd-bc-flag) "ad "))
615 ((eq x 'AAA)
616 (and (not math-fd-bc-flag) "AD "))
617 ((eq x 'aaaa)
618 (and (not math-fd-bc-flag) "a.d."))
619 ((eq x 'AAAA)
620 (and (not math-fd-bc-flag) "A.D."))
621 ((eq x 'bb)
622 (and math-fd-bc-flag "bc"))
623 ((eq x 'BB)
624 (and math-fd-bc-flag "BC"))
625 ((eq x 'bbb)
626 (and math-fd-bc-flag " bc"))
627 ((eq x 'BBB)
628 (and math-fd-bc-flag " BC"))
629 ((eq x 'bbbb)
630 (and math-fd-bc-flag "b.c."))
631 ((eq x 'BBBB)
632 (and math-fd-bc-flag "B.C."))
633 ((eq x 'M)
634 (format "%d" math-fd-month))
635 ((eq x 'MM)
636 (format "%02d" math-fd-month))
637 ((eq x 'BM)
638 (format "%2d" math-fd-month))
639 ((eq x 'mmm)
640 (downcase (nth (1- math-fd-month) math-short-month-names)))
641 ((eq x 'Mmm)
642 (nth (1- math-fd-month) math-short-month-names))
643 ((eq x 'MMM)
644 (upcase (nth (1- math-fd-month) math-short-month-names)))
645 ((eq x 'Mmmm)
646 (nth (1- math-fd-month) math-long-month-names))
647 ((eq x 'MMMM)
648 (upcase (nth (1- math-fd-month) math-long-month-names)))
649 ((eq x 'D)
650 (format "%d" math-fd-day))
651 ((eq x 'DD)
652 (format "%02d" math-fd-day))
653 ((eq x 'BD)
654 (format "%2d" math-fd-day))
655 ((eq x 'W)
656 (format "%d" math-fd-weekday))
657 ((eq x 'www)
658 (downcase (nth math-fd-weekday math-short-weekday-names)))
659 ((eq x 'Www)
660 (nth math-fd-weekday math-short-weekday-names))
661 ((eq x 'WWW)
662 (upcase (nth math-fd-weekday math-short-weekday-names)))
663 ((eq x 'Wwww)
664 (nth math-fd-weekday math-long-weekday-names))
665 ((eq x 'WWWW)
666 (upcase (nth math-fd-weekday math-long-weekday-names)))
667 ((eq x 'd)
668 (format "%d" (math-day-number math-fd-year math-fd-month math-fd-day)))
669 ((eq x 'ddd)
670 (format "%03d" (math-day-number math-fd-year math-fd-month math-fd-day)))
671 ((eq x 'bdd)
672 (format "%3d" (math-day-number math-fd-year math-fd-month math-fd-day)))
673 ((eq x 'h)
674 (and math-fd-hour (format "%d" math-fd-hour)))
675 ((eq x 'hh)
676 (and math-fd-hour (format "%02d" math-fd-hour)))
677 ((eq x 'bh)
678 (and math-fd-hour (format "%2d" math-fd-hour)))
679 ((eq x 'H)
680 (and math-fd-hour (format "%d" (1+ (% (+ math-fd-hour 11) 12)))))
681 ((eq x 'HH)
682 (and math-fd-hour (format "%02d" (1+ (% (+ math-fd-hour 11) 12)))))
683 ((eq x 'BH)
684 (and math-fd-hour (format "%2d" (1+ (% (+ math-fd-hour 11) 12)))))
685 ((eq x 'p)
686 (and math-fd-hour (if (< math-fd-hour 12) "a" "p")))
687 ((eq x 'P)
688 (and math-fd-hour (if (< math-fd-hour 12) "A" "P")))
689 ((eq x 'pp)
690 (and math-fd-hour (if (< math-fd-hour 12) "am" "pm")))
691 ((eq x 'PP)
692 (and math-fd-hour (if (< math-fd-hour 12) "AM" "PM")))
693 ((eq x 'pppp)
694 (and math-fd-hour (if (< math-fd-hour 12) "a.m." "p.m.")))
695 ((eq x 'PPPP)
696 (and math-fd-hour (if (< math-fd-hour 12) "A.M." "P.M.")))
697 ((eq x 'm)
698 (and math-fd-minute (format "%d" math-fd-minute)))
699 ((eq x 'mm)
700 (and math-fd-minute (format "%02d" math-fd-minute)))
701 ((eq x 'bm)
702 (and math-fd-minute (format "%2d" math-fd-minute)))
703 ((eq x 'C)
704 (and math-fd-second (not (math-zerop math-fd-second))
705 ":"))
706 ((memq x '(s ss bs SS BS))
707 (and math-fd-second
708 (not (and (memq x '(SS BS)) (math-zerop math-fd-second)))
709 (if (integerp math-fd-second)
710 (format (cond ((memq x '(ss SS)) "%02d")
711 ((memq x '(bs BS)) "%2d")
712 (t "%d"))
713 math-fd-second)
714 (concat (if (Math-lessp math-fd-second 10)
715 (cond ((memq x '(ss SS)) "0")
716 ((memq x '(bs BS)) " ")
717 (t ""))
718 "")
719 (let ((calc-float-format
720 (list 'fix (min (- 12 calc-internal-prec)
721 0))))
722 (math-format-number math-fd-second))))))))
723
724 ;; The variable math-pd-str is local to math-parse-date and
725 ;; math-parse-standard-date, but is used by math-parse-date-word,
726 ;; which is called by math-parse-date and math-parse-standard-date.
727 (defvar math-pd-str)
728
729 (defun math-parse-date (math-pd-str)
730 (catch 'syntax
731 (or (math-parse-standard-date math-pd-str t)
732 (math-parse-standard-date math-pd-str nil)
733 (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" math-pd-str)
734 (list 'date (math-read-number (math-match-substring math-pd-str 1))))
735 (let ((case-fold-search t)
736 (year nil) (month nil) (day nil) (weekday nil)
737 (hour nil) (minute nil) (second nil) (bc-flag nil)
738 (a nil) (b nil) (c nil) (bigyear nil) temp)
739
740 ;; Extract the time, if any.
741 (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)
742 (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]\\>\\|[ap]m\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" math-pd-str))
743 (let ((ampm (math-match-substring math-pd-str 6)))
744 (setq hour (string-to-number (math-match-substring math-pd-str 1))
745 minute (math-match-substring math-pd-str 2)
746 second (math-match-substring math-pd-str 4)
747 math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
748 (substring math-pd-str (match-end 0))))
749 (if (equal minute "")
750 (setq minute 0)
751 (setq minute (string-to-number minute)))
752 (if (equal second "")
753 (setq second 0)
754 (setq second (math-read-number second)))
755 (if (equal ampm "")
756 (if (> hour 23)
757 (throw 'syntax "Hour value out of range"))
758 (setq ampm (upcase (aref ampm 0)))
759 (if (memq ampm '(?N ?M))
760 (if (and (= hour 12) (= minute 0) (eq second 0))
761 (if (eq ampm ?M) (setq hour 0))
762 (throw 'syntax
763 "Time must be 12:00:00 in this context"))
764 (if (or (= hour 0) (> hour 12))
765 (throw 'syntax "Hour value out of range"))
766 (if (eq (= ampm ?A) (= hour 12))
767 (setq hour (% (+ hour 12) 24)))))))
768
769 ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
770 (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" math-pd-str)
771 (progn
772 (setq math-pd-str (copy-sequence math-pd-str))
773 (aset math-pd-str (match-beginning 1) ?\/)))
774
775 ;; Extract obvious month or weekday names.
776 (if (string-match "[a-zA-Z]" math-pd-str)
777 (progn
778 (setq month (math-parse-date-word math-long-month-names))
779 (setq weekday (math-parse-date-word math-long-weekday-names))
780 (or month (setq month
781 (math-parse-date-word math-short-month-names)))
782 (or weekday (math-parse-date-word math-short-weekday-names))
783 (or hour
784 (if (setq temp (math-parse-date-word
785 '( "noon" "midnight" "mid" )))
786 (setq hour (if (= temp 1) 12 0) minute 0 second 0)))
787 (or (math-parse-date-word '( "ad" "a.d." ))
788 (if (math-parse-date-word '( "bc" "b.c." ))
789 (setq bc-flag t)))
790 (if (string-match "[a-zA-Z]+" math-pd-str)
791 (throw 'syntax (format "Bad word in date: \"%s\""
792 (math-match-substring math-pd-str 0))))))
793
794 ;; If there is a huge number other than the year, ignore it.
795 (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" math-pd-str)
796 (setq temp (concat (substring math-pd-str 0 (match-beginning 0))
797 (substring math-pd-str (match-end 0))))
798 (string-match
799 "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp))
800 (setq math-pd-str temp))
801
802 ;; If there is a number with a sign or a large number, it is a year.
803 (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" math-pd-str)
804 (string-match "\\(0*[1-9][0-9][0-9]+\\)" math-pd-str))
805 (setq year (math-match-substring math-pd-str 1)
806 math-pd-str (concat (substring math-pd-str 0 (match-beginning 1))
807 (substring math-pd-str (match-end 1)))
808 year (math-read-number year)
809 bigyear t))
810
811 ;; Collect remaining numbers.
812 (setq temp 0)
813 (while (string-match "[0-9]+" math-pd-str temp)
814 (and c (throw 'syntax "Too many numbers in date"))
815 (setq c (string-to-number (math-match-substring math-pd-str 0)))
816 (or b (setq b c c nil))
817 (or a (setq a b b nil))
818 (setq temp (match-end 0)))
819
820 ;; Check that we have the right amount of information.
821 (setq temp (+ (if year 1 0) (if month 1 0) (if day 1 0)
822 (if a 1 0) (if b 1 0) (if c 1 0)))
823 (if (> temp 3)
824 (throw 'syntax "Too many numbers in date")
825 (if (or (< temp 2) (and year (= temp 2)))
826 (throw 'syntax "Not enough numbers in date")
827 (if (= temp 2) ; if year omitted, assume current year
828 (setq year (math-this-year)))))
829
830 ;; A large number must be a year.
831 (or year
832 (if (and a (or (> a 31) (< a 1)))
833 (setq year a a b b c c nil)
834 (if (and b (or (> b 31) (< b 1)))
835 (setq year b b c c nil)
836 (if (and c (or (> c 31) (< c 1)))
837 (setq year c c nil)))))
838
839 ;; A medium-large number must be a day.
840 (if year
841 (if (and a (> a 12))
842 (setq day a a b b c c nil)
843 (if (and b (> b 12))
844 (setq day b b c c nil)
845 (if (and c (> c 12))
846 (setq day c c nil)))))
847
848 ;; We may know enough to sort it out now.
849 (if (and year day)
850 (or month (setq month a))
851 (if (and year month)
852 (setq day a)
853
854 ;; Interpret order of numbers as same as for display format.
855 (setq temp calc-date-format)
856 (while temp
857 (cond ((not (symbolp (car temp))))
858 ((memq (car temp) '(Y YY BY YYY YYYY))
859 (or year (setq year a a b b c)))
860 ((memq (car temp) '(M MM BM mmm Mmm Mmmm MMM MMMM))
861 (or month (setq month a a b b c)))
862 ((memq (car temp) '(D DD BD))
863 (or day (setq day a a b b c))))
864 (setq temp (cdr temp)))
865
866 ;; If display format was not complete, assume American style.
867 (or month (setq month a a b b c))
868 (or day (setq day a a b b c))
869 (or year (setq year a a b b c))))
870
871 (if bc-flag
872 (setq year (math-neg (math-abs year))))
873
874 (math-parse-date-validate year bigyear month day
875 hour minute second)))))
876
877 (defun math-parse-date-validate (year bigyear month day hour minute second)
878 (and (not bigyear) (natnump year) (< year 100)
879 (setq year (+ year (if (< year 40) 2000 1900))))
880 (if (eq year 0)
881 (throw 'syntax "Year value is out of range"))
882 (if (or (< month 1) (> month 12))
883 (throw 'syntax "Month value is out of range"))
884 (if (or (< day 1) (> day (math-days-in-month year month)))
885 (throw 'syntax "Day value is out of range"))
886 (and hour
887 (progn
888 (if (or (< hour 0) (> hour 23))
889 (throw 'syntax "Hour value is out of range"))
890 (if (or (< minute 0) (> minute 59))
891 (throw 'syntax "Minute value is out of range"))
892 (if (or (math-negp second) (not (Math-lessp second 60)))
893 (throw 'syntax "Seconds value is out of range"))))
894 (list 'date (math-dt-to-date (append (list year month day)
895 (and hour (list hour minute second))))))
896
897 (defun math-parse-date-word (names &optional front)
898 (let ((n 1))
899 (while (and names (not (string-match (if (equal (car names) "Sep")
900 "Sept?"
901 (regexp-quote (car names)))
902 math-pd-str)))
903 (setq names (cdr names)
904 n (1+ n)))
905 (and names
906 (or (not front) (= (match-beginning 0) 0))
907 (progn
908 (setq math-pd-str (concat (substring math-pd-str 0 (match-beginning 0))
909 (if front "" " ")
910 (substring math-pd-str (match-end 0))))
911 n))))
912
913 (defun math-parse-standard-date (math-pd-str with-time)
914 (let ((case-fold-search t)
915 (okay t) num
916 (fmt calc-date-format) this next (gnext nil)
917 (year nil) (month nil) (day nil) (bigyear nil) (yearday nil)
918 (hour nil) (minute nil) (second nil) (bc-flag nil))
919 (while (and fmt okay)
920 (setq this (car fmt)
921 fmt (setq fmt (or (cdr fmt)
922 (prog1
923 gnext
924 (setq gnext nil))))
925 next (car fmt))
926 (if (consp next) (setq next (car next)))
927 (or (cond ((listp this)
928 (or (not with-time)
929 (not this)
930 (setq gnext fmt
931 fmt this)))
932 ((stringp this)
933 (if (and (<= (length this) (length math-pd-str))
934 (equal this
935 (substring math-pd-str 0 (length this))))
936 (setq math-pd-str (substring math-pd-str (length this)))))
937 ((eq this 'X)
938 t)
939 ((memq this '(n N j J))
940 (and (string-match "\\`[-+]?[0-9.]+\\([eE][-+]?[0-9]+\\)?" math-pd-str)
941 (setq num (math-match-substring math-pd-str 0)
942 math-pd-str (substring math-pd-str (match-end 0))
943 num (math-date-to-dt (math-read-number num))
944 num (math-sub num
945 (if (memq this '(n N))
946 0
947 (if (or (eq this 'j)
948 (math-integerp num))
949 math-julian-date-beginning-int
950 math-julian-date-beginning)))
951 hour (or (nth 3 num) hour)
952 minute (or (nth 4 num) minute)
953 second (or (nth 5 num) second)
954 year (car num)
955 month (nth 1 num)
956 day (nth 2 num))))
957 ((eq this 'U)
958 (and (string-match "\\`[-+]?[0-9]+" math-pd-str)
959 (setq num (math-match-substring math-pd-str 0)
960 math-pd-str (substring math-pd-str (match-end 0))
961 num (math-date-to-dt
962 (math-add 719164
963 (math-div (math-read-number num)
964 '(float 864 2))))
965 hour (nth 3 num)
966 minute (nth 4 num)
967 second (nth 5 num)
968 year (car num)
969 month (nth 1 num)
970 day (nth 2 num))))
971 ((memq this '(mmm Mmm MMM))
972 (setq month (math-parse-date-word math-short-month-names t)))
973 ((memq this '(Mmmm MMMM))
974 (setq month (math-parse-date-word math-long-month-names t)))
975 ((memq this '(www Www WWW))
976 (math-parse-date-word math-short-weekday-names t))
977 ((memq this '(Wwww WWWW))
978 (math-parse-date-word math-long-weekday-names t))
979 ((memq this '(p P))
980 (if (string-match "\\`a" math-pd-str)
981 (setq hour (if (= hour 12) 0 hour)
982 math-pd-str (substring math-pd-str 1))
983 (if (string-match "\\`p" math-pd-str)
984 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
985 math-pd-str (substring math-pd-str 1)))))
986 ((memq this '(pp PP pppp PPPP))
987 (if (string-match "\\`am\\|a\\.m\\." math-pd-str)
988 (setq hour (if (= hour 12) 0 hour)
989 math-pd-str (substring math-pd-str (match-end 0)))
990 (if (string-match "\\`pm\\|p\\.m\\." math-pd-str)
991 (setq hour (if (= hour 12) 12 (% (+ hour 12) 24))
992 math-pd-str (substring math-pd-str (match-end 0))))))
993 ((memq this '(Y YY BY YYY YYYY))
994 (and (if (memq next '(MM DD ddd hh HH mm ss SS))
995 (if (memq this '(Y YY BYY))
996 (string-match "\\` *[0-9][0-9]" math-pd-str)
997 (string-match "\\`[0-9][0-9][0-9][0-9]" math-pd-str))
998 (string-match "\\`[-+]?[0-9]+" math-pd-str))
999 (setq year (math-match-substring math-pd-str 0)
1000 bigyear (or (eq this 'YYY)
1001 (memq (aref math-pd-str 0) '(?\+ ?\-)))
1002 math-pd-str (substring math-pd-str (match-end 0))
1003 year (math-read-number year))))
1004 ((eq this 'b)
1005 t)
1006 ((memq this '(aa AA aaaa AAAA))
1007 (if (string-match "\\` *\\(ad\\|a\\.d\\.\\)" math-pd-str)
1008 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1009 ((memq this '(aaa AAA))
1010 (if (string-match "\\` *ad *" math-pd-str)
1011 (setq math-pd-str (substring math-pd-str (match-end 0)))))
1012 ((memq this '(bb BB bbb BBB bbbb BBBB))
1013 (if (string-match "\\` *\\(bc\\|b\\.c\\.\\)" math-pd-str)
1014 (setq math-pd-str (substring math-pd-str (match-end 0))
1015 bc-flag t)))
1016 ((memq this '(s ss bs SS BS))
1017 (and (if (memq next '(YY YYYY MM DD hh HH mm))
1018 (string-match "\\` *[0-9][0-9]\\(\\.[0-9]+\\)?" math-pd-str)
1019 (string-match "\\` *[0-9][0-9]?\\(\\.[0-9]+\\)?" math-pd-str))
1020 (setq second (math-match-substring math-pd-str 0)
1021 math-pd-str (substring math-pd-str (match-end 0))
1022 second (math-read-number second))))
1023 ((eq this 'C)
1024 (if (string-match "\\`:[0-9][0-9]" math-pd-str)
1025 (setq math-pd-str (substring math-pd-str 1))
1026 t))
1027 ((or (not (if (and (memq this '(ddd MM DD hh HH mm))
1028 (memq next '(YY YYYY MM DD ddd
1029 hh HH mm ss SS)))
1030 (if (eq this 'ddd)
1031 (string-match "\\` *[0-9][0-9][0-9]" math-pd-str)
1032 (string-match "\\` *[0-9][0-9]" math-pd-str))
1033 (string-match "\\` *[0-9]+" math-pd-str)))
1034 (and (setq num (string-to-number
1035 (math-match-substring math-pd-str 0))
1036 math-pd-str (substring math-pd-str (match-end 0)))
1037 nil))
1038 nil)
1039 ((eq this 'W)
1040 (and (>= num 0) (< num 7)))
1041 ((memq this '(d ddd bdd))
1042 (setq yearday num))
1043 ((memq this '(M MM BM))
1044 (setq month num))
1045 ((memq this '(D DD BD))
1046 (setq day num))
1047 ((memq this '(h hh bh H HH BH))
1048 (setq hour num))
1049 ((memq this '(m mm bm))
1050 (setq minute num)))
1051 (setq okay nil)))
1052 (if yearday
1053 (if (and month day)
1054 (setq yearday nil)
1055 (setq month 1 day 1)))
1056 (if (and okay (equal math-pd-str ""))
1057 (and month day (or (not (or hour minute second))
1058 (and hour minute))
1059 (progn
1060 (or year (setq year (math-this-year)))
1061 (or second (setq second 0))
1062 (if bc-flag
1063 (setq year (math-neg (math-abs year))))
1064 (setq day (math-parse-date-validate year bigyear month day
1065 hour minute second))
1066 (if yearday
1067 (setq day (math-add day (1- yearday))))
1068 day)))))
1069
1070
1071 (defun calcFunc-now (&optional zone)
1072 (let ((date (let ((calc-date-format nil))
1073 (math-parse-date (current-time-string)))))
1074 (if (consp date)
1075 (if zone
1076 (math-add date (math-div (math-sub (calcFunc-tzone nil date)
1077 (calcFunc-tzone zone date))
1078 '(float 864 2)))
1079 date)
1080 (calc-record-why "*Unable to interpret current date from system")
1081 (append (list 'calcFunc-now) (and zone (list zone))))))
1082
1083 (defun calcFunc-year (date)
1084 (car (math-date-to-dt date)))
1085
1086 (defun calcFunc-month (date)
1087 (nth 1 (math-date-to-dt date)))
1088
1089 (defun calcFunc-day (date)
1090 (nth 2 (math-date-to-dt date)))
1091
1092 (defun calcFunc-weekday (date)
1093 (if (eq (car-safe date) 'date)
1094 (setq date (nth 1 date)))
1095 (or (math-realp date)
1096 (math-reject-arg date 'datep))
1097 (math-mod (math-add (math-floor date) 6) 7))
1098
1099 (defun calcFunc-yearday (date)
1100 (let ((dt (math-date-to-dt date)))
1101 (math-day-number (car dt) (nth 1 dt) (nth 2 dt))))
1102
1103 (defun calcFunc-hour (date)
1104 (if (eq (car-safe date) 'hms)
1105 (nth 1 date)
1106 (or (nth 3 (math-date-to-dt date)) 0)))
1107
1108 (defun calcFunc-minute (date)
1109 (if (eq (car-safe date) 'hms)
1110 (nth 2 date)
1111 (or (nth 4 (math-date-to-dt date)) 0)))
1112
1113 (defun calcFunc-second (date)
1114 (if (eq (car-safe date) 'hms)
1115 (nth 3 date)
1116 (or (nth 5 (math-date-to-dt date)) 0)))
1117
1118 (defun calcFunc-time (date)
1119 (let ((dt (math-date-to-dt date)))
1120 (if (nth 3 dt)
1121 (cons 'hms (nthcdr 3 dt))
1122 (list 'hms 0 0 0))))
1123
1124 (defun calcFunc-date (date &optional month day hour minute second)
1125 (and (math-messy-integerp month) (setq month (math-trunc month)))
1126 (and month (not (integerp month)) (math-reject-arg month 'fixnump))
1127 (and (math-messy-integerp day) (setq day (math-trunc day)))
1128 (and day (not (integerp day)) (math-reject-arg day 'fixnump))
1129 (if (and (eq (car-safe hour) 'hms) (not minute))
1130 (setq second (nth 3 hour)
1131 minute (nth 2 hour)
1132 hour (nth 1 hour)))
1133 (and (math-messy-integerp hour) (setq hour (math-trunc hour)))
1134 (and hour (not (integerp hour)) (math-reject-arg hour 'fixnump))
1135 (and (math-messy-integerp minute) (setq minute (math-trunc minute)))
1136 (and minute (not (integerp minute)) (math-reject-arg minute 'fixnump))
1137 (and (math-messy-integerp second) (setq second (math-trunc second)))
1138 (and second (not (math-realp second)) (math-reject-arg second 'realp))
1139 (if month
1140 (progn
1141 (and (math-messy-integerp date) (setq date (math-trunc date)))
1142 (and date (not (math-integerp date)) (math-reject-arg date 'integerp))
1143 (if day
1144 (if hour
1145 (list 'date (math-dt-to-date (list date month day hour
1146 (or minute 0)
1147 (or second 0))))
1148 (list 'date (math-dt-to-date (list date month day))))
1149 (list 'date (math-dt-to-date (list (math-this-year) date month)))))
1150 (if (math-realp date)
1151 (list 'date date)
1152 (if (eq (car date) 'date)
1153 (nth 1 date)
1154 (math-reject-arg date 'datep)))))
1155
1156 (defun calcFunc-julian (date &optional zone)
1157 (if (math-realp date)
1158 (list 'date (if (math-integerp date)
1159 (math-sub date math-julian-date-beginning-int)
1160 (setq date (math-sub date math-julian-date-beginning))
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 math-julian-date-beginning-int
1166 (math-add math-julian-date-beginning
1167 (math-div (calcFunc-tzone zone date)
1168 '(float 864 2)))))
1169 (math-reject-arg date 'datep))))
1170
1171 (defun calcFunc-unixtime (date &optional zone)
1172 (if (math-realp date)
1173 (progn
1174 (setq date (math-add 719164 (math-div date '(float 864 2))))
1175 (list 'date (math-sub date (math-div (calcFunc-tzone zone date)
1176 '(float 864 2)))))
1177 (if (eq (car date) 'date)
1178 (math-add (nth 1 (math-date-parts (nth 1 date) 719164))
1179 (calcFunc-tzone zone date))
1180 (math-reject-arg date 'datep))))
1181
1182
1183 ;;; Note: Longer names must appear before shorter names which are
1184 ;;; substrings of them.
1185 (defvar math-tzone-names
1186 '(( "UTC" 0 0)
1187 ( "MEGT" -1 "MET" "METDST" ) ; Middle Europe
1188 ( "METDST" -1 -1 ) ( "MET" -1 0 )
1189 ( "MEGZ" -1 "MEZ" "MESZ" ) ( "MEZ" -1 0 ) ( "MESZ" -1 -1 )
1190 ( "WEGT" 0 "WET" "WETDST" ) ; Western Europe
1191 ( "WETDST" 0 -1 ) ( "WET" 0 0 )
1192 ( "BGT" 0 "GMT" "BST" ) ( "GMT" 0 0 ) ( "BST" 0 -1 ) ; Britain
1193 ( "NGT" (float 35 -1) "NST" "NDT" ) ; Newfoundland
1194 ( "NST" (float 35 -1) 0 ) ( "NDT" (float 35 -1) -1 )
1195 ( "AGT" 4 "AST" "ADT" ) ( "AST" 4 0 ) ( "ADT" 4 -1 ) ; Atlantic
1196 ( "EGT" 5 "EST" "EDT" ) ( "EST" 5 0 ) ( "EDT" 5 -1 ) ; Eastern
1197 ( "CGT" 6 "CST" "CDT" ) ( "CST" 6 0 ) ( "CDT" 6 -1 ) ; Central
1198 ( "MGT" 7 "MST" "MDT" ) ( "MST" 7 0 ) ( "MDT" 7 -1 ) ; Mountain
1199 ( "PGT" 8 "PST" "PDT" ) ( "PST" 8 0 ) ( "PDT" 8 -1 ) ; Pacific
1200 ( "YGT" 9 "YST" "YDT" ) ( "YST" 9 0 ) ( "YDT" 9 -1 ) ; Yukon
1201 )
1202 "No doc yet. See calc manual for now. ")
1203
1204 (defvar var-TimeZone nil)
1205
1206 ;; From cal-dst
1207 (defvar calendar-current-time-zone-cache)
1208
1209 (defvar math-calendar-tzinfo
1210 nil
1211 "Information about the timezone, retrieved from the calendar.")
1212
1213 (defun math-get-calendar-tzinfo ()
1214 "Get information about the timezone from the calendar.
1215 The result should be a list of two items about the current time zone:
1216 first, the number of seconds difference from GMT
1217 second, the number of seconds offset for daylight savings."
1218 (if math-calendar-tzinfo
1219 math-calendar-tzinfo
1220 (require 'cal-dst)
1221 (let ((tzinfo (progn
1222 (calendar-current-time-zone)
1223 calendar-current-time-zone-cache)))
1224 (setq math-calendar-tzinfo
1225 (list (* 60 (abs (nth 0 tzinfo)))
1226 (* 60 (nth 1 tzinfo)))))))
1227
1228 (defun calcFunc-tzone (&optional zone date)
1229 (if zone
1230 (cond ((math-realp zone)
1231 (math-round (math-mul zone 3600)))
1232 ((eq (car zone) 'hms)
1233 (math-round (math-mul (math-from-hms zone 'deg) 3600)))
1234 ((eq (car zone) '+)
1235 (math-add (calcFunc-tzone (nth 1 zone) date)
1236 (calcFunc-tzone (nth 2 zone) date)))
1237 ((eq (car zone) '-)
1238 (math-sub (calcFunc-tzone (nth 1 zone) date)
1239 (calcFunc-tzone (nth 2 zone) date)))
1240 ((eq (car zone) 'var)
1241 (let ((name (upcase (symbol-name (nth 1 zone))))
1242 found)
1243 (if (setq found (assoc name math-tzone-names))
1244 (calcFunc-tzone (math-add (nth 1 found)
1245 (if (integerp (nth 2 found))
1246 (nth 2 found)
1247 (or
1248 (math-daylight-savings-adjust
1249 date (car found))
1250 0)))
1251 date)
1252 (if (equal name "LOCAL")
1253 (calcFunc-tzone nil date)
1254 (math-reject-arg zone "*Unrecognized time zone name")))))
1255 (t (math-reject-arg zone "*Expected a time zone")))
1256 (if (calc-var-value 'var-TimeZone)
1257 (calcFunc-tzone (calc-var-value 'var-TimeZone) date)
1258 (let ((tzinfo (math-get-calendar-tzinfo)))
1259 (+ (nth 0 tzinfo)
1260 (* (math-cal-daylight-savings-adjust date) (nth 1 tzinfo)))))))
1261
1262 (defvar math-daylight-savings-hook 'math-std-daylight-savings)
1263
1264 (defun math-daylight-savings-adjust (date zone &optional dt)
1265 (or date (setq date (nth 1 (calcFunc-now))))
1266 (let (bump)
1267 (if (eq (car-safe date) 'date)
1268 (setq bump 0
1269 date (nth 1 date))
1270 (if (and date (math-realp date))
1271 (let ((zadj (assoc zone math-tzone-names)))
1272 (if zadj (setq bump -1
1273 date (math-sub date (math-div (nth 1 zadj)
1274 '(float 24 0))))))
1275 (math-reject-arg date 'datep)))
1276 (setq date (math-float date))
1277 (or dt (setq dt (math-date-to-dt date)))
1278 (and math-daylight-savings-hook
1279 (funcall math-daylight-savings-hook date dt zone bump))))
1280
1281 ;;; Based on part of dst-adjust-time in cal-dst.el
1282 ;;; For calcFunc-dst, when zone=nil
1283 (defun math-cal-daylight-savings-adjust (date)
1284 "Return -1 if DATE is using daylight saving, 0 otherwise."
1285 (require 'cal-dst)
1286 (unless date (setq date (calcFunc-now)))
1287 (let* ((dt (math-date-to-dt date))
1288 (time (cond
1289 ((nth 3 dt)
1290 (nth 3 dt))
1291 ((nth 4 dt)
1292 (+ (nth 3 dt) (/ (nth 4 dt) 60.0)))
1293 (t
1294 0)))
1295 (rounded-abs-date
1296 (+
1297 (calendar-absolute-from-gregorian
1298 (list (nth 1 dt) (nth 2 dt) (nth 0 dt)))
1299 (/ (round (* 60 time)) 60.0 24.0))))
1300 (if (dst-in-effect rounded-abs-date)
1301 -1
1302 0)))
1303
1304 (defun calcFunc-dsadj (date &optional zone)
1305 (if zone
1306 (or (eq (car-safe zone) 'var)
1307 (math-reject-arg zone "*Time zone variable expected"))
1308 (setq zone (calc-var-value 'var-TimeZone)))
1309 (if zone
1310 (progn
1311 (setq zone (and (eq (car-safe zone) 'var)
1312 (upcase (symbol-name (nth 1 zone)))))
1313 (let ((zadj (assoc zone math-tzone-names)))
1314 (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1315 (if (integerp (nth 2 zadj))
1316 (nth 2 zadj)
1317 (math-daylight-savings-adjust date zone))))
1318 (math-cal-daylight-savings-adjust date)))
1319
1320 ;; (defun calcFunc-dsadj (date &optional zone)
1321 ;; (if zone
1322 ;; (or (eq (car-safe zone) 'var)
1323 ;; (math-reject-arg zone "*Time zone variable expected"))
1324 ;; (setq zone (or (calc-var-value 'var-TimeZone)
1325 ;; (progn
1326 ;; (calcFunc-tzone)
1327 ;; (calc-var-value 'var-TimeZone)))))
1328 ;; (setq zone (and (eq (car-safe zone) 'var)
1329 ;; (upcase (symbol-name (nth 1 zone)))))
1330 ;; (let ((zadj (assoc zone math-tzone-names)))
1331 ;; (or zadj (math-reject-arg zone "*Unrecognized time zone name"))
1332 ;; (if (integerp (nth 2 zadj))
1333 ;; (nth 2 zadj)
1334 ;; (math-daylight-savings-adjust date zone))))
1335
1336 (defun calcFunc-tzconv (date z1 z2)
1337 (if (math-realp date)
1338 (nth 1 (calcFunc-tzconv (list 'date date) z1 z2))
1339 (calcFunc-unixtime (calcFunc-unixtime date z1) z2)))
1340
1341 (defun math-std-daylight-savings (date dt zone bump)
1342 "Standard North American daylight saving algorithm.
1343 Before 2007, this uses `math-std-daylight-savings-old', where
1344 daylight saving began on the first Sunday of April at 2 a.m.,
1345 and ended on the last Sunday of October at 2 a.m.
1346 As of 2007, this uses `math-std-daylight-savings-new', where
1347 daylight saving begins on the second Sunday of March at 2 a.m.,
1348 and ends on the first Sunday of November at 2 a.m."
1349 (if (< (car dt) 2007)
1350 (math-std-daylight-savings-old date dt zone bump)
1351 (math-std-daylight-savings-new date dt zone bump)))
1352
1353 (defun math-std-daylight-savings-new (date dt zone bump)
1354 "Standard North American daylight saving algorithm as of 2007.
1355 This implements the rules for the U.S. and Canada.
1356 Daylight saving begins on the second Sunday of March at 2 a.m.,
1357 and ends on the first Sunday of November at 2 a.m."
1358 (cond ((< (nth 1 dt) 3) 0)
1359 ((= (nth 1 dt) 3)
1360 (let ((sunday (math-prev-weekday-in-month date dt 14 0)))
1361 (cond ((< (nth 2 dt) sunday) 0)
1362 ((= (nth 2 dt) sunday)
1363 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1364 (t -1))))
1365 ((< (nth 1 dt) 11) -1)
1366 ((= (nth 1 dt) 11)
1367 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1368 (cond ((< (nth 2 dt) sunday) -1)
1369 ((= (nth 2 dt) sunday)
1370 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1371 (t 0))))
1372 (t 0)))
1373
1374 (defun math-std-daylight-savings-old (date dt zone bump)
1375 "Standard North American daylight saving algorithm before 2007.
1376 This implements the rules for the U.S. and Canada.
1377 Daylight saving begins on the first Sunday of April at 2 a.m.,
1378 and ends on the last Sunday of October at 2 a.m."
1379 (cond ((< (nth 1 dt) 4) 0)
1380 ((= (nth 1 dt) 4)
1381 (let ((sunday (math-prev-weekday-in-month date dt 7 0)))
1382 (cond ((< (nth 2 dt) sunday) 0)
1383 ((= (nth 2 dt) sunday)
1384 (if (>= (nth 3 dt) (+ 3 bump)) -1 0))
1385 (t -1))))
1386 ((< (nth 1 dt) 10) -1)
1387 ((= (nth 1 dt) 10)
1388 (let ((sunday (math-prev-weekday-in-month date dt 31 0)))
1389 (cond ((< (nth 2 dt) sunday) -1)
1390 ((= (nth 2 dt) sunday)
1391 (if (>= (nth 3 dt) (+ 2 bump)) 0 -1))
1392 (t 0))))
1393 (t 0)))
1394
1395 ;;; Compute the day (1-31) of the WDAY (0-6) on or preceding the given
1396 ;;; day of the given month.
1397 (defun math-prev-weekday-in-month (date dt day wday)
1398 (or day (setq day (nth 2 dt)))
1399 (if (> day (math-days-in-month (car dt) (nth 1 dt)))
1400 (setq day (math-days-in-month (car dt) (nth 1 dt))))
1401 (let ((zeroth (math-sub (math-floor date) (nth 2 dt))))
1402 (math-sub (nth 1 (calcFunc-newweek (math-add zeroth day))) zeroth)))
1403
1404 (defun calcFunc-pwday (date &optional day weekday)
1405 (if (eq (car-safe date) 'date)
1406 (setq date (nth 1 date)))
1407 (or (math-realp date)
1408 (math-reject-arg date 'datep))
1409 (if (math-messy-integerp day) (setq day (math-trunc day)))
1410 (or (integerp day) (math-reject-arg day 'fixnump))
1411 (if (= day 0) (setq day 31))
1412 (and (or (< day 7) (> day 31)) (math-reject-arg day 'range))
1413 (math-prev-weekday-in-month date (math-date-to-dt date) day (or weekday 0)))
1414
1415
1416 (defun calcFunc-newweek (date &optional weekday)
1417 (if (eq (car-safe date) 'date)
1418 (setq date (nth 1 date)))
1419 (or (math-realp date)
1420 (math-reject-arg date 'datep))
1421 (or weekday (setq weekday 0))
1422 (and (math-messy-integerp weekday) (setq weekday (math-trunc weekday)))
1423 (or (integerp weekday) (math-reject-arg weekday 'fixnump))
1424 (and (or (< weekday 0) (> weekday 6)) (math-reject-arg weekday 'range))
1425 (setq date (math-floor date))
1426 (list 'date (math-sub date (calcFunc-weekday (math-sub date weekday)))))
1427
1428 (defun calcFunc-newmonth (date &optional day)
1429 (or day (setq day 1))
1430 (and (math-messy-integerp day) (setq day (math-trunc day)))
1431 (or (integerp day) (math-reject-arg day 'fixnump))
1432 (and (or (< day 0) (> day 31)) (math-reject-arg day 'range))
1433 (let ((dt (math-date-to-dt date)))
1434 (if (or (= day 0) (> day (math-days-in-month (car dt) (nth 1 dt))))
1435 (setq day (math-days-in-month (car dt) (nth 1 dt))))
1436 (and (eq (car dt) 1752) (= (nth 1 dt) 9)
1437 (if (>= day 14) (setq day (- day 11))))
1438 (list 'date (math-add (math-dt-to-date (list (car dt) (nth 1 dt) 1))
1439 (1- day)))))
1440
1441 (defun calcFunc-newyear (date &optional day)
1442 (or day (setq day 1))
1443 (and (math-messy-integerp day) (setq day (math-trunc day)))
1444 (or (integerp day) (math-reject-arg day 'fixnump))
1445 (let ((dt (math-date-to-dt date)))
1446 (if (and (>= day 0) (<= day 366))
1447 (let ((max (if (eq (car dt) 1752) 355
1448 (if (math-leap-year-p (car dt)) 366 365))))
1449 (if (or (= day 0) (> day max)) (setq day max))
1450 (list 'date (math-add (math-dt-to-date (list (car dt) 1 1))
1451 (1- day))))
1452 (if (and (>= day -12) (<= day -1))
1453 (list 'date (math-dt-to-date (list (car dt) (- day) 1)))
1454 (math-reject-arg day 'range)))))
1455
1456 (defun calcFunc-incmonth (date &optional step)
1457 (or step (setq step 1))
1458 (and (math-messy-integerp step) (setq step (math-trunc step)))
1459 (or (math-integerp step) (math-reject-arg step 'integerp))
1460 (let* ((dt (math-date-to-dt date))
1461 (year (car dt))
1462 (month (math-add (1- (nth 1 dt)) step))
1463 (extra (calcFunc-idiv month 12))
1464 (day (nth 2 dt)))
1465 (setq month (1+ (math-sub month (math-mul extra 12)))
1466 year (math-add year extra)
1467 day (min day (math-days-in-month year month)))
1468 (and (math-posp (car dt)) (not (math-posp year))
1469 (setq year (math-sub year 1))) ; did we go past the year zero?
1470 (and (math-negp (car dt)) (not (math-negp year))
1471 (setq year (math-add year 1)))
1472 (list 'date (math-dt-to-date
1473 (cons year (cons month (cons day (cdr (cdr (cdr dt))))))))))
1474
1475 (defun calcFunc-incyear (date &optional step)
1476 (calcFunc-incmonth date (math-mul (or step 1) 12)))
1477
1478
1479
1480 (defun calcFunc-bsub (a b)
1481 (or (eq (car-safe a) 'date)
1482 (math-reject-arg a 'datep))
1483 (if (eq (car-safe b) 'date)
1484 (if (math-lessp (nth 1 a) (nth 1 b))
1485 (math-neg (calcFunc-bsub b a))
1486 (math-setup-holidays b)
1487 (let* ((da (math-to-business-day a))
1488 (db (math-to-business-day b)))
1489 (math-add (math-sub (car da) (car db))
1490 (if (and (cdr db) (not (cdr da))) 1 0))))
1491 (calcFunc-badd a (math-neg b))))
1492
1493 (defvar math-holidays-cache nil)
1494 (defvar math-holidays-cache-tag t)
1495 (defun calcFunc-badd (a b)
1496 (if (eq (car-safe b) 'date)
1497 (if (eq (car-safe a) 'date)
1498 (math-reject-arg nil "*Invalid combination in date arithmetic")
1499 (calcFunc-badd b a))
1500 (if (eq (car-safe a) 'date)
1501 (if (Math-realp b)
1502 (if (Math-zerop b)
1503 a
1504 (let* ((d (math-to-business-day a))
1505 (bb (math-add (car d)
1506 (if (and (cdr d) (Math-posp b))
1507 (math-sub b 1) b))))
1508 (or (math-from-business-day bb)
1509 (calcFunc-badd a b))))
1510 (if (eq (car-safe b) 'hms)
1511 (let ((hours (nth 7 math-holidays-cache)))
1512 (setq b (math-div (math-from-hms b 'deg) 24))
1513 (if hours
1514 (setq b (math-div b (cdr hours))))
1515 (calcFunc-badd a b))
1516 (math-reject-arg nil "*Invalid combination in date arithmetic")))
1517 (math-reject-arg a 'datep))))
1518
1519 (defun calcFunc-holiday (a)
1520 (if (cdr (math-to-business-day a)) 1 0))
1521
1522 ;;; Compute the number of business days since Jan 1, 1 AD.
1523
1524 (defun math-to-business-day (date &optional need-year)
1525 (if (eq (car-safe date) 'date)
1526 (setq date (nth 1 date)))
1527 (or (Math-realp date)
1528 (math-reject-arg date 'datep))
1529 (let* ((day (math-floor date))
1530 (time (math-sub date day))
1531 (dt (math-date-to-dt day))
1532 (delta 0)
1533 (holiday nil))
1534 (or (not need-year) (eq (car dt) need-year)
1535 (math-reject-arg (list 'date day) "*Generated holiday has wrong year"))
1536 (math-setup-holidays date)
1537 (let ((days (car math-holidays-cache)))
1538 (while (and (setq days (cdr days)) (< (car days) day))
1539 (setq delta (1+ delta)))
1540 (and days (= day (car days))
1541 (setq holiday t)))
1542 (let* ((weekdays (nth 3 math-holidays-cache))
1543 (weeks (1- (/ (+ day 6) 7)))
1544 (wkday (- day 1 (* weeks 7))))
1545 (setq delta (+ delta (* weeks (length weekdays))))
1546 (while (and weekdays (< (car weekdays) wkday))
1547 (setq weekdays (cdr weekdays)
1548 delta (1+ delta)))
1549 (and weekdays (eq wkday (car weekdays))
1550 (setq holiday t)))
1551 (let ((hours (nth 7 math-holidays-cache)))
1552 (if hours
1553 (progn
1554 (setq time (math-div (math-sub time (car hours)) (cdr hours)))
1555 (if (Math-lessp time 0) (setq time 0))
1556 (or (Math-lessp time 1)
1557 (setq time
1558 (math-sub 1
1559 (math-div 1 (math-mul 86400 (cdr hours)))))))))
1560 (cons (math-add (math-sub day delta) time) holiday)))
1561
1562
1563 ;;; Compute the date a certain number of business days since Jan 1, 1 AD.
1564 ;;; If this returns nil, holiday table was adjusted; redo calculation.
1565
1566 (defun math-from-business-day (num)
1567 (let* ((day (math-floor num))
1568 (time (math-sub num day)))
1569 (or (integerp day)
1570 (math-reject-arg nil "*Date is outside valid range"))
1571 (math-setup-holidays)
1572 (let ((days (nth 1 math-holidays-cache))
1573 (delta 0))
1574 (while (and (setq days (cdr days)) (< (car days) day))
1575 (setq delta (1+ delta)))
1576 (setq day (+ day delta)))
1577 (let* ((weekdays (nth 3 math-holidays-cache))
1578 (bweek (- 7 (length weekdays)))
1579 (weeks (1- (/ (+ day (1- bweek)) bweek)))
1580 (wkday (- day 1 (* weeks bweek)))
1581 (w 0))
1582 (setq day (+ day (* weeks (length weekdays))))
1583 (while (if (memq w weekdays)
1584 (setq day (1+ day))
1585 (> (setq wkday (1- wkday)) 0))
1586 (setq w (1+ w)))
1587 (let ((hours (nth 7 math-holidays-cache)))
1588 (if hours
1589 (setq time (math-add (math-mul time (cdr hours)) (car hours)))))
1590 (and (not (math-setup-holidays day))
1591 (list 'date (math-add day time))))))
1592
1593 ;; The variable math-sh-year is local to math-setup-holidays
1594 ;; and math-setup-year-holiday, but is used by math-setup-add-holidays,
1595 ;; which is called by math-setup-holidays and math-setup-year-holiday.
1596 (defvar math-sh-year)
1597
1598 (defun math-setup-holidays (&optional date)
1599 (or (eq (calc-var-value 'var-Holidays) math-holidays-cache-tag)
1600 (let ((h (calc-var-value 'var-Holidays))
1601 (wdnames '( (sun . 0) (mon . 1) (tue . 2) (wed . 3)
1602 (thu . 4) (fri . 5) (sat . 6) ))
1603 (days nil) (weekdays nil) (exprs nil) (limit nil) (hours nil))
1604 (or (math-vectorp h)
1605 (math-reject-arg h "*Holidays variable must be a vector"))
1606 (while (setq h (cdr h))
1607 (cond ((or (and (eq (car-safe (car h)) 'date)
1608 (integerp (nth 1 (car h))))
1609 (and (eq (car-safe (car h)) 'intv)
1610 (eq (car-safe (nth 2 (car h))) 'date))
1611 (eq (car-safe (car h)) 'vec))
1612 (setq days (cons (car h) days)))
1613 ((and (eq (car-safe (car h)) 'var)
1614 (assq (nth 1 (car h)) wdnames))
1615 (setq weekdays (cons (cdr (assq (nth 1 (car h)) wdnames))
1616 weekdays)))
1617 ((and (eq (car-safe (car h)) 'intv)
1618 (eq (car-safe (nth 2 (car h))) 'hms)
1619 (eq (car-safe (nth 3 (car h))) 'hms))
1620 (if hours
1621 (math-reject-arg
1622 (car h) "*Only one hours interval allowed in Holidays"))
1623 (setq hours (math-div (car h) '(hms 24 0 0)))
1624 (if (or (Math-lessp (nth 2 hours) 0)
1625 (Math-lessp 1 (nth 3 hours)))
1626 (math-reject-arg
1627 (car h) "*Hours interval out of range"))
1628 (setq hours (cons (nth 2 hours)
1629 (math-sub (nth 3 hours) (nth 2 hours))))
1630 (if (Math-zerop (cdr hours))
1631 (math-reject-arg
1632 (car h) "*Degenerate hours interval")))
1633 ((or (and (eq (car-safe (car h)) 'intv)
1634 (Math-integerp (nth 2 (car h)))
1635 (Math-integerp (nth 3 (car h))))
1636 (and (integerp (car h))
1637 (> (car h) 1900) (< (car h) 2100)))
1638 (if limit
1639 (math-reject-arg
1640 (car h) "*Only one limit allowed in Holidays"))
1641 (setq limit (calcFunc-vint (car h) '(intv 3 1 2737)))
1642 (if (equal limit '(vec))
1643 (math-reject-arg (car h) "*Limit is out of range")))
1644 ((or (math-expr-contains (car h) '(var y var-y))
1645 (math-expr-contains (car h) '(var m var-m)))
1646 (setq exprs (cons (car h) exprs)))
1647 (t (math-reject-arg
1648 (car h) "*Holidays must contain a vector of holidays"))))
1649 (if (= (length weekdays) 7)
1650 (math-reject-arg nil "*Too many weekend days"))
1651 (setq math-holidays-cache (list (list -1) ; 0: days list
1652 (list -1) ; 1: inverse-days list
1653 nil ; 2: exprs
1654 (sort weekdays '<)
1655 (or limit '(intv 3 1 2737))
1656 nil ; 5: (lo.hi) expanded years
1657 (cons exprs days)
1658 hours) ; 7: business hours
1659 math-holidays-cache-tag (calc-var-value 'var-Holidays))))
1660 (if date
1661 (let ((year (calcFunc-year date))
1662 (limits (nth 5 math-holidays-cache))
1663 (done nil))
1664 (or (eq (calcFunc-in year (nth 4 math-holidays-cache)) 1)
1665 (progn
1666 (or (eq (car-safe date) 'date) (setq date (list 'date date)))
1667 (math-reject-arg date "*Date is outside valid range")))
1668 (unwind-protect
1669 (let ((days (nth 6 math-holidays-cache)))
1670 (if days
1671 (let ((math-sh-year nil)) ; see below
1672 (setcar (nthcdr 6 math-holidays-cache) nil)
1673 (math-setup-add-holidays (cons 'vec (cdr days)))
1674 (setcar (nthcdr 2 math-holidays-cache) (car days))))
1675 (cond ((not (nth 2 math-holidays-cache))
1676 (setq done t)
1677 nil)
1678 ((not limits)
1679 (setcar (nthcdr 5 math-holidays-cache) (cons year year))
1680 (math-setup-year-holidays year)
1681 (setq done t))
1682 ((< year (car limits))
1683 (message "Computing holidays, %d .. %d"
1684 year (1- (car limits)))
1685 (calc-set-command-flag 'clear-message)
1686 (while (< year (car limits))
1687 (setcar limits (1- (car limits)))
1688 (math-setup-year-holidays (car limits)))
1689 (setq done t))
1690 ((> year (cdr limits))
1691 (message "Computing holidays, %d .. %d"
1692 (1+ (cdr limits)) year)
1693 (calc-set-command-flag 'clear-message)
1694 (while (> year (cdr limits))
1695 (setcdr limits (1+ (cdr limits)))
1696 (math-setup-year-holidays (cdr limits)))
1697 (setq done t))
1698 (t
1699 (setq done t)
1700 nil)))
1701 (or done (setq math-holidays-cache-tag t))))))
1702
1703 (defun math-setup-year-holidays (math-sh-year)
1704 (let ((exprs (nth 2 math-holidays-cache)))
1705 (while exprs
1706 (let* ((var-y math-sh-year)
1707 (var-m nil)
1708 (expr (math-evaluate-expr (car exprs))))
1709 (if (math-expr-contains expr '(var m var-m))
1710 (let ((var-m 0))
1711 (while (<= (setq var-m (1+ var-m)) 12)
1712 (math-setup-add-holidays (math-evaluate-expr expr))))
1713 (math-setup-add-holidays expr)))
1714 (setq exprs (cdr exprs)))))
1715
1716 (defun math-setup-add-holidays (days) ; uses "math-sh-year"
1717 (cond ((eq (car-safe days) 'vec)
1718 (while (setq days (cdr days))
1719 (math-setup-add-holidays (car days))))
1720 ((eq (car-safe days) 'intv)
1721 (let ((day (math-ceiling (nth 2 days))))
1722 (or (eq (calcFunc-in day days) 1)
1723 (setq day (math-add day 1)))
1724 (while (eq (calcFunc-in day days) 1)
1725 (math-setup-add-holidays day)
1726 (setq day (math-add day 1)))))
1727 ((eq (car-safe days) 'date)
1728 (math-setup-add-holidays (nth 1 days)))
1729 ((eq days 0))
1730 ((integerp days)
1731 (let ((b (math-to-business-day days math-sh-year)))
1732 (or (cdr b) ; don't register holidays twice!
1733 (let ((prev (car math-holidays-cache))
1734 (iprev (nth 1 math-holidays-cache)))
1735 (while (and (cdr prev) (< (nth 1 prev) days))
1736 (setq prev (cdr prev) iprev (cdr iprev)))
1737 (setcdr prev (cons days (cdr prev)))
1738 (setcdr iprev (cons (car b) (cdr iprev)))
1739 (while (setq iprev (cdr iprev))
1740 (setcar iprev (1- (car iprev))))))))
1741 ((Math-realp days)
1742 (math-reject-arg (list 'date days) "*Invalid holiday value"))
1743 (t
1744 (math-reject-arg days "*Holiday formula failed to evaluate"))))
1745
1746
1747
1748
1749 ;;;; Error forms.
1750
1751 ;;; Build a standard deviation form. [X X X]
1752 (defun math-make-sdev (x sigma)
1753 (if (memq (car-safe x) '(date mod sdev intv vec))
1754 (math-reject-arg x 'realp))
1755 (if (memq (car-safe sigma) '(date mod sdev intv vec))
1756 (math-reject-arg sigma 'realp))
1757 (if (or (Math-negp sigma) (memq (car-safe sigma) '(cplx polar)))
1758 (setq sigma (math-abs sigma)))
1759 (if (and (Math-zerop sigma) (Math-scalarp x))
1760 x
1761 (list 'sdev x sigma)))
1762 (defun calcFunc-sdev (x sigma)
1763 (math-make-sdev x sigma))
1764
1765
1766
1767 ;;;; Modulo forms.
1768
1769 (defun math-normalize-mod (a)
1770 (let ((n (math-normalize (nth 1 a)))
1771 (m (math-normalize (nth 2 a))))
1772 (if (and (math-anglep n) (math-anglep m) (math-posp m))
1773 (math-make-mod n m)
1774 (math-normalize (list 'calcFunc-makemod n m)))))
1775
1776 ;;; Build a modulo form. [N R R]
1777 (defun math-make-mod (n m)
1778 (setq calc-previous-modulo m)
1779 (and n
1780 (cond ((not (Math-anglep m))
1781 (math-reject-arg m 'anglep))
1782 ((not (math-posp m))
1783 (math-reject-arg m 'posp))
1784 ((Math-anglep n)
1785 (if (or (Math-negp n)
1786 (not (Math-lessp n m)))
1787 (list 'mod (math-mod n m) m)
1788 (list 'mod n m)))
1789 ((memq (car n) '(+ - / vec neg))
1790 (math-normalize
1791 (cons (car n)
1792 (mapcar (function (lambda (x) (math-make-mod x m)))
1793 (cdr n)))))
1794 ((and (eq (car n) '*) (Math-anglep (nth 1 n)))
1795 (math-mul (math-make-mod (nth 1 n) m) (nth 2 n)))
1796 ((memq (car n) '(* ^ var calcFunc-subscr))
1797 (math-mul (math-make-mod 1 m) n))
1798 (t (math-reject-arg n 'anglep)))))
1799 (defun calcFunc-makemod (n m)
1800 (math-make-mod n m))
1801
1802
1803
1804 ;;;; Interval forms.
1805
1806 ;;; Build an interval form. [X S X X]
1807 (defun math-make-intv (mask lo hi)
1808 (if (memq (car-safe lo) '(cplx polar mod sdev intv vec))
1809 (math-reject-arg lo 'realp))
1810 (if (memq (car-safe hi) '(cplx polar mod sdev intv vec))
1811 (math-reject-arg hi 'realp))
1812 (or (eq (eq (car-safe lo) 'date) (eq (car-safe hi) 'date))
1813 (math-reject-arg (if (eq (car-safe lo) 'date) hi lo) 'datep))
1814 (if (and (or (Math-realp lo) (eq (car lo) 'date))
1815 (or (Math-realp hi) (eq (car hi) 'date)))
1816 (let ((cmp (math-compare lo hi)))
1817 (if (= cmp 0)
1818 (if (= mask 3)
1819 lo
1820 (list 'intv mask lo hi))
1821 (if (> cmp 0)
1822 (if (= mask 3)
1823 (list 'intv 2 lo lo)
1824 (list 'intv mask lo lo))
1825 (list 'intv mask lo hi))))
1826 (list 'intv mask lo hi)))
1827 (defun calcFunc-intv (mask lo hi)
1828 (if (math-messy-integerp mask) (setq mask (math-trunc mask)))
1829 (or (natnump mask) (math-reject-arg mask 'fixnatnump))
1830 (or (<= mask 3) (math-reject-arg mask 'range))
1831 (math-make-intv mask lo hi))
1832
1833 (defun math-sort-intv (mask lo hi)
1834 (if (Math-lessp hi lo)
1835 (math-make-intv (aref [0 2 1 3] mask) hi lo)
1836 (math-make-intv mask lo hi)))
1837
1838
1839
1840
1841 (defun math-combine-intervals (a am b bm c cm d dm)
1842 (let (res)
1843 (if (= (setq res (math-compare a c)) 1)
1844 (setq a c am cm)
1845 (if (= res 0)
1846 (setq am (or am cm))))
1847 (if (= (setq res (math-compare b d)) -1)
1848 (setq b d bm dm)
1849 (if (= res 0)
1850 (setq bm (or bm dm))))
1851 (math-make-intv (+ (if am 2 0) (if bm 1 0)) a b)))
1852
1853
1854 (defun math-div-mod (a b m) ; [R R R R] (Returns nil if no solution)
1855 (and (Math-integerp a) (Math-integerp b) (Math-integerp m)
1856 (let ((u1 1) (u3 b) (v1 0) (v3 m))
1857 (while (not (eq v3 0)) ; See Knuth sec 4.5.2, exercise 15
1858 (let* ((q (math-idivmod u3 v3))
1859 (t1 (math-sub u1 (math-mul v1 (car q)))))
1860 (setq u1 v1 u3 v3 v1 t1 v3 (cdr q))))
1861 (let ((q (math-idivmod a u3)))
1862 (and (eq (cdr q) 0)
1863 (math-mod (math-mul (car q) u1) m))))))
1864
1865 (defun math-mod-intv (a b)
1866 (let* ((q1 (math-floor (math-div (nth 2 a) b)))
1867 (q2 (math-floor (math-div (nth 3 a) b)))
1868 (m1 (math-sub (nth 2 a) (math-mul q1 b)))
1869 (m2 (math-sub (nth 3 a) (math-mul q2 b))))
1870 (cond ((equal q1 q2)
1871 (math-sort-intv (nth 1 a) m1 m2))
1872 ((and (math-equal-int (math-sub q2 q1) 1)
1873 (math-zerop m2)
1874 (memq (nth 1 a) '(0 2)))
1875 (math-make-intv (nth 1 a) m1 b))
1876 (t
1877 (math-make-intv 2 0 b)))))
1878
1879 ;; The variables math-exp-str and math-exp-pos are local to
1880 ;; math-read-exprs in math-aent.el, but are used by
1881 ;; math-read-angle-brackets, which is called (indirectly) by
1882 ;; math-read-exprs.
1883 (defvar math-exp-str)
1884 (defvar math-exp-pos)
1885
1886 (defun math-read-angle-brackets ()
1887 (let* ((last (or (math-check-for-commas t) (length math-exp-str)))
1888 (str (substring math-exp-str math-exp-pos last))
1889 (res
1890 (if (string-match "\\` *\\([a-zA-Z#][a-zA-Z0-9#]* *,? *\\)*:" str)
1891 (let ((str1 (substring str 0 (1- (match-end 0))))
1892 (str2 (substring str (match-end 0)))
1893 (calc-hashes-used 0))
1894 (setq str1 (math-read-expr (concat "[" str1 "]")))
1895 (if (eq (car-safe str1) 'error)
1896 str1
1897 (setq str2 (math-read-expr str2))
1898 (if (eq (car-safe str2) 'error)
1899 str2
1900 (append '(calcFunc-lambda) (cdr str1) (list str2)))))
1901 (if (string-match "#" str)
1902 (let ((calc-hashes-used 0))
1903 (and (setq str (math-read-expr str))
1904 (if (eq (car-safe str) 'error)
1905 str
1906 (append '(calcFunc-lambda)
1907 (calc-invent-args calc-hashes-used)
1908 (list str)))))
1909 (math-parse-date str)))))
1910 (if (stringp res)
1911 (throw 'syntax res))
1912 (if (eq (car-safe res) 'error)
1913 (throw 'syntax (nth 2 res)))
1914 (setq math-exp-pos (1+ last))
1915 (math-read-token)
1916 res))
1917
1918 (provide 'calc-forms)
1919
1920 ;;; arch-tag: a3d8f33b-9508-4043-8060-d02b8c9c750c
1921 ;;; calc-forms.el ends here