1 ;;; cal-julian.el --- calendar functions for the Julian calendar
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
9 ;; Human-Keywords: Julian calendar, Julian day number, calendar, diary
11 ;; This file is part of GNU Emacs.
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)
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.
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.
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)))
45 1 0) ; correct for Julian but not Gregorian leap year
50 (define-obsolete-function-alias 'calendar-absolute-from-julian
51 'calendar-julian-to-absolute
"23.1")
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
61 (calendar-sum y approx
62 (>= date
(calendar-julian-to-absolute
65 (month ; search forward from January
68 (calendar-julian-to-absolute
70 (if (and (= m
2) (zerop (% year
4)))
72 (aref [31 28 31 30 31 30 31
77 (day ; calculate the day by subtraction
78 (- date
(1- (calendar-julian-to-absolute (list month
1 year
))))))
79 (list month day year
)))
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'."
87 (calendar-julian-from-absolute
88 (calendar-absolute-from-gregorian (or date
(calendar-current-date))))
92 (defun calendar-julian-print-date ()
93 "Show the Julian calendar equivalent of the date under the cursor."
95 (message "Julian date: %s"
96 (calendar-julian-date-string (calendar-cursor-to-date t
))))
98 (define-obsolete-function-alias 'calendar-print-julian-date
99 'calendar-julian-print-date
"23.1")
102 (defun calendar-julian-goto-date (date &optional noecho
)
103 "Move cursor to Julian DATE; echo Julian date unless NOECHO is non-nil."
105 (let* ((today (calendar-current-date))
107 "Julian calendar year (>0): "
110 (extract-calendar-year
111 (calendar-julian-from-absolute
112 (calendar-absolute-from-gregorian
114 (month-array calendar-month-name-array
)
115 (completion-ignore-case t
)
116 (month (cdr (assoc-string
118 "Julian calendar month name: "
119 (mapcar 'list
(append month-array nil
))
121 (calendar-make-alist month-array
1) t
)))
123 (if (and (zerop (% year
4)) (= month
2))
125 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month
))))
127 (format "Julian calendar day (%d-%d): "
128 (if (and (= year
1) (= month
1)) 3 1) last
)
130 (and (< (if (and (= year
1) (= month
1)) 2 0) x
)
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)))
137 (define-obsolete-function-alias 'calendar-goto-julian-date
138 'calendar-julian-goto-date
"23.1")
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
)))))
157 (defun calendar-astro-to-absolute (d)
158 "Absolute date of astronomical (Julian) day number D."
161 (define-obsolete-function-alias 'calendar-absolute-from-astro
162 'calendar-astro-to-absolute
"23.1")
165 (defun calendar-astro-from-absolute (d)
166 "Astronomical (Julian) day number of absolute date D."
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."
175 (calendar-astro-from-absolute
176 (calendar-absolute-from-gregorian (or date
(calendar-current-date)))))))
179 (defun calendar-astro-print-day-number ()
180 "Show astronomical (Julian) day number after noon UTC on cursor date."
183 "Astronomical (Julian) day number (at noon UTC): %s.0"
184 (calendar-astro-date-string (calendar-cursor-to-date t
))))
186 (define-obsolete-function-alias 'calendar-print-astro-day-number
187 'calendar-astro-print-day-number
"23.1")
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)))))
197 (calendar-gregorian-from-absolute
199 (calendar-astro-to-absolute daynumber
))))
200 (or noecho
(calendar-astro-print-day-number)))
202 (define-obsolete-function-alias 'calendar-goto-astro-day-number
203 'calendar-astro-goto-day-number
"23.1")
207 ;; To be called from list-sexp-diary-entries, where DATE is bound.
209 (defun diary-julian-date ()
210 "Julian calendar equivalent of date diary entry."
211 (format "Julian date: %s" (calendar-julian-date-string date
)))
213 ;; To be called from list-sexp-diary-entries, where DATE is bound.
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
)))
220 (provide 'cal-julian
)
222 ;; arch-tag: 0520acdd-1c60-4188-9aa8-9b8c24d856ae
223 ;;; cal-julian.el ends here