Update copyright info.
[bpt/emacs.git] / lisp / calendar / cal-julian.el
CommitLineData
3afbc435 1;;; cal-julian.el --- calendar functions for the Julian calendar
0808d911 2
a96a5fca 3;; Copyright (C) 1995, 1997 Free Software Foundation, Inc.
0808d911
ER
4
5;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6;; Keywords: calendar
7;; Human-Keywords: Julian calendar, Julian day number, calendar, diary
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
0808d911
ER
25
26;;; Commentary:
27
28;; This collection of functions implements the features of calendar.el and
29;; diary.el that deal with the Julian calendar.
30
a96a5fca 31;; Technical details of all the calendrical calculations can be found in
fffaba77
PE
32;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold
33;; and Nachum Dershowitz, Cambridge University Press (2001).
a96a5fca 34
0808d911
ER
35;; Comments, corrections, and improvements should be sent to
36;; Edward M. Reingold Department of Computer Science
37;; (217) 333-6733 University of Illinois at Urbana-Champaign
38;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
39;; Urbana, Illinois 61801
40
41;;; Code:
42
04ec3414 43(defvar date)
0b0f8fa4
GM
44(defvar displayed-month)
45(defvar displayed-year)
46
0808d911
ER
47(require 'calendar)
48
49(defun calendar-julian-from-absolute (date)
50 "Compute the Julian (month day year) corresponding to the absolute DATE.
51The absolute date is the number of days elapsed since the (imaginary)
52Gregorian date Sunday, December 31, 1 BC."
53 (let* ((approx (/ (+ date 2) 366));; Approximation from below.
54 (year ;; Search forward from the approximation.
55 (+ approx
56 (calendar-sum y approx
57 (>= date (calendar-absolute-from-julian (list 1 1 (1+ y))))
58 1)))
59 (month ;; Search forward from January.
60 (1+ (calendar-sum m 1
61 (> date
62 (calendar-absolute-from-julian
63 (list m
64 (if (and (= m 2) (= (% year 4) 0))
65 29
66 (aref [31 28 31 30 31 30 31 31 30 31 30 31]
67 (1- m)))
68 year)))
69 1)))
70 (day ;; Calculate the day by subtraction.
71 (- date (1- (calendar-absolute-from-julian (list month 1 year))))))
72 (list month day year)))
73
74(defun calendar-absolute-from-julian (date)
75 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
76The Gregorian date Sunday, December 31, 1 BC is imaginary."
77 (let ((month (extract-calendar-month date))
78 (day (extract-calendar-day date))
79 (year (extract-calendar-year date)))
80 (+ (calendar-day-number date)
81 (if (and (= (% year 100) 0)
82 (/= (% year 400) 0)
83 (> month 2))
84 1 0);; Correct for Julian but not Gregorian leap year.
85 (* 365 (1- year))
86 (/ (1- year) 4)
87 -2)))
88
89(defun calendar-julian-date-string (&optional date)
90 "String of Julian date of Gregorian DATE.
91Defaults to today's date if DATE is not given.
92Driven by the variable `calendar-date-display-form'."
93 (calendar-date-string
94 (calendar-julian-from-absolute
95 (calendar-absolute-from-gregorian
96 (or date (calendar-current-date))))
97 nil t))
98
99(defun calendar-print-julian-date ()
100 "Show the Julian calendar equivalent of the date under the cursor."
101 (interactive)
102 (message "Julian date: %s"
103 (calendar-julian-date-string (calendar-cursor-to-date t))))
104
105(defun calendar-goto-julian-date (date &optional noecho)
106 "Move cursor to Julian DATE; echo Julian date unless NOECHO is t."
107 (interactive
108 (let* ((today (calendar-current-date))
109 (year (calendar-read
110 "Julian calendar year (>0): "
111 '(lambda (x) (> x 0))
112 (int-to-string
113 (extract-calendar-year
114 (calendar-julian-from-absolute
115 (calendar-absolute-from-gregorian
116 today))))))
117 (month-array calendar-month-name-array)
118 (completion-ignore-case t)
2ad9ebbd 119 (month (cdr (assoc-string
0808d911
ER
120 (completing-read
121 "Julian calendar month name: "
122 (mapcar 'list (append month-array nil))
b5394859 123 nil t)
2ad9ebbd 124 (calendar-make-alist month-array 1) t)))
a1506d29 125 (last
0808d911
ER
126 (if (and (zerop (% year 4)) (= month 2))
127 29
128 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
129 (day (calendar-read
130 (format "Julian calendar day (%d-%d): "
131 (if (and (= year 1) (= month 1)) 3 1) last)
a1506d29 132 '(lambda (x)
0808d911
ER
133 (and (< (if (and (= year 1) (= month 1)) 2 0) x)
134 (<= x last))))))
135 (list (list month day year))))
136 (calendar-goto-date (calendar-gregorian-from-absolute
137 (calendar-absolute-from-julian date)))
138 (or noecho (calendar-print-julian-date)))
139
140(defun holiday-julian (month day string)
141 "Holiday on MONTH, DAY (Julian) called STRING.
142If MONTH, DAY (Julian) is visible, the value returned is corresponding
143Gregorian date in the form of the list (((month day year) STRING)). Returns
144nil if it is not visible in the current calendar window."
145 (let ((m1 displayed-month)
146 (y1 displayed-year)
147 (m2 displayed-month)
148 (y2 displayed-year)
149 (year))
150 (increment-calendar-month m1 y1 -1)
151 (increment-calendar-month m2 y2 1)
152 (let* ((start-date (calendar-absolute-from-gregorian
153 (list m1 1 y1)))
154 (end-date (calendar-absolute-from-gregorian
155 (list m2 (calendar-last-day-of-month m2 y2) y2)))
156 (julian-start (calendar-julian-from-absolute start-date))
157 (julian-end (calendar-julian-from-absolute end-date))
158 (julian-y1 (extract-calendar-year julian-start))
159 (julian-y2 (extract-calendar-year julian-end)))
160 (setq year (if (< 10 month) julian-y1 julian-y2))
161 (let ((date (calendar-gregorian-from-absolute
162 (calendar-absolute-from-julian
163 (list month day year)))))
164 (if (calendar-date-is-visible-p date)
165 (list (list date string)))))))
166
167(defun diary-julian-date ()
168 "Julian calendar equivalent of date diary entry."
169 (format "Julian date: %s" (calendar-julian-date-string date)))
170
171(defun calendar-absolute-from-astro (d)
4ed43fd3 172 "Absolute date of astronomical (Julian) day number D."
0808d911
ER
173 (- d 1721424.5))
174
175(defun calendar-astro-from-absolute (d)
176 "Astronomical (Julian) day number of absolute date D."
177 (+ d 1721424.5))
178
179(defun calendar-astro-date-string (&optional date)
180 "String of astronomical (Julian) day number after noon UTC of Gregorian DATE.
181Defaults to today's date if DATE is not given."
182 (int-to-string
183 (ceiling
184 (calendar-astro-from-absolute
185 (calendar-absolute-from-gregorian
186 (or date (calendar-current-date)))))))
187
188(defun calendar-print-astro-day-number ()
189 "Show astronomical (Julian) day number after noon UTC on date shown by cursor."
190 (interactive)
191 (message
e9703897 192 "Astronomical (Julian) day number (at noon UTC): %s.0"
0808d911
ER
193 (calendar-astro-date-string (calendar-cursor-to-date t))))
194
195(defun calendar-goto-astro-day-number (daynumber &optional noecho)
196 "Move cursor to astronomical (Julian) DAYNUMBER.
197Echo astronomical (Julian) day number unless NOECHO is t."
198 (interactive (list (calendar-read
199 "Astronomical (Julian) day number (>1721425): "
200 '(lambda (x) (> x 1721425)))))
201 (calendar-goto-date
202 (calendar-gregorian-from-absolute
203 (floor
204 (calendar-absolute-from-astro daynumber))))
205 (or noecho (calendar-print-astro-day-number)))
206
207(defun diary-astro-day-number ()
208 "Astronomical (Julian) day number diary entry."
75697ad9 209 (format "Astronomical (Julian) day number at noon UTC: %s.0"
0808d911
ER
210 (calendar-astro-date-string date)))
211
212(provide 'cal-julian)
213
ab5796a9 214;;; arch-tag: 0520acdd-1c60-4188-9aa8-9b8c24d856ae
0808d911 215;;; cal-julian.el ends here