(calendar-julian-to-absolute): Rename calendar-absolute-from-julian.
[bpt/emacs.git] / lisp / calendar / cal-julian.el
1 ;;; cal-julian.el --- calendar functions for the Julian calendar
2
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
5
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Human-Keywords: Julian calendar, Julian day number, calendar, diary
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;; See calendar.el.
31
32 ;;; Code:
33
34 (require 'calendar)
35
36 (defun calendar-julian-to-absolute (date)
37 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
38 The Gregorian date Sunday, December 31, 1 BC is imaginary."
39 (let ((month (extract-calendar-month date))
40 (year (extract-calendar-year date)))
41 (+ (calendar-day-number date)
42 (if (and (zerop (% year 100))
43 (not (zerop (% year 400)))
44 (> month 2))
45 1 0) ; correct for Julian but not Gregorian leap year
46 (* 365 (1- year))
47 (/ (1- year) 4)
48 -2)))
49
50 (define-obsolete-function-alias 'calendar-absolute-from-julian
51 'calendar-julian-to-absolute "23.1")
52
53 ;;;###cal-autoload
54 (defun calendar-julian-from-absolute (date)
55 "Compute the Julian (month day year) corresponding to the absolute DATE.
56 The absolute date is the number of days elapsed since the (imaginary)
57 Gregorian date Sunday, December 31, 1 BC."
58 (let* ((approx (/ (+ date 2) 366)) ; approximation from below
59 (year ; search forward from the approximation
60 (+ approx
61 (calendar-sum y approx
62 (>= date (calendar-julian-to-absolute
63 (list 1 1 (1+ y))))
64 1)))
65 (month ; search forward from January
66 (1+ (calendar-sum m 1
67 (> date
68 (calendar-julian-to-absolute
69 (list m
70 (if (and (= m 2) (zerop (% year 4)))
71 29
72 (aref [31 28 31 30 31 30 31
73 31 30 31 30 31]
74 (1- m)))
75 year)))
76 1)))
77 (day ; calculate the day by subtraction
78 (- date (1- (calendar-julian-to-absolute (list month 1 year))))))
79 (list month day year)))
80
81 ;;;###cal-autoload
82 (defun calendar-julian-date-string (&optional date)
83 "String of Julian date of Gregorian DATE.
84 Defaults to today's date if DATE is not given.
85 Driven by the variable `calendar-date-display-form'."
86 (calendar-date-string
87 (calendar-julian-from-absolute
88 (calendar-absolute-from-gregorian (or date (calendar-current-date))))
89 nil t))
90
91 ;;;###cal-autoload
92 (defun calendar-julian-print-date ()
93 "Show the Julian calendar equivalent of the date under the cursor."
94 (interactive)
95 (message "Julian date: %s"
96 (calendar-julian-date-string (calendar-cursor-to-date t))))
97
98 (define-obsolete-function-alias 'calendar-print-julian-date
99 'calendar-julian-print-date "23.1")
100
101 ;;;###cal-autoload
102 (defun calendar-julian-goto-date (date &optional noecho)
103 "Move cursor to Julian DATE; echo Julian date unless NOECHO is non-nil."
104 (interactive
105 (let* ((today (calendar-current-date))
106 (year (calendar-read
107 "Julian calendar year (>0): "
108 (lambda (x) (> x 0))
109 (int-to-string
110 (extract-calendar-year
111 (calendar-julian-from-absolute
112 (calendar-absolute-from-gregorian
113 today))))))
114 (month-array calendar-month-name-array)
115 (completion-ignore-case t)
116 (month (cdr (assoc-string
117 (completing-read
118 "Julian calendar month name: "
119 (mapcar 'list (append month-array nil))
120 nil t)
121 (calendar-make-alist month-array 1) t)))
122 (last
123 (if (and (zerop (% year 4)) (= month 2))
124 29
125 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
126 (day (calendar-read
127 (format "Julian calendar day (%d-%d): "
128 (if (and (= year 1) (= month 1)) 3 1) last)
129 (lambda (x)
130 (and (< (if (and (= year 1) (= month 1)) 2 0) x)
131 (<= x last))))))
132 (list (list month day year))))
133 (calendar-goto-date (calendar-gregorian-from-absolute
134 (calendar-julian-to-absolute date)))
135 (or noecho (calendar-julian-print-date)))
136
137 (define-obsolete-function-alias 'calendar-goto-julian-date
138 'calendar-julian-goto-date "23.1")
139
140 ;;;###holiday-autoload
141 (defun holiday-julian (month day string)
142 "Holiday on MONTH, DAY (Julian) called STRING.
143 If MONTH, DAY (Julian) is visible, the value returned is corresponding
144 Gregorian date in the form of the list (((month day year) STRING)). Returns
145 nil if it is not visible in the current calendar window."
146 (let ((gdate (calendar-nongregorian-visible-p
147 month day 'calendar-julian-to-absolute
148 'calendar-julian-from-absolute
149 ;; In the Gregorian case, we'd use the lower year when
150 ;; month >= 11. In the Julian case, there is an offset
151 ;; of two weeks (ie 1 Nov Greg = 19 Oct Julian). So we
152 ;; use month >= 10, since it can't cause any problems.
153 (lambda (m) (< m 10)))))
154 (if gdate (list (list gdate string)))))
155
156 ;;;###cal-autoload
157 (defun calendar-astro-to-absolute (d)
158 "Absolute date of astronomical (Julian) day number D."
159 (- d 1721424.5))
160
161 (define-obsolete-function-alias 'calendar-absolute-from-astro
162 'calendar-astro-to-absolute "23.1")
163
164 ;;;###cal-autoload
165 (defun calendar-astro-from-absolute (d)
166 "Astronomical (Julian) day number of absolute date D."
167 (+ d 1721424.5))
168
169 ;;;###cal-autoload
170 (defun calendar-astro-date-string (&optional date)
171 "String of astronomical (Julian) day number after noon UTC of Gregorian DATE.
172 Defaults to today's date if DATE is not given."
173 (int-to-string
174 (ceiling
175 (calendar-astro-from-absolute
176 (calendar-absolute-from-gregorian (or date (calendar-current-date)))))))
177
178 ;;;###cal-autoload
179 (defun calendar-astro-print-day-number ()
180 "Show astronomical (Julian) day number after noon UTC on cursor date."
181 (interactive)
182 (message
183 "Astronomical (Julian) day number (at noon UTC): %s.0"
184 (calendar-astro-date-string (calendar-cursor-to-date t))))
185
186 (define-obsolete-function-alias 'calendar-print-astro-day-number
187 'calendar-astro-print-day-number "23.1")
188
189 ;;;###cal-autoload
190 (defun calendar-astro-goto-day-number (daynumber &optional noecho)
191 "Move cursor to astronomical (Julian) DAYNUMBER.
192 Echo astronomical (Julian) day number unless NOECHO is non-nil."
193 (interactive (list (calendar-read
194 "Astronomical (Julian) day number (>1721425): "
195 (lambda (x) (> x 1721425)))))
196 (calendar-goto-date
197 (calendar-gregorian-from-absolute
198 (floor
199 (calendar-astro-to-absolute daynumber))))
200 (or noecho (calendar-astro-print-day-number)))
201
202 (define-obsolete-function-alias 'calendar-goto-astro-day-number
203 'calendar-astro-goto-day-number "23.1")
204
205 (defvar date)
206
207 ;; To be called from list-sexp-diary-entries, where DATE is bound.
208 ;;;###diary-autoload
209 (defun diary-julian-date ()
210 "Julian calendar equivalent of date diary entry."
211 (format "Julian date: %s" (calendar-julian-date-string date)))
212
213 ;; To be called from list-sexp-diary-entries, where DATE is bound.
214 ;;;###diary-autoload
215 (defun diary-astro-day-number ()
216 "Astronomical (Julian) day number diary entry."
217 (format "Astronomical (Julian) day number at noon UTC: %s.0"
218 (calendar-astro-date-string date)))
219
220 (provide 'cal-julian)
221
222 ;; arch-tag: 0520acdd-1c60-4188-9aa8-9b8c24d856ae
223 ;;; cal-julian.el ends here