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