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