Mercurial > emacs
annotate lisp/calendar/cal-julian.el @ 52149:9928a29a3e80
(Library Headers): Explain where to put -*-.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 06 Aug 2003 01:42:44 +0000 |
parents | e7d0572ccca5 |
children | 695cf19ef79e |
rev | line source |
---|---|
38422
7a94f1c588c4
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
24189
diff
changeset
|
1 ;;; cal-julian.el --- calendar functions for the Julian calendar |
13053 | 2 |
20462
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
17380
diff
changeset
|
3 ;; Copyright (C) 1995, 1997 Free Software Foundation, Inc. |
13053 | 4 |
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu> | |
6 ;; Keywords: calendar | |
7 ;; Human-Keywords: Julian calendar, Julian day number, calendar, diary | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
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) | |
14 ;; any later version. | |
15 | |
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. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
14169 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
13053 | 25 |
26 ;;; Commentary: | |
27 | |
28 ;; This collection of functions implements the features of calendar.el and | |
29 ;; diary.el that deal with the Julian calendar. | |
30 | |
20462
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
17380
diff
changeset
|
31 ;; Technical details of all the calendrical calculations can be found in |
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
17380
diff
changeset
|
32 ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold, |
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
17380
diff
changeset
|
33 ;; Cambridge University Press (1997). |
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
17380
diff
changeset
|
34 |
13053 | 35 ;; Comments, corrections, and improvements should be sent to |
36 ;; Edward M. Reingold Department of Computer Science | |
37 ;; (217) 333-6733 University of Illinois at Urbana-Champaign | |
38 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue | |
39 ;; Urbana, Illinois 61801 | |
40 | |
41 ;;; Code: | |
42 | |
52112
e7d0572ccca5
(displayed-month, displayed-year): Define for compiler.
Glenn Morris <rgm@gnu.org>
parents:
49598
diff
changeset
|
43 (defvar displayed-month) |
e7d0572ccca5
(displayed-month, displayed-year): Define for compiler.
Glenn Morris <rgm@gnu.org>
parents:
49598
diff
changeset
|
44 (defvar displayed-year) |
e7d0572ccca5
(displayed-month, displayed-year): Define for compiler.
Glenn Morris <rgm@gnu.org>
parents:
49598
diff
changeset
|
45 |
13053 | 46 (require 'calendar) |
47 | |
48 (defun calendar-julian-from-absolute (date) | |
49 "Compute the Julian (month day year) corresponding to the absolute DATE. | |
50 The absolute date is the number of days elapsed since the (imaginary) | |
51 Gregorian date Sunday, December 31, 1 BC." | |
52 (let* ((approx (/ (+ date 2) 366));; Approximation from below. | |
53 (year ;; Search forward from the approximation. | |
54 (+ approx | |
55 (calendar-sum y approx | |
56 (>= date (calendar-absolute-from-julian (list 1 1 (1+ y)))) | |
57 1))) | |
58 (month ;; Search forward from January. | |
59 (1+ (calendar-sum m 1 | |
60 (> date | |
61 (calendar-absolute-from-julian | |
62 (list m | |
63 (if (and (= m 2) (= (% year 4) 0)) | |
64 29 | |
65 (aref [31 28 31 30 31 30 31 31 30 31 30 31] | |
66 (1- m))) | |
67 year))) | |
68 1))) | |
69 (day ;; Calculate the day by subtraction. | |
70 (- date (1- (calendar-absolute-from-julian (list month 1 year)))))) | |
71 (list month day year))) | |
72 | |
73 (defun calendar-absolute-from-julian (date) | |
74 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE. | |
75 The Gregorian date Sunday, December 31, 1 BC is imaginary." | |
76 (let ((month (extract-calendar-month date)) | |
77 (day (extract-calendar-day date)) | |
78 (year (extract-calendar-year date))) | |
79 (+ (calendar-day-number date) | |
80 (if (and (= (% year 100) 0) | |
81 (/= (% year 400) 0) | |
82 (> month 2)) | |
83 1 0);; Correct for Julian but not Gregorian leap year. | |
84 (* 365 (1- year)) | |
85 (/ (1- year) 4) | |
86 -2))) | |
87 | |
88 (defun calendar-julian-date-string (&optional date) | |
89 "String of Julian date of Gregorian DATE. | |
90 Defaults to today's date if DATE is not given. | |
91 Driven by the variable `calendar-date-display-form'." | |
92 (calendar-date-string | |
93 (calendar-julian-from-absolute | |
94 (calendar-absolute-from-gregorian | |
95 (or date (calendar-current-date)))) | |
96 nil t)) | |
97 | |
98 (defun calendar-print-julian-date () | |
99 "Show the Julian calendar equivalent of the date under the cursor." | |
100 (interactive) | |
101 (message "Julian date: %s" | |
102 (calendar-julian-date-string (calendar-cursor-to-date t)))) | |
103 | |
104 (defun calendar-goto-julian-date (date &optional noecho) | |
105 "Move cursor to Julian DATE; echo Julian date unless NOECHO is t." | |
106 (interactive | |
107 (let* ((today (calendar-current-date)) | |
108 (year (calendar-read | |
109 "Julian calendar year (>0): " | |
110 '(lambda (x) (> x 0)) | |
111 (int-to-string | |
112 (extract-calendar-year | |
113 (calendar-julian-from-absolute | |
114 (calendar-absolute-from-gregorian | |
115 today)))))) | |
116 (month-array calendar-month-name-array) | |
117 (completion-ignore-case t) | |
24189
c70c6c750126
(calendar-goto-julian-date): Use assoc-ignore-case and do not
Richard M. Stallman <rms@gnu.org>
parents:
20462
diff
changeset
|
118 (month (cdr (assoc-ignore-case |
13053 | 119 (completing-read |
120 "Julian calendar month name: " | |
121 (mapcar 'list (append month-array nil)) | |
24189
c70c6c750126
(calendar-goto-julian-date): Use assoc-ignore-case and do not
Richard M. Stallman <rms@gnu.org>
parents:
20462
diff
changeset
|
122 nil t) |
c70c6c750126
(calendar-goto-julian-date): Use assoc-ignore-case and do not
Richard M. Stallman <rms@gnu.org>
parents:
20462
diff
changeset
|
123 (calendar-make-alist month-array 1)))) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38422
diff
changeset
|
124 (last |
13053 | 125 (if (and (zerop (% year 4)) (= month 2)) |
126 29 | |
127 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month)))) | |
128 (day (calendar-read | |
129 (format "Julian calendar day (%d-%d): " | |
130 (if (and (= year 1) (= month 1)) 3 1) last) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38422
diff
changeset
|
131 '(lambda (x) |
13053 | 132 (and (< (if (and (= year 1) (= month 1)) 2 0) x) |
133 (<= x last)))))) | |
134 (list (list month day year)))) | |
135 (calendar-goto-date (calendar-gregorian-from-absolute | |
136 (calendar-absolute-from-julian date))) | |
137 (or noecho (calendar-print-julian-date))) | |
138 | |
139 (defun holiday-julian (month day string) | |
140 "Holiday on MONTH, DAY (Julian) called STRING. | |
141 If MONTH, DAY (Julian) is visible, the value returned is corresponding | |
142 Gregorian date in the form of the list (((month day year) STRING)). Returns | |
143 nil if it is not visible in the current calendar window." | |
144 (let ((m1 displayed-month) | |
145 (y1 displayed-year) | |
146 (m2 displayed-month) | |
147 (y2 displayed-year) | |
148 (year)) | |
149 (increment-calendar-month m1 y1 -1) | |
150 (increment-calendar-month m2 y2 1) | |
151 (let* ((start-date (calendar-absolute-from-gregorian | |
152 (list m1 1 y1))) | |
153 (end-date (calendar-absolute-from-gregorian | |
154 (list m2 (calendar-last-day-of-month m2 y2) y2))) | |
155 (julian-start (calendar-julian-from-absolute start-date)) | |
156 (julian-end (calendar-julian-from-absolute end-date)) | |
157 (julian-y1 (extract-calendar-year julian-start)) | |
158 (julian-y2 (extract-calendar-year julian-end))) | |
159 (setq year (if (< 10 month) julian-y1 julian-y2)) | |
160 (let ((date (calendar-gregorian-from-absolute | |
161 (calendar-absolute-from-julian | |
162 (list month day year))))) | |
163 (if (calendar-date-is-visible-p date) | |
164 (list (list date string))))))) | |
165 | |
166 (defun diary-julian-date () | |
167 "Julian calendar equivalent of date diary entry." | |
168 (format "Julian date: %s" (calendar-julian-date-string date))) | |
169 | |
170 (defun calendar-absolute-from-astro (d) | |
13673
da11ffac4f8b
(calendar-absolute-from-astro): Doc fix.
Paul Eggert <eggert@twinsun.com>
parents:
13053
diff
changeset
|
171 "Absolute date of astronomical (Julian) day number D." |
13053 | 172 (- d 1721424.5)) |
173 | |
174 (defun calendar-astro-from-absolute (d) | |
175 "Astronomical (Julian) day number of absolute date D." | |
176 (+ d 1721424.5)) | |
177 | |
178 (defun calendar-astro-date-string (&optional date) | |
179 "String of astronomical (Julian) day number after noon UTC of Gregorian DATE. | |
180 Defaults to today's date if DATE is not given." | |
181 (int-to-string | |
182 (ceiling | |
183 (calendar-astro-from-absolute | |
184 (calendar-absolute-from-gregorian | |
185 (or date (calendar-current-date))))))) | |
186 | |
187 (defun calendar-print-astro-day-number () | |
188 "Show astronomical (Julian) day number after noon UTC on date shown by cursor." | |
189 (interactive) | |
190 (message | |
15069
6237f2b08205
Spelling error.
Edward M. Reingold <reingold@emr.cs.iit.edu>
parents:
14169
diff
changeset
|
191 "Astronomical (Julian) day number (at noon UTC): %s.0" |
13053 | 192 (calendar-astro-date-string (calendar-cursor-to-date t)))) |
193 | |
194 (defun calendar-goto-astro-day-number (daynumber &optional noecho) | |
195 "Move cursor to astronomical (Julian) DAYNUMBER. | |
196 Echo astronomical (Julian) day number unless NOECHO is t." | |
197 (interactive (list (calendar-read | |
198 "Astronomical (Julian) day number (>1721425): " | |
199 '(lambda (x) (> x 1721425))))) | |
200 (calendar-goto-date | |
201 (calendar-gregorian-from-absolute | |
202 (floor | |
203 (calendar-absolute-from-astro daynumber)))) | |
204 (or noecho (calendar-print-astro-day-number))) | |
205 | |
206 (defun diary-astro-day-number () | |
207 "Astronomical (Julian) day number diary entry." | |
17380
ba0844956fde
(diary-astro-day-number): Change format string.
Richard M. Stallman <rms@gnu.org>
parents:
15069
diff
changeset
|
208 (format "Astronomical (Julian) day number at noon UTC: %s.0" |
13053 | 209 (calendar-astro-date-string date))) |
210 | |
211 (provide 'cal-julian) | |
212 | |
213 ;;; cal-julian.el ends here |