Commit | Line | Data |
---|---|---|
3afbc435 | 1 | ;;; cal-julian.el --- calendar functions for the Julian calendar |
0808d911 | 2 | |
f83f0d87 GM |
3 | ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
4 | ;; 2008 Free Software Foundation, Inc. | |
0808d911 ER |
5 | |
6 | ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu> | |
dbfca9c4 | 7 | ;; Maintainer: Glenn Morris <rgm@gnu.org> |
0808d911 ER |
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 | |
075969b4 | 15 | ;; the Free Software Foundation; either version 3, or (at your option) |
0808d911 ER |
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 | |
b578f267 | 24 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3a35cf56 LK |
25 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 | ;; Boston, MA 02110-1301, USA. | |
0808d911 ER |
27 | |
28 | ;;; Commentary: | |
29 | ||
551c8f1a | 30 | ;; See calendar.el. |
a96a5fca | 31 | |
0808d911 ER |
32 | ;;; Code: |
33 | ||
34 | (require 'calendar) | |
35 | ||
bd773d17 | 36 | (defun calendar-julian-to-absolute (date) |
c8ca95dd GM |
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." | |
e803eab7 GM |
39 | (let ((month (calendar-extract-month date)) |
40 | (year (calendar-extract-year date))) | |
c8ca95dd GM |
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 | ||
bd773d17 GM |
50 | (define-obsolete-function-alias 'calendar-absolute-from-julian |
51 | 'calendar-julian-to-absolute "23.1") | |
52 | ||
c24be81e | 53 | ;;;###cal-autoload |
0808d911 ER |
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." | |
863ad01b GM |
58 | (let* ((approx (/ (+ date 2) 366)) ; approximation from below |
59 | (year ; search forward from the approximation | |
0808d911 ER |
60 | (+ approx |
61 | (calendar-sum y approx | |
bd773d17 | 62 | (>= date (calendar-julian-to-absolute |
c8ca95dd GM |
63 | (list 1 1 (1+ y)))) |
64 | 1))) | |
863ad01b | 65 | (month ; search forward from January |
0808d911 | 66 | (1+ (calendar-sum m 1 |
c8ca95dd | 67 | (> date |
bd773d17 | 68 | (calendar-julian-to-absolute |
c8ca95dd GM |
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))) | |
863ad01b | 77 | (day ; calculate the day by subtraction |
bd773d17 | 78 | (- date (1- (calendar-julian-to-absolute (list month 1 year)))))) |
0808d911 ER |
79 | (list month day year))) |
80 | ||
c24be81e | 81 | ;;;###cal-autoload |
0808d911 ER |
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 | |
c8ca95dd | 88 | (calendar-absolute-from-gregorian (or date (calendar-current-date)))) |
0808d911 ER |
89 | nil t)) |
90 | ||
c24be81e | 91 | ;;;###cal-autoload |
bd773d17 | 92 | (defun calendar-julian-print-date () |
0808d911 ER |
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 | ||
bd773d17 GM |
98 | (define-obsolete-function-alias 'calendar-print-julian-date |
99 | 'calendar-julian-print-date "23.1") | |
100 | ||
c24be81e | 101 | ;;;###cal-autoload |
bd773d17 | 102 | (defun calendar-julian-goto-date (date &optional noecho) |
c8ca95dd | 103 | "Move cursor to Julian DATE; echo Julian date unless NOECHO is non-nil." |
0808d911 ER |
104 | (interactive |
105 | (let* ((today (calendar-current-date)) | |
106 | (year (calendar-read | |
107 | "Julian calendar year (>0): " | |
d383fd97 | 108 | (lambda (x) (> x 0)) |
d92bcf94 | 109 | (number-to-string |
e803eab7 | 110 | (calendar-extract-year |
0808d911 ER |
111 | (calendar-julian-from-absolute |
112 | (calendar-absolute-from-gregorian | |
113 | today)))))) | |
114 | (month-array calendar-month-name-array) | |
115 | (completion-ignore-case t) | |
2ad9ebbd | 116 | (month (cdr (assoc-string |
c8ca95dd GM |
117 | (completing-read |
118 | "Julian calendar month name: " | |
119 | (mapcar 'list (append month-array nil)) | |
120 | nil t) | |
2ad9ebbd | 121 | (calendar-make-alist month-array 1) t))) |
a1506d29 | 122 | (last |
0808d911 ER |
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) | |
d383fd97 | 129 | (lambda (x) |
c8ca95dd GM |
130 | (and (< (if (and (= year 1) (= month 1)) 2 0) x) |
131 | (<= x last)))))) | |
0808d911 ER |
132 | (list (list month day year)))) |
133 | (calendar-goto-date (calendar-gregorian-from-absolute | |
bd773d17 GM |
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") | |
0808d911 | 139 | |
c24be81e | 140 | ;;;###holiday-autoload |
0808d911 | 141 | (defun holiday-julian (month day string) |
863ad01b | 142 | "Holiday on MONTH, DAY (Julian) called STRING. |
0808d911 ER |
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." | |
589117b4 | 146 | (let ((gdate (calendar-nongregorian-visible-p |
bd773d17 | 147 | month day 'calendar-julian-to-absolute |
589117b4 GM |
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))))) | |
0808d911 | 155 | |
c24be81e | 156 | ;;;###cal-autoload |
bd773d17 | 157 | (defun calendar-astro-to-absolute (d) |
4ed43fd3 | 158 | "Absolute date of astronomical (Julian) day number D." |
0808d911 ER |
159 | (- d 1721424.5)) |
160 | ||
bd773d17 GM |
161 | (define-obsolete-function-alias 'calendar-absolute-from-astro |
162 | 'calendar-astro-to-absolute "23.1") | |
163 | ||
c24be81e | 164 | ;;;###cal-autoload |
0808d911 ER |
165 | (defun calendar-astro-from-absolute (d) |
166 | "Astronomical (Julian) day number of absolute date D." | |
167 | (+ d 1721424.5)) | |
168 | ||
c24be81e | 169 | ;;;###cal-autoload |
0808d911 ER |
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." | |
d92bcf94 | 173 | (number-to-string |
0808d911 ER |
174 | (ceiling |
175 | (calendar-astro-from-absolute | |
c8ca95dd | 176 | (calendar-absolute-from-gregorian (or date (calendar-current-date))))))) |
0808d911 | 177 | |
c24be81e | 178 | ;;;###cal-autoload |
bd773d17 | 179 | (defun calendar-astro-print-day-number () |
863ad01b | 180 | "Show astronomical (Julian) day number after noon UTC on cursor date." |
0808d911 ER |
181 | (interactive) |
182 | (message | |
e9703897 | 183 | "Astronomical (Julian) day number (at noon UTC): %s.0" |
0808d911 ER |
184 | (calendar-astro-date-string (calendar-cursor-to-date t)))) |
185 | ||
bd773d17 GM |
186 | (define-obsolete-function-alias 'calendar-print-astro-day-number |
187 | 'calendar-astro-print-day-number "23.1") | |
188 | ||
c24be81e | 189 | ;;;###cal-autoload |
bd773d17 | 190 | (defun calendar-astro-goto-day-number (daynumber &optional noecho) |
0808d911 | 191 | "Move cursor to astronomical (Julian) DAYNUMBER. |
c8ca95dd | 192 | Echo astronomical (Julian) day number unless NOECHO is non-nil." |
0808d911 ER |
193 | (interactive (list (calendar-read |
194 | "Astronomical (Julian) day number (>1721425): " | |
d383fd97 | 195 | (lambda (x) (> x 1721425))))) |
0808d911 ER |
196 | (calendar-goto-date |
197 | (calendar-gregorian-from-absolute | |
198 | (floor | |
bd773d17 GM |
199 | (calendar-astro-to-absolute daynumber)))) |
200 | (or noecho (calendar-astro-print-day-number))) | |
0808d911 | 201 | |
bd773d17 GM |
202 | (define-obsolete-function-alias 'calendar-goto-astro-day-number |
203 | 'calendar-astro-goto-day-number "23.1") | |
c24be81e GM |
204 | |
205 | (defvar date) | |
206 | ||
8c34d83e | 207 | ;; To be called from diary-list-sexp-entries, where DATE is bound. |
c24be81e GM |
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 | ||
8c34d83e | 213 | ;; To be called from diary-list-sexp-entries, where DATE is bound. |
c24be81e | 214 | ;;;###diary-autoload |
0808d911 ER |
215 | (defun diary-astro-day-number () |
216 | "Astronomical (Julian) day number diary entry." | |
75697ad9 | 217 | (format "Astronomical (Julian) day number at noon UTC: %s.0" |
0808d911 ER |
218 | (calendar-astro-date-string date))) |
219 | ||
220 | (provide 'cal-julian) | |
221 | ||
d383fd97 | 222 | ;; arch-tag: 0520acdd-1c60-4188-9aa8-9b8c24d856ae |
0808d911 | 223 | ;;; cal-julian.el ends here |