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