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