1 ;;; cal-julian.el --- calendar functions for the Julian calendar.
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Human-Keywords: Julian calendar, Julian day number, calendar, diary
9 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; This collection of functions implements the features of calendar.el and
28 ;; diary.el that deal with the Julian calendar.
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
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.
47 (calendar-sum y approx
48 (>= date
(calendar-absolute-from-julian (list 1 1 (1+ y
))))
50 (month ;; Search forward from January.
53 (calendar-absolute-from-julian
55 (if (and (= m
2) (= (% year
4) 0))
57 (aref [31 28 31 30 31 30 31 31 30 31 30 31]
61 (day ;; Calculate the day by subtraction.
62 (- date
(1- (calendar-absolute-from-julian (list month
1 year
))))))
63 (list month day year
)))
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)
75 1 0);; Correct for Julian but not Gregorian leap year.
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'."
85 (calendar-julian-from-absolute
86 (calendar-absolute-from-gregorian
87 (or date
(calendar-current-date))))
90 (defun calendar-print-julian-date ()
91 "Show the Julian calendar equivalent of the date under the cursor."
93 (message "Julian date: %s"
94 (calendar-julian-date-string (calendar-cursor-to-date t
))))
96 (defun calendar-goto-julian-date (date &optional noecho
)
97 "Move cursor to Julian DATE; echo Julian date unless NOECHO is t."
99 (let* ((today (calendar-current-date))
101 "Julian calendar year (>0): "
102 '(lambda (x) (> x
0))
104 (extract-calendar-year
105 (calendar-julian-from-absolute
106 (calendar-absolute-from-gregorian
108 (month-array calendar-month-name-array
)
109 (completion-ignore-case t
)
113 "Julian calendar month name: "
114 (mapcar 'list
(append month-array nil
))
116 (calendar-make-alist month-array
1 'capitalize
))))
118 (if (and (zerop (% year
4)) (= month
2))
120 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month
))))
122 (format "Julian calendar day (%d-%d): "
123 (if (and (= year
1) (= month
1)) 3 1) last
)
125 (and (< (if (and (= year
1) (= month
1)) 2 0) x
)
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)))
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
)
142 (increment-calendar-month m1 y1 -
1)
143 (increment-calendar-month m2 y2
1)
144 (let* ((start-date (calendar-absolute-from-gregorian
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
)))))))
159 (defun diary-julian-date ()
160 "Julian calendar equivalent of date diary entry."
161 (format "Julian date: %s" (calendar-julian-date-string date
)))
163 (defun calendar-absolute-from-astro (d)
164 "Absolute date of astronomical (Julian) day number D."
167 (defun calendar-astro-from-absolute (d)
168 "Astronomical (Julian) day number of absolute date D."
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."
176 (calendar-astro-from-absolute
177 (calendar-absolute-from-gregorian
178 (or date
(calendar-current-date)))))))
180 (defun calendar-print-astro-day-number ()
181 "Show astronomical (Julian) day number after noon UTC on date shown by cursor."
184 "Astronomical (Julian) day number (after noon UTC): %s"
185 (calendar-astro-date-string (calendar-cursor-to-date t
))))
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)))))
194 (calendar-gregorian-from-absolute
196 (calendar-absolute-from-astro daynumber
))))
197 (or noecho
(calendar-print-astro-day-number)))
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
)))
204 (provide 'cal-julian
)
206 ;;; cal-julian.el ends here