Comment fixes.
[bpt/emacs.git] / lisp / calendar / cal-julian.el
1 ;;; cal-julian.el --- calendar functions for the Julian calendar.
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
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
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;; This collection of functions implements the features of calendar.el and
28 ;; diary.el that deal with the Julian calendar.
29
30 ;; Comments, corrections, and improvements should be sent to
31 ;; Edward M. Reingold Department of Computer Science
32 ;; (217) 333-6733 University of Illinois at Urbana-Champaign
33 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
34 ;; Urbana, Illinois 61801
35
36 ;;; Code:
37
38 (require 'calendar)
39
40 (defun calendar-julian-from-absolute (date)
41 "Compute the Julian (month day year) corresponding to the absolute DATE.
42 The absolute date is the number of days elapsed since the (imaginary)
43 Gregorian date Sunday, December 31, 1 BC."
44 (let* ((approx (/ (+ date 2) 366));; Approximation from below.
45 (year ;; Search forward from the approximation.
46 (+ approx
47 (calendar-sum y approx
48 (>= date (calendar-absolute-from-julian (list 1 1 (1+ y))))
49 1)))
50 (month ;; Search forward from January.
51 (1+ (calendar-sum m 1
52 (> date
53 (calendar-absolute-from-julian
54 (list m
55 (if (and (= m 2) (= (% year 4) 0))
56 29
57 (aref [31 28 31 30 31 30 31 31 30 31 30 31]
58 (1- m)))
59 year)))
60 1)))
61 (day ;; Calculate the day by subtraction.
62 (- date (1- (calendar-absolute-from-julian (list month 1 year))))))
63 (list month day year)))
64
65 (defun calendar-absolute-from-julian (date)
66 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
67 The Gregorian date Sunday, December 31, 1 BC is imaginary."
68 (let ((month (extract-calendar-month date))
69 (day (extract-calendar-day date))
70 (year (extract-calendar-year date)))
71 (+ (calendar-day-number date)
72 (if (and (= (% year 100) 0)
73 (/= (% year 400) 0)
74 (> month 2))
75 1 0);; Correct for Julian but not Gregorian leap year.
76 (* 365 (1- year))
77 (/ (1- year) 4)
78 -2)))
79
80 (defun calendar-julian-date-string (&optional date)
81 "String of Julian date of Gregorian DATE.
82 Defaults to today's date if DATE is not given.
83 Driven by the variable `calendar-date-display-form'."
84 (calendar-date-string
85 (calendar-julian-from-absolute
86 (calendar-absolute-from-gregorian
87 (or date (calendar-current-date))))
88 nil t))
89
90 (defun calendar-print-julian-date ()
91 "Show the Julian calendar equivalent of the date under the cursor."
92 (interactive)
93 (message "Julian date: %s"
94 (calendar-julian-date-string (calendar-cursor-to-date t))))
95
96 (defun calendar-goto-julian-date (date &optional noecho)
97 "Move cursor to Julian DATE; echo Julian date unless NOECHO is t."
98 (interactive
99 (let* ((today (calendar-current-date))
100 (year (calendar-read
101 "Julian calendar year (>0): "
102 '(lambda (x) (> x 0))
103 (int-to-string
104 (extract-calendar-year
105 (calendar-julian-from-absolute
106 (calendar-absolute-from-gregorian
107 today))))))
108 (month-array calendar-month-name-array)
109 (completion-ignore-case t)
110 (month (cdr (assoc
111 (capitalize
112 (completing-read
113 "Julian calendar month name: "
114 (mapcar 'list (append month-array nil))
115 nil t))
116 (calendar-make-alist month-array 1 'capitalize))))
117 (last
118 (if (and (zerop (% year 4)) (= month 2))
119 29
120 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
121 (day (calendar-read
122 (format "Julian calendar day (%d-%d): "
123 (if (and (= year 1) (= month 1)) 3 1) last)
124 '(lambda (x)
125 (and (< (if (and (= year 1) (= month 1)) 2 0) x)
126 (<= x last))))))
127 (list (list month day year))))
128 (calendar-goto-date (calendar-gregorian-from-absolute
129 (calendar-absolute-from-julian date)))
130 (or noecho (calendar-print-julian-date)))
131
132 (defun holiday-julian (month day string)
133 "Holiday on MONTH, DAY (Julian) called STRING.
134 If MONTH, DAY (Julian) is visible, the value returned is corresponding
135 Gregorian date in the form of the list (((month day year) STRING)). Returns
136 nil if it is not visible in the current calendar window."
137 (let ((m1 displayed-month)
138 (y1 displayed-year)
139 (m2 displayed-month)
140 (y2 displayed-year)
141 (year))
142 (increment-calendar-month m1 y1 -1)
143 (increment-calendar-month m2 y2 1)
144 (let* ((start-date (calendar-absolute-from-gregorian
145 (list m1 1 y1)))
146 (end-date (calendar-absolute-from-gregorian
147 (list m2 (calendar-last-day-of-month m2 y2) y2)))
148 (julian-start (calendar-julian-from-absolute start-date))
149 (julian-end (calendar-julian-from-absolute end-date))
150 (julian-y1 (extract-calendar-year julian-start))
151 (julian-y2 (extract-calendar-year julian-end)))
152 (setq year (if (< 10 month) julian-y1 julian-y2))
153 (let ((date (calendar-gregorian-from-absolute
154 (calendar-absolute-from-julian
155 (list month day year)))))
156 (if (calendar-date-is-visible-p date)
157 (list (list date string)))))))
158
159 (defun diary-julian-date ()
160 "Julian calendar equivalent of date diary entry."
161 (format "Julian date: %s" (calendar-julian-date-string date)))
162
163 (defun calendar-absolute-from-astro (d)
164 "Absolute date of astronomical (Julian) day number D."
165 (- d 1721424.5))
166
167 (defun calendar-astro-from-absolute (d)
168 "Astronomical (Julian) day number of absolute date D."
169 (+ d 1721424.5))
170
171 (defun calendar-astro-date-string (&optional date)
172 "String of astronomical (Julian) day number after noon UTC of Gregorian DATE.
173 Defaults to today's date if DATE is not given."
174 (int-to-string
175 (ceiling
176 (calendar-astro-from-absolute
177 (calendar-absolute-from-gregorian
178 (or date (calendar-current-date)))))))
179
180 (defun calendar-print-astro-day-number ()
181 "Show astronomical (Julian) day number after noon UTC on date shown by cursor."
182 (interactive)
183 (message
184 "Astronomical (Julian) day number (after noon UTC): %s"
185 (calendar-astro-date-string (calendar-cursor-to-date t))))
186
187 (defun calendar-goto-astro-day-number (daynumber &optional noecho)
188 "Move cursor to astronomical (Julian) DAYNUMBER.
189 Echo astronomical (Julian) day number unless NOECHO is t."
190 (interactive (list (calendar-read
191 "Astronomical (Julian) day number (>1721425): "
192 '(lambda (x) (> x 1721425)))))
193 (calendar-goto-date
194 (calendar-gregorian-from-absolute
195 (floor
196 (calendar-absolute-from-astro daynumber))))
197 (or noecho (calendar-print-astro-day-number)))
198
199 (defun diary-astro-day-number ()
200 "Astronomical (Julian) day number diary entry."
201 (format "Astronomical (Julian) day number %s"
202 (calendar-astro-date-string date)))
203
204 (provide 'cal-julian)
205
206 ;;; cal-julian.el ends here