annotate lisp/calc/calc-forms.el @ 82791:25892386493b

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