Sync to HEAD
[bpt/emacs.git] / lisp / calendar / solar.el
CommitLineData
3afbc435 1;;; solar.el --- calendar functions for solar events
9f34a2a0 2
3f762917 3;; Copyright (C) 1992, 1993, 1995, 1997, 2003 Free Software Foundation, Inc.
9f34a2a0
JB
4
5;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
087c56fa 6;; Denis B. Roegel <Denis.Roegel@loria.fr>
68e60225
ER
7;; Keywords: calendar
8;; Human-Keywords: sunrise, sunset, equinox, solstice, calendar, diary,
9;; holidays
9f34a2a0
JB
10
11;; This file is part of GNU Emacs.
12
59243403
RS
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 2, or (at your option)
16;; any later version.
17
9f34a2a0 18;; GNU Emacs is distributed in the hope that it will be useful,
59243403
RS
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
EN
24;; along with GNU Emacs; see the file COPYING. If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
9f34a2a0
JB
27
28;;; Commentary:
29
6ff099c3
ER
30;; This collection of functions implements the features of calendar.el,
31;; diary.el, and holiday.el that deal with times of day, sunrise/sunset, and
ac138dc0 32;; equinoxes/solstices.
9f34a2a0
JB
33
34;; Based on the ``Almanac for Computers 1984,'' prepared by the Nautical
75af4a4a
ER
35;; Almanac Office, United States Naval Observatory, Washington, 1984, on
36;; ``Astronomical Formulae for Calculators,'' 3rd ed., by Jean Meeus,
087c56fa
ER
37;; Willmann-Bell, Inc., 1985, on ``Astronomical Algorithms'' by Jean Meeus,
38;; Willmann-Bell, Inc., 1991, and on ``Planetary Programs and Tables from
39;; -4000 to +2800'' by Pierre Bretagnon and Jean-Louis Simon, Willmann-Bell,
40;; Inc., 1986.
75af4a4a 41
9f34a2a0 42;;
087c56fa
ER
43;; Accuracy:
44;; 1. Sunrise/sunset times will be accurate to the minute for years
45;; 1951--2050. For other years the times will be within +/- 2 minutes.
9f34a2a0 46;;
087c56fa
ER
47;; 2. Equinox/solstice times will be accurate to the minute for years
48;; 1951--2050. For other years the times will be within +/- 1 minute.
9f34a2a0 49
a96a5fca
PE
50;; Technical details of all the calendrical calculations can be found in
51;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
52;; Cambridge University Press (1997).
53
9f34a2a0
JB
54;; Comments, corrections, and improvements should be sent to
55;; Edward M. Reingold Department of Computer Science
56;; (217) 333-6733 University of Illinois at Urbana-Champaign
57;; reingold@cs.uiuc.edu 1304 West Springfield Avenue
58;; Urbana, Illinois 61801
59
60;;; Code:
61
3f762917
GM
62(defvar displayed-month)
63(defvar displayed-year)
64
9f34a2a0
JB
65(if (fboundp 'atan)
66 (require 'lisp-float-type)
3afbc435 67 (error "Solar/lunar calculations impossible since floating point is unavailable"))
9f34a2a0 68
a92ade89 69(require 'cal-dst)
087c56fa 70(require 'cal-julian)
a92ade89
JB
71
72;;;###autoload
5e11a170 73(defcustom calendar-time-display-form
a92ade89
JB
74 '(12-hours ":" minutes am-pm
75 (if time-zone " (") time-zone (if time-zone ")"))
76 "*The pseudo-pattern that governs the way a time of day is formatted.
77
78A pseudo-pattern is a list of expressions that can involve the keywords
4f7bf990
JB
79`12-hours', `24-hours', and `minutes', all numbers in string form,
80and `am-pm' and `time-zone', both alphabetic strings.
a92ade89
JB
81
82For example, the form
83
84 '(24-hours \":\" minutes
85 (if time-zone \" (\") time-zone (if time-zone \")\"))
86
5e11a170
RS
87would give military-style times like `21:07 (UTC)'."
88 :type 'sexp
89 :group 'calendar)
a92ade89
JB
90
91;;;###autoload
5e11a170 92(defcustom calendar-latitude nil
6ff099c3
ER
93 "*Latitude of `calendar-location-name' in degrees.
94
95The value can be either a decimal fraction (one place of accuracy is
96sufficient), + north, - south, such as 40.7 for New York City, or the value
97can be a vector [degrees minutes north/south] such as [40 50 north] for New
98York City.
99
5e11a170
RS
100This variable should be set in `site-start'.el."
101 :type '(choice (const nil)
102 (number :tag "Exact")
103 (vector :value [0 0 north]
104 (integer :tag "Degrees")
105 (integer :tag "Minutes")
106 (choice :tag "Position"
107 (const north)
108 (const south))))
109 :group 'calendar)
a92ade89
JB
110
111;;;###autoload
5e11a170 112(defcustom calendar-longitude nil
6ff099c3
ER
113 "*Longitude of `calendar-location-name' in degrees.
114
115The value can be either a decimal fraction (one place of accuracy is
116sufficient), + east, - west, such as -73.9 for New York City, or the value
117can be a vector [degrees minutes east/west] such as [73 55 west] for New
118York City.
119
5e11a170
RS
120This variable should be set in `site-start'.el."
121 :type '(choice (const nil)
122 (number :tag "Exact")
123 (vector :value [0 0 west]
124 (integer :tag "Degrees")
125 (integer :tag "Minutes")
126 (choice :tag "Position"
127 (const east)
128 (const west))))
129 :group 'calendar)
6ff099c3
ER
130
131(defsubst calendar-latitude ()
132 "Convert calendar-latitude to a signed decimal fraction, if needed."
133 (if (numberp calendar-latitude)
134 calendar-latitude
135 (let ((lat (+ (aref calendar-latitude 0)
136 (/ (aref calendar-latitude 1) 60.0))))
137 (if (equal (aref calendar-latitude 2) 'north)
138 lat
139 (- lat)))))
140
141(defsubst calendar-longitude ()
142 "Convert calendar-longitude to a signed decimal fraction, if needed."
143 (if (numberp calendar-longitude)
144 calendar-longitude
145 (let ((long (+ (aref calendar-longitude 0)
146 (/ (aref calendar-longitude 1) 60.0))))
147 (if (equal (aref calendar-longitude 2) 'east)
148 long
149 (- long)))))
a92ade89
JB
150
151;;;###autoload
5e11a170 152(defcustom calendar-location-name
a92ade89
JB
153 '(let ((float-output-format "%.1f"))
154 (format "%s%s, %s%s"
6ff099c3
ER
155 (if (numberp calendar-latitude)
156 (abs calendar-latitude)
157 (+ (aref calendar-latitude 0)
158 (/ (aref calendar-latitude 1) 60.0)))
159 (if (numberp calendar-latitude)
160 (if (> calendar-latitude 0) "N" "S")
161 (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
162 (if (numberp calendar-longitude)
163 (abs calendar-longitude)
164 (+ (aref calendar-longitude 0)
165 (/ (aref calendar-longitude 1) 60.0)))
166 (if (numberp calendar-longitude)
167 (if (> calendar-longitude 0) "E" "W")
562a94a0 168 (if (equal (aref calendar-longitude 2) 'east) "E" "W"))))
52f4a7f7 169 "*Expression evaluating to name of `calendar-longitude', `calendar-latitude'.
6ff099c3
ER
170For example, \"New York City\". Default value is just the latitude, longitude
171pair.
172
5e11a170
RS
173This variable should be set in `site-start'.el."
174 :type 'sexp
175 :group 'calendar)
087c56fa 176
5e11a170 177(defcustom solar-error 0.5
087c56fa
ER
178"*Tolerance (in minutes) for sunrise/sunset calculations.
179
180A larger value makes the calculations for sunrise/sunset faster, but less
181accurate. The default is half a minute (30 seconds), so that sunrise/sunset
182times will be correct to the minute.
183
184It is useless to set the value smaller than 4*delta, where delta is the
185accuracy in the longitude of the sun (given by the function
186`solar-ecliptic-coordinates') in degrees since (delta/360) x (86400/60) = 4 x
187delta. At present, delta = 0.01 degrees, so the value of the variable
5e11a170
RS
188`solar-error' should be at least 0.04 minutes (about 2.5 seconds)."
189 :type 'number
190 :group 'calendar)
9f34a2a0 191
3d9dece2 192(defvar solar-n-hemi-seasons
fc68b552
BF
193 '("Vernal Equinox" "Summer Solstice" "Autumnal Equinox" "Winter Solstice")
194 "List of season changes for the northern hemisphere.")
195
3d9dece2 196(defvar solar-s-hemi-seasons
fc68b552
BF
197 '("Autumnal Equinox" "Winter Solstice" "Vernal Equinox" "Summer Solstice")
198 "List of season changes for the southern hemisphere.")
199
a1506d29
JB
200(defvar solar-sidereal-time-greenwich-midnight
201 nil
087c56fa
ER
202 "Sidereal time at Greenwich at midnight (universal time).")
203
651f4408 204(defvar solar-northern-spring-or-summer-season nil
85fd0d84 205 "Non-nil if northern spring or summer and nil otherwise.
087c56fa
ER
206Needed for polar areas, in order to know whether the day lasts 0 or 24 hours.")
207
9f34a2a0
JB
208(defun solar-setup ()
209 "Prompt user for latitude, longitude, and time zone."
210 (beep)
211 (if (not calendar-longitude)
212 (setq calendar-longitude
213 (solar-get-number
214 "Enter longitude (decimal fraction; + east, - west): ")))
215 (if (not calendar-latitude)
216 (setq calendar-latitude
217 (solar-get-number
218 "Enter latitude (decimal fraction; + north, - south): ")))
219 (if (not calendar-time-zone)
220 (setq calendar-time-zone
221 (solar-get-number
e2fe2f52 222 "Enter difference from Coordinated Universal Time (in minutes): "))))
9f34a2a0
JB
223
224(defun solar-get-number (prompt)
225 "Return a number from the minibuffer, prompting with PROMPT.
226Returns nil if nothing was entered."
227 (let ((x (read-string prompt "")))
228 (if (not (string-equal x ""))
229 (string-to-int x))))
230
087c56fa
ER
231;; The condition-case stuff is needed to catch bogus arithmetic
232;; exceptions that occur on some machines (like Sparcs)
233(defun solar-sin-degrees (x)
234 (condition-case nil
235 (sin (degrees-to-radians (mod x 360.0)))
236 (solar-sin-degrees x)))
237(defun solar-cosine-degrees (x)
238 (condition-case nil
239 (cos (degrees-to-radians (mod x 360.0)))
240 (solar-cosine-degrees x)))
241(defun solar-tangent-degrees (x)
242 (condition-case nil
243 (tan (degrees-to-radians (mod x 360.0)))
244 (solar-tangent-degrees x)))
a1506d29 245
9f34a2a0
JB
246(defun solar-xy-to-quadrant (x y)
247 "Determines the quadrant of the point X, Y."
248 (if (> x 0)
249 (if (> y 0) 1 4)
250 (if (> y 0) 2 3)))
251
252(defun solar-degrees-to-quadrant (angle)
253 "Determines the quadrant of ANGLE."
3a2e3ab5 254 (1+ (floor (mod angle 360) 90)))
9f34a2a0
JB
255
256(defun solar-arctan (x quad)
257 "Arctangent of X in quadrant QUAD."
258 (let ((deg (radians-to-degrees (atan x))))
259 (cond ((equal quad 2) (+ deg 180))
260 ((equal quad 3) (+ deg 180))
261 ((equal quad 4) (+ deg 360))
262 (t deg))))
263
087c56fa
ER
264(defun solar-atn2 (x y)
265 "Arctan of point X, Y."
b92f6988
ER
266 (if (= x 0)
267 (if (> y 0) 90 270)
bd0f7fd3 268 (solar-arctan (/ y x) (solar-xy-to-quadrant x y))))
087c56fa 269
9f34a2a0 270(defun solar-arccos (x)
b92f6988
ER
271 "Arcos of X."
272 (let ((y (sqrt (- 1 (* x x)))))
273 (solar-atn2 x y)))
9f34a2a0
JB
274
275(defun solar-arcsin (y)
b92f6988
ER
276 "Arcsin of Y."
277 (let ((x (sqrt (- 1 (* y y)))))
278 (solar-atn2 x y)
279 ))
9f34a2a0 280
087c56fa
ER
281(defsubst solar-degrees-to-hours (degrees)
282 "Convert DEGREES to hours."
283 (/ degrees 15.0))
9f34a2a0 284
6ff099c3 285(defsubst solar-hours-to-days (hour)
087c56fa 286 "Convert HOUR to decimal fraction of a day."
6ff099c3 287 (/ hour 24.0))
9f34a2a0 288
087c56fa
ER
289(defun solar-right-ascension (longitude obliquity)
290 "Right ascension of the sun, in hours, given LONGITUDE and OBLIQUITY.
291Both arguments are in degrees."
9f34a2a0
JB
292 (solar-degrees-to-hours
293 (solar-arctan
087c56fa 294 (* (solar-cosine-degrees obliquity) (solar-tangent-degrees longitude))
9f34a2a0
JB
295 (solar-degrees-to-quadrant longitude))))
296
087c56fa
ER
297(defun solar-declination (longitude obliquity)
298 "Declination of the sun, in degrees, given LONGITUDE and OBLIQUITY.
299Both arguments are in degrees."
9f34a2a0 300 (solar-arcsin
087c56fa 301 (* (solar-sin-degrees obliquity)
9f34a2a0
JB
302 (solar-sin-degrees longitude))))
303
651f4408
RS
304(defun solar-sunrise-and-sunset (time latitude longitude height)
305 "Sunrise, sunset and length of day.
087c56fa
ER
306Parameters are the midday TIME and the LATITUDE, LONGITUDE of the location.
307
308TIME is a pair with the first component being the number of Julian centuries
309elapsed at 0 Universal Time, and the second component being the universal
310time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
556b41d2 311\(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
087c56fa
ER
312Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
313
651f4408
RS
314HEIGHT is the angle the center of the sun has over the horizon for the contact
315we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
316accounting for the edge of the sun being on the horizon.
317
318Coordinates are included because this function is called with latitude=1
087c56fa 319degrees to find out if polar regions have 24 hours of sun or only night."
651f4408
RS
320 (let* ((rise-time (solar-moment -1 latitude longitude time height))
321 (set-time (solar-moment 1 latitude longitude time height))
087c56fa
ER
322 (day-length))
323 (if (not (and rise-time set-time))
651f4408
RS
324 (if (or (and (> latitude 0)
325 solar-northern-spring-or-summer-season)
326 (and (< latitude 0)
327 (not solar-northern-spring-or-summer-season)))
328 (setq day-length 24)
329 (setq day-length 0))
330 (setq day-length (- set-time rise-time)))
b92f6988
ER
331 (list (if rise-time (+ rise-time (/ calendar-time-zone 60.0)) nil)
332 (if set-time (+ set-time (/ calendar-time-zone 60.0)) nil)
333 day-length)))
087c56fa 334
651f4408 335(defun solar-moment (direction latitude longitude time height)
087c56fa
ER
336 "Sunrise/sunset at location.
337Sunrise if DIRECTION =-1 or sunset if =1 at LATITUDE, LONGITUDE, with midday
338being TIME.
339
340TIME is a pair with the first component being the number of Julian centuries
341elapsed at 0 Universal Time, and the second component being the universal
342time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
371ea94f 343\(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
087c56fa
ER
344Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
345
651f4408
RS
346HEIGHT is the angle the center of the sun has over the horizon for the contact
347we are trying to find. For sunrise and sunset, it is usually -0.61 degrees,
348accounting for the edge of the sun being on the horizon.
349
087c56fa
ER
350Uses binary search."
351 (let* ((ut (car (cdr time)))
651f4408
RS
352 (possible t) ; we assume that rise or set are possible
353 (utmin (+ ut (* direction 12.0)))
087c56fa
ER
354 (utmax ut) ; the time searched is between utmin and utmax
355 ; utmin and utmax are in hours
356 (utmoment-old 0.0) ; rise or set approximation
357 (utmoment 1.0) ; rise or set approximation
358 (hut 0) ; sun height at utmoment
359 (t0 (car time))
651f4408
RS
360 (hmin (car (cdr
361 (solar-horizontal-coordinates (list t0 utmin)
087c56fa 362 latitude longitude t))))
651f4408
RS
363 (hmax (car (cdr
364 (solar-horizontal-coordinates (list t0 utmax)
087c56fa
ER
365 latitude longitude t)))))
366 ; -0.61 degrees is the height of the middle of the sun, when it rises
367 ; or sets.
651f4408
RS
368 (if (< hmin height)
369 (if (> hmax height)
087c56fa 370 (while ;(< i 20) ; we perform a simple dichotomy
651f4408 371 ; (> (abs (- hut height)) epsilon)
087c56fa
ER
372 (>= (abs (- utmoment utmoment-old))
373 (/ solar-error 60))
374 (setq utmoment-old utmoment)
375 (setq utmoment (/ (+ utmin utmax) 2))
651f4408
RS
376 (setq hut (car (cdr
377 (solar-horizontal-coordinates
087c56fa 378 (list t0 utmoment) latitude longitude t))))
651f4408
RS
379 (if (< hut height) (setq utmin utmoment))
380 (if (> hut height) (setq utmax utmoment))
087c56fa 381 )
651f4408
RS
382 (setq possible nil)) ; the sun never rises
383 (setq possible nil)) ; the sun never sets
384 (if (not possible) nil utmoment)))
9f34a2a0 385
c68488d2 386(defun solar-time-string (time time-zone)
75af4a4a 387 "Printable form for decimal fraction TIME in TIME-ZONE.
c68488d2
ER
388Format used is given by `calendar-time-display-form'."
389 (let* ((time (round (* 60 time)))
a92ade89
JB
390 (24-hours (/ time 60))
391 (minutes (format "%02d" (% time 60)))
392 (12-hours (format "%d" (1+ (% (+ 24-hours 11) 12))))
9f34a2a0 393 (am-pm (if (>= 24-hours 12) "pm" "am"))
9f34a2a0
JB
394 (24-hours (format "%02d" 24-hours)))
395 (mapconcat 'eval calendar-time-display-form "")))
396
087c56fa
ER
397
398(defun solar-daylight (time)
399 "Printable form for time expressed in hours."
400 (format "%d:%02d"
401 (floor time)
402 (floor (* 60 (- time (floor time))))))
403
404(defun solar-exact-local-noon (date)
a1506d29 405 "Date and Universal Time of local noon at *local date* date.
087c56fa
ER
406
407The date may be different from the one asked for, but it will be the right
408local date. The second component of date should be an integer."
409 (let* ((nd date)
556b41d2 410 (ut (- 12.0 (/ (calendar-longitude) 15)))
087c56fa
ER
411 (te (solar-time-equation date ut)))
412 (setq ut (- ut te))
413 (if (>= ut 24)
a1506d29 414 (progn
087c56fa
ER
415 (setq nd (list (car date) (+ 1 (car (cdr date)))
416 (car (cdr (cdr date)))))
417 (setq ut (- ut 24))))
418 (if (< ut 0)
a1506d29 419 (progn
087c56fa
ER
420 (setq nd (list (car date) (- (car (cdr date)) 1)
421 (car (cdr (cdr date)))))
422 (setq ut (+ ut 24))))
423 (setq nd (calendar-gregorian-from-absolute
424 (calendar-absolute-from-gregorian nd)))
425 ; date standardization
426 (list nd ut)))
427
9f34a2a0 428(defun solar-sunrise-sunset (date)
087c56fa
ER
429 "List of *local* times of sunrise, sunset, and daylight on Gregorian DATE.
430
431Corresponding value is nil if there is no sunrise/sunset."
432 (let* (; first, get the exact moment of local noon.
433 (exact-local-noon (solar-exact-local-noon date))
110c171f 434 ; get the time from the 2000 epoch.
087c56fa
ER
435 (t0 (solar-julian-ut-centuries (car exact-local-noon)))
436 ; store the sidereal time at Greenwich at midnight of UT time.
437 ; find if summer or winter slightly above the equator
438 (equator-rise-set
651f4408 439 (progn (setq solar-sidereal-time-greenwich-midnight
087c56fa 440 (solar-sidereal-time t0))
651f4408 441 (solar-sunrise-and-sunset
087c56fa 442 (list t0 (car (cdr exact-local-noon)))
651f4408
RS
443 1.0
444 (calendar-longitude) 0)))
087c56fa
ER
445 ; store the spring/summer information,
446 ; compute sunrise and sunset (two first components of rise-set).
447 ; length of day is the third component (it is only the difference
448 ; between sunset and sunrise when there is a sunset and a sunrise)
449 (rise-set
450 (progn
651f4408
RS
451 (setq solar-northern-spring-or-summer-season
452 (if (> (car (cdr (cdr equator-rise-set))) 12) t nil))
453 (solar-sunrise-and-sunset
087c56fa 454 (list t0 (car (cdr exact-local-noon)))
556b41d2 455 (calendar-latitude)
651f4408 456 (calendar-longitude) -0.61)))
087c56fa
ER
457 (rise (car rise-set))
458 (adj-rise (if rise (dst-adjust-time date rise) nil))
459 (set (car (cdr rise-set)))
460 (adj-set (if set (dst-adjust-time date set) nil))
461 (length (car (cdr (cdr rise-set)))) )
462 (list
463 (and rise (calendar-date-equal date (car adj-rise)) (cdr adj-rise))
464 (and set (calendar-date-equal date (car adj-set)) (cdr adj-set))
465 (solar-daylight length))))
466
467(defun solar-sunrise-sunset-string (date)
468 "String of *local* times of sunrise, sunset, and daylight on Gregorian DATE."
469 (let ((l (solar-sunrise-sunset date)))
470 (format
471 "%s, %s at %s (%s hours daylight)"
472 (if (car l)
473 (concat "Sunrise " (apply 'solar-time-string (car l)))
474 "No sunrise")
475 (if (car (cdr l))
476 (concat "sunset " (apply 'solar-time-string (car (cdr l))))
477 "no sunset")
478 (eval calendar-location-name)
479 (car (cdr (cdr l))))))
480
481(defun solar-julian-ut-centuries (date)
482 "Number of Julian centuries elapsed since 1 Jan, 2000 at noon U.T. for Gregorian DATE."
a1506d29 483 (/ (- (calendar-absolute-from-gregorian date)
087c56fa
ER
484 (calendar-absolute-from-gregorian '(1 1.5 2000)))
485 36525.0))
a1506d29 486
087c56fa
ER
487(defun solar-ephemeris-time(time)
488 "Ephemeris Time at moment TIME.
489
490TIME is a pair with the first component being the number of Julian centuries
491elapsed at 0 Universal Time, and the second component being the universal
492time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
371ea94f 493\(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
087c56fa
ER
494Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
495
496Result is in julian centuries of ephemeris time."
497 (let* ((t0 (car time))
498 (ut (car (cdr time)))
499 (t1 (+ t0 (/ (/ ut 24.0) 36525)))
500 (y (+ 2000 (* 100 t1)))
501 (dt (* 86400 (solar-ephemeris-correction (floor y)))))
502 (+ t1 (/ (/ dt 86400) 36525))))
9f34a2a0 503
75af4a4a
ER
504(defun solar-date-next-longitude (d l)
505 "First moment on or after Julian day number D when sun's longitude is a
506multiple of L degrees at calendar-location-name with that location's
507local time (including any daylight savings rules).
508
509L must be an integer divisor of 360.
510
511Result is in local time expressed astronomical (Julian) day numbers.
512
513The values of calendar-daylight-savings-starts,
514calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
515calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
516calendar-time-zone are used to interpret local time."
517 (let* ((long)
518 (start d)
519 (start-long (solar-longitude d))
520 (next (mod (* l (1+ (floor (/ start-long l)))) 360))
521 (end (+ d (* (/ l 360.0) 400)))
522 (end-long (solar-longitude end)))
523 (while ;; bisection search for nearest minute
524 (< 0.00001 (- end start))
525 ;; start <= d < end
526 ;; start-long <= next < end-long when next != 0
527 ;; when next = 0, we look for the discontinuity (start-long is near 360
528 ;; and end-long is small (less than l).
529 (setq d (/ (+ start end) 2.0))
530 (setq long (solar-longitude d))
531 (if (or (and (/= next 0) (< long next))
532 (and (= next 0) (< l long)))
533 (progn
534 (setq start d)
535 (setq start-long long))
536 (setq end d)
537 (setq end-long long)))
538 (/ (+ start end) 2.0)))
539
a1506d29 540(defun solar-horizontal-coordinates
087c56fa
ER
541 (time latitude longitude for-sunrise-sunset)
542 "Azimuth and height of the sun at TIME, LATITUDE, and LONGITUDE.
543
544TIME is a pair with the first component being the number of Julian centuries
545elapsed at 0 Universal Time, and the second component being the universal
546time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
371ea94f 547\(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
087c56fa
ER
548Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT.
549
550The azimuth is given in degrees as well as the height (between -180 and 180)."
551 (let* ((ut (car (cdr time)))
552 (ec (solar-equatorial-coordinates time for-sunrise-sunset))
553 (st (+ solar-sidereal-time-greenwich-midnight
554 (* ut 1.00273790935)))
556b41d2 555 (ah (- (* st 15) (* 15 (car ec)) (* -1 (calendar-longitude))))
087c56fa
ER
556 ; hour angle (in degrees)
557 (de (car (cdr ec)))
b92f6988 558 (azimuth (solar-atn2 (- (* (solar-cosine-degrees ah)
087c56fa
ER
559 (solar-sin-degrees latitude))
560 (* (solar-tangent-degrees de)
b92f6988
ER
561 (solar-cosine-degrees latitude)))
562 (solar-sin-degrees ah)))
a1506d29 563 (height (solar-arcsin
087c56fa
ER
564 (+ (* (solar-sin-degrees latitude) (solar-sin-degrees de))
565 (* (solar-cosine-degrees latitude)
566 (solar-cosine-degrees de)
567 (solar-cosine-degrees ah))))))
568 (if (> height 180) (setq height (- height 360)))
569 (list azimuth height)))
570
571(defun solar-equatorial-coordinates (time for-sunrise-sunset)
572 "Right ascension (in hours) and declination (in degrees) of the sun at TIME.
573
574TIME is a pair with the first component being the number of Julian centuries
575elapsed at 0 Universal Time, and the second component being the universal
576time. For instance, the pair corresponding to November 28, 1995 at 16 UT is
371ea94f 577\(-0.040945 16), -0.040945 being the number of julian centuries elapsed between
087c56fa 578Jan 1, 2000 at 12 UT and November 28, 1995 at 0 UT."
a1506d29 579 (let* ((tm (solar-ephemeris-time time))
087c56fa
ER
580 (ec (solar-ecliptic-coordinates tm for-sunrise-sunset)))
581 (list (solar-right-ascension (car ec) (car (cdr ec)))
582 (solar-declination (car ec) (car (cdr ec))))))
583
584(defun solar-ecliptic-coordinates (time for-sunrise-sunset)
585 "Apparent longitude of the sun, ecliptic inclination, (both in degrees)
586equation of time (in hours) and nutation in longitude (in seconds)
587at moment `time', expressed in julian centuries of Ephemeris Time
588since January 1st, 2000, at 12 ET."
589 (let* ((l (+ 280.46645
590 (* 36000.76983 time)
a1506d29 591 (* 0.0003032 time time))) ; sun mean longitude
087c56fa 592 (ml (+ 218.3165
a1506d29 593 (* 481267.8813 time))) ; moon mean longitude
087c56fa
ER
594 (m (+ 357.52910
595 (* 35999.05030 time)
596 (* -0.0001559 time time)
a1506d29 597 (* -0.00000048 time time time))) ; sun mean anomaly
087c56fa
ER
598 (i (+ 23.43929111 (* -0.013004167 time)
599 (* -0.00000016389 time time)
a1506d29 600 (* 0.0000005036 time time time))); mean inclination
087c56fa
ER
601 (c (+ (* (+ 1.914600
602 (* -0.004817 time)
603 (* -0.000014 time time))
604 (solar-sin-degrees m))
605 (* (+ 0.019993 (* -0.000101 time))
606 (solar-sin-degrees (* 2 m)))
607 (* 0.000290
a1506d29
JB
608 (solar-sin-degrees (* 3 m))))) ; center equation
609 (L (+ l c)) ; total longitude
087c56fa
ER
610 (omega (+ 125.04
611 (* -1934.136 time))) ; longitude of moon's ascending node
612 ; on the ecliptic
613 (nut (if (not for-sunrise-sunset)
614 (+ (* -17.20 (solar-sin-degrees omega))
615 (* -1.32 (solar-sin-degrees (* 2 l)))
616 (* -0.23 (solar-sin-degrees (* 2 ml)))
617 (* 0.21 (solar-sin-degrees (* 2 omega))))
618 nil))
619 ; nut = nutation in longitude, measured in seconds of angle.
620 (ecc (if (not for-sunrise-sunset)
621 (+ 0.016708617
622 (* -0.000042037 time)
623 (* -0.0000001236 time time)) ; eccentricity of earth's orbit
624 nil))
625 (app (+ L
626 -0.00569
627 (* -0.00478
628 (solar-sin-degrees omega)))) ; apparent longitude of sun
629 (y (if (not for-sunrise-sunset)
a1506d29 630 (* (solar-tangent-degrees (/ i 2))
087c56fa
ER
631 (solar-tangent-degrees (/ i 2)))
632 nil))
633 (time-eq (if (not for-sunrise-sunset)
634 (/ (* 12 (+ (* y (solar-sin-degrees (* 2 l)))
635 (* -2 ecc (solar-sin-degrees m))
a1506d29 636 (* 4 ecc y (solar-sin-degrees m)
087c56fa
ER
637 (solar-cosine-degrees (* 2 l)))
638 (* -0.5 y y (solar-sin-degrees (* 4 l)))
639 (* -1.25 ecc ecc (solar-sin-degrees (* 2 m)))))
640 3.1415926535)
641 nil)))
642 ; equation of time, in hours
643 (list app i time-eq nut)))
644
75af4a4a
ER
645(defun solar-longitude (d)
646 "Longitude of sun on astronomical (Julian) day number D.
087c56fa 647Accurary is about 0.0006 degree (about 365.25*24*60*0.0006/360 = 1 minutes).
75af4a4a
ER
648
649The values of calendar-daylight-savings-starts,
650calendar-daylight-savings-starts-time, calendar-daylight-savings-ends,
651calendar-daylight-savings-ends-time, calendar-daylight-time-offset, and
652calendar-time-zone are used to interpret local time."
653 (let* ((a-d (calendar-absolute-from-astro d))
75af4a4a 654 ;; get Universal Time
087c56fa
ER
655 (date (calendar-astro-from-absolute
656 (- a-d
657 (if (dst-in-effect a-d)
658 (/ calendar-daylight-time-offset 24.0 60.0) 0)
659 (/ calendar-time-zone 60.0 24.0))))
75af4a4a
ER
660 ;; get Ephemeris Time
661 (date (+ date (solar-ephemeris-correction
662 (extract-calendar-year
663 (calendar-gregorian-from-absolute
664 (floor
665 (calendar-absolute-from-astro
666 date)))))))
087c56fa
ER
667 (U (/ (- date 2451545) 3652500))
668 (longitude
669 (+ 4.9353929
670 (* 62833.1961680 U)
671 (* 0.0000001
672 (apply '+
673 (mapcar '(lambda (x)
674 (* (car x)
675 (sin (mod
676 (+ (car (cdr x))
677 (* (car (cdr (cdr x))) U))
678 (* 2 pi)))))
679 solar-data-list)))))
680 (aberration
681 (* 0.0000001 (- (* 17 (cos (+ 3.10 (* 62830.14 U)))) 973)))
682 (A1 (mod (+ 2.18 (* U (+ -3375.70 (* 0.36 U)))) (* 2 pi)))
683 (A2 (mod (+ 3.51 (* U (+ 125666.39 (* 0.10 U)))) (* 2 pi)))
684 (nutation (* -0.0000001 (+ (* 834 (sin A1)) (* 64 (sin A2))))))
685 (mod (radians-to-degrees (+ longitude aberration nutation)) 360.0)))
686
687(defconst solar-data-list
688 '((403406 4.721964 1.621043)
689 (195207 5.937458 62830.348067)
690 (119433 1.115589 62830.821524)
691 (112392 5.781616 62829.634302)
692 (3891 5.5474 125660.5691)
693 (2819 1.5120 125660.984)
694 (1721 4.1897 62832.4766)
695 (0 1.163 0.813)
696 (660 5.415 125659.31)
697 (350 4.315 57533.85)
698 (334 4.553 -33.931)
699 (314 5.198 777137.715)
700 (268 5.989 78604.191)
701 (242 2.911 5.412)
702 (234 1.423 39302.098)
703 (158 0.061 -34.861)
704 (132 2.317 115067.698)
705 (129 3.193 15774.337)
706 (114 2.828 5296.670)
707 (99 0.52 58849.27)
708 (93 4.65 5296.11)
709 (86 4.35 -3980.70)
710 (78 2.75 52237.69)
711 (72 4.50 55076.47)
712 (68 3.23 261.08)
713 (64 1.22 15773.85)
714 (46 0.14 188491.03)
715 (38 3.44 -7756.55)
716 (37 4.37 264.89)
717 (32 1.14 117906.27)
718 (29 2.84 55075.75)
719 (28 5.96 -7961.39)
720 (27 5.09 188489.81)
721 (27 1.72 2132.19)
722 (25 2.56 109771.03)
723 (24 1.92 54868.56)
724 (21 0.09 25443.93)
725 (21 5.98 -55731.43)
726 (20 4.03 60697.74)
727 (18 4.47 2132.79)
728 (17 0.79 109771.63)
729 (14 4.24 -7752.82)
730 (13 2.01 188491.91)
731 (13 2.65 207.81)
732 (13 4.98 29424.63)
733 (12 0.93 -7.99)
734 (10 2.21 46941.14)
735 (10 3.59 -68.29)
736 (10 1.50 21463.25)
737 (10 2.55 157208.40)))
9f34a2a0
JB
738
739(defun solar-ephemeris-correction (year)
087c56fa
ER
740 "Ephemeris time minus Universal Time during Gregorian year.
741Result is in days.
742
743For the years 1800-1987, the maximum error is 1.9 seconds.
75af4a4a
ER
744For the other years, the maximum error is about 30 seconds."
745 (cond ((and (<= 1988 year) (< year 2020))
746 (/ (+ year -2000 67.0) 60.0 60.0 24.0))
747 ((and (<= 1900 year) (< year 1988))
748 (let* ((theta (/ (- (calendar-astro-from-absolute
749 (calendar-absolute-from-gregorian
750 (list 7 1 year)))
751 (calendar-astro-from-absolute
752 (calendar-absolute-from-gregorian
753 '(1 1 1900))))
754 36525.0))
755 (theta2 (* theta theta))
756 (theta3 (* theta2 theta))
757 (theta4 (* theta2 theta2))
758 (theta5 (* theta3 theta2)))
759 (+ -0.00002
760 (* 0.000297 theta)
761 (* 0.025184 theta2)
762 (* -0.181133 theta3)
763 (* 0.553040 theta4)
764 (* -0.861938 theta5)
765 (* 0.677066 theta3 theta3)
766 (* -0.212591 theta4 theta3))))
767 ((and (<= 1800 year) (< year 1900))
768 (let* ((theta (/ (- (calendar-astro-from-absolute
769 (calendar-absolute-from-gregorian
770 (list 7 1 year)))
771 (calendar-astro-from-absolute
772 (calendar-absolute-from-gregorian
773 '(1 1 1900))))
774 36525.0))
775 (theta2 (* theta theta))
776 (theta3 (* theta2 theta))
777 (theta4 (* theta2 theta2))
778 (theta5 (* theta3 theta2)))
779 (+ -0.000009
780 (* 0.003844 theta)
781 (* 0.083563 theta2)
782 (* 0.865736 theta3)
783 (* 4.867575 theta4)
784 (* 15.845535 theta5)
785 (* 31.332267 theta3 theta3)
786 (* 38.291999 theta4 theta3)
787 (* 28.316289 theta4 theta4)
788 (* 11.636204 theta4 theta5)
789 (* 2.043794 theta5 theta5))))
790 ((and (<= 1620 year) (< year 1800))
791 (let ((x (/ (- year 1600) 10.0)))
792 (/ (+ (* 2.19167 x x) (* -40.675 x) 196.58333) 60.0 60.0 24.0)))
793 (t (let* ((tmp (- (calendar-astro-from-absolute
794 (calendar-absolute-from-gregorian
795 (list 1 1 year)))
796 2382148))
797 (second (- (/ (* tmp tmp) 41048480.0) 15)))
798 (/ second 60.0 60.0 24.0)))))
9f34a2a0 799
087c56fa
ER
800(defun solar-sidereal-time (t0)
801 "Sidereal time (in hours) in Greenwich.
802
803At T0=Julian centuries of universal time.
804T0 must correspond to 0 hours UT."
805 (let* ((mean-sid-time (+ 6.6973746
806 (* 2400.051337 t0)
807 (* 0.0000258622 t0 t0)
808 (* -0.0000000017222 t0 t0 t0)))
809 (et (solar-ephemeris-time (list t0 0.0)))
810 (nut-i (solar-ecliptic-coordinates et nil))
811 (nut (car (cdr (cdr (cdr nut-i))))) ; nutation
812 (i (car (cdr nut-i)))) ; inclination
a1506d29 813 (mod (+ (mod (+ mean-sid-time
087c56fa
ER
814 (/ (/ (* nut (solar-cosine-degrees i)) 15) 3600)) 24.0)
815 24.0)
816 24.0)))
817
818(defun solar-time-equation (date ut)
819 "Equation of time expressed in hours at Gregorian DATE at Universal time UT."
820 (let* ((et (solar-date-to-et date ut))
821 (ec (solar-ecliptic-coordinates et nil)))
822 (car (cdr (cdr ec)))))
823
824(defun solar-date-to-et (date ut)
825 "Ephemeris Time at Gregorian DATE at Universal Time UT (in hours).
826Expressed in julian centuries of Ephemeris Time."
827 (let ((t0 (solar-julian-ut-centuries date)))
828 (solar-ephemeris-time (list t0 ut))))
829
9f34a2a0
JB
830;;;###autoload
831(defun sunrise-sunset (&optional arg)
087c56fa 832 "Local time of sunrise and sunset for today. Accurate to a few seconds.
ec4dfb6b 833If called with an optional prefix argument, prompt for date.
9f34a2a0 834
ec4dfb6b
RS
835If called with an optional double prefix argument, prompt for longitude,
836latitude, time zone, and date, and always use standard time.
9f34a2a0
JB
837
838This function is suitable for execution in a .emacs file."
839 (interactive "p")
6a6e6405 840 (or arg (setq arg 1))
fbfed6f0
JB
841 (if (and (< arg 16)
842 (not (and calendar-latitude calendar-longitude calendar-time-zone)))
843 (solar-setup))
9f34a2a0 844 (let* ((calendar-longitude
fbfed6f0 845 (if (< arg 16) calendar-longitude
9f34a2a0
JB
846 (solar-get-number
847 "Enter longitude (decimal fraction; + east, - west): ")))
848 (calendar-latitude
fbfed6f0 849 (if (< arg 16) calendar-latitude
9f34a2a0
JB
850 (solar-get-number
851 "Enter latitude (decimal fraction; + north, - south): ")))
852 (calendar-time-zone
fbfed6f0 853 (if (< arg 16) calendar-time-zone
9f34a2a0 854 (solar-get-number
e2fe2f52 855 "Enter difference from Coordinated Universal Time (in minutes): ")))
9f34a2a0 856 (calendar-location-name
fbfed6f0
JB
857 (if (< arg 16) calendar-location-name
858 (let ((float-output-format "%.1f"))
859 (format "%s%s, %s%s"
6ff099c3
ER
860 (if (numberp calendar-latitude)
861 (abs calendar-latitude)
862 (+ (aref calendar-latitude 0)
863 (/ (aref calendar-latitude 1) 60.0)))
864 (if (numberp calendar-latitude)
865 (if (> calendar-latitude 0) "N" "S")
866 (if (equal (aref calendar-latitude 2) 'north) "N" "S"))
867 (if (numberp calendar-longitude)
868 (abs calendar-longitude)
869 (+ (aref calendar-longitude 0)
870 (/ (aref calendar-longitude 1) 60.0)))
871 (if (numberp calendar-longitude)
872 (if (> calendar-longitude 0) "E" "W")
562a94a0 873 (if (equal (aref calendar-longitude 2) 'east)
6ff099c3 874 "E" "W"))))))
9f34a2a0 875 (calendar-standard-time-zone-name
fbfed6f0 876 (if (< arg 16) calendar-standard-time-zone-name
a92ade89 877 (cond ((= calendar-time-zone 0) "UTC")
fbfed6f0 878 ((< calendar-time-zone 0)
a92ade89
JB
879 (format "UTC%dmin" calendar-time-zone))
880 (t (format "UTC+%dmin" calendar-time-zone)))))
ec4dfb6b
RS
881 (calendar-daylight-savings-starts
882 (if (< arg 16) calendar-daylight-savings-starts))
883 (calendar-daylight-savings-ends
884 (if (< arg 16) calendar-daylight-savings-ends))
fbfed6f0 885 (date (if (< arg 4) (calendar-current-date) (calendar-read-date)))
9f34a2a0 886 (date-string (calendar-date-string date t))
087c56fa 887 (time-string (solar-sunrise-sunset-string date))
9f34a2a0
JB
888 (msg (format "%s: %s" date-string time-string))
889 (one-window (one-window-p t)))
80897760 890 (if (<= (length msg) (frame-width))
498020d2 891 (message "%s" msg)
9f34a2a0
JB
892 (with-output-to-temp-buffer "*temp*"
893 (princ (concat date-string "\n" time-string)))
498020d2
KH
894 (message "%s"
895 (substitute-command-keys
9f34a2a0
JB
896 (if one-window
897 (if pop-up-windows
898 "Type \\[delete-other-windows] to remove temp window."
899 "Type \\[switch-to-buffer] RET to remove temp window.")
900 "Type \\[switch-to-buffer-other-window] RET to restore old contents of temp window."))))))
a1506d29 901
9f34a2a0
JB
902(defun calendar-sunrise-sunset ()
903 "Local time of sunrise and sunset for date under cursor.
087c56fa 904Accurate to a few seconds."
9f34a2a0
JB
905 (interactive)
906 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
907 (solar-setup))
6a6e6405 908 (let ((date (calendar-cursor-to-date t)))
abd93e66
RS
909 (message "%s: %s"
910 (calendar-date-string date t t)
087c56fa 911 (solar-sunrise-sunset-string date))))
9f34a2a0
JB
912
913(defun diary-sunrise-sunset ()
914 "Local time of sunrise and sunset as a diary entry.
087c56fa 915Accurate to a few seconds."
9f34a2a0
JB
916 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
917 (solar-setup))
087c56fa 918 (solar-sunrise-sunset-string date))
9f34a2a0 919
651f4408
RS
920(defcustom diary-sabbath-candles-minutes 18
921 "*Number of minutes before sunset for sabbath candle lighting."
922 :group 'diary
923 :type 'integer
924 :version "21.1")
925
16cd607d 926(defun diary-sabbath-candles (&optional mark)
9f34a2a0 927 "Local time of candle lighting diary entry--applies if date is a Friday.
16cd607d
RS
928No diary entry if there is no sunset on that date.
929
a1506d29 930An optional parameter MARK specifies a face or single-character string to
16cd607d 931use when highlighting the day in the calendar."
9f34a2a0
JB
932 (if (not (and calendar-latitude calendar-longitude calendar-time-zone))
933 (solar-setup))
934 (if (= (% (calendar-absolute-from-gregorian date) 7) 5);; Friday
087c56fa 935 (let* ((sunset (car (cdr (solar-sunrise-sunset date))))
651f4408
RS
936 (light (if sunset
937 (cons (- (car sunset)
938 (/ diary-sabbath-candles-minutes 60.0))
939 (cdr sunset)))))
2396dbcb 940 (if sunset
16cd607d
RS
941 (cons mark
942 (format "%s Sabbath candle lighting"
943 (apply 'solar-time-string light)))))))
9f34a2a0 944
3f762917
GM
945; from Meeus, 1991, page 167
946(defconst solar-seasons-data
947 '((485 324.96 1934.136)
948 (203 337.23 32964.467)
949 (199 342.08 20.186)
950 (182 27.85 445267.112)
951 (156 73.14 45036.886)
952 (136 171.52 22518.443)
953 (77 222.54 65928.934)
954 (74 296.72 3034.906)
955 (70 243.58 9037.513)
956 (58 119.81 33718.147)
957 (52 297.17 150.678)
958 (50 21.02 2281.226)
959 (45 247.54 29929.562)
960 (44 325.15 31555.956)
961 (29 60.93 4443.417)
962 (18 155.12 67555.328)
963 (17 288.79 4562.452)
964 (16 198.04 62894.029)
965 (14 199.76 31436.921)
966 (12 95.39 14577.848)
967 (12 287.11 31931.756)
968 (12 320.81 34777.259)
969 (9 227.73 1222.114)
970 (8 15.45 16859.074)))
971
087c56fa
ER
972(defun solar-equinoxes/solstices (k year)
973 "Date of equinox/solstice K for YEAR.
974K=0, spring equinox; K=1, summer solstice; K=2, fall equinox;
a1506d29 975K=3, winter solstice.
087c56fa
ER
976RESULT is a gregorian local date.
977
978Accurate to less than a minute between 1951 and 2050."
979 (let* ((JDE0 (solar-mean-equinoxes/solstices k year))
980 (T (/ (- JDE0 2451545.0) 36525))
981 (W (- (* 35999.373 T) 2.47))
982 (Delta-lambda (+ 1 (* 0.0334 (solar-cosine-degrees W))
983 (* 0.0007 (solar-cosine-degrees (* 2 W)))))
a1506d29
JB
984 (S (apply '+ (mapcar '(lambda(x)
985 (* (car x) (solar-cosine-degrees
087c56fa 986 (+ (* (car (cdr (cdr x))) T)
a1506d29 987 (car (cdr x))))))
087c56fa
ER
988 solar-seasons-data)))
989 (JDE (+ JDE0 (/ (* 0.00001 S) Delta-lambda)))
a1506d29 990 (correction (+ 102.3 (* 123.5 T) (* 32.5 T T)))
087c56fa
ER
991 ; ephemeris time correction
992 (JD (- JDE (/ correction 86400)))
993 (date (calendar-gregorian-from-absolute (floor (- JD 1721424.5))))
994 (time (- (- JD 0.5) (floor (- JD 0.5))))
995 )
996 (list (car date) (+ (car (cdr date)) time
997 (/ (/ calendar-time-zone 60.0) 24.0))
998 (car (cdr (cdr date))))))
999
1000; from Meeus, 1991, page 166
1001(defun solar-mean-equinoxes/solstices (k year)
a1506d29 1002 "Julian day of mean equinox/solstice K for YEAR.
087c56fa
ER
1003K=0, spring equinox; K=1, summer solstice; K=2, fall equinox; K=3, winter
1004solstice. These formulas are only to be used between 1000 BC and 3000 AD."
1005 (let ((y (/ year 1000.0))
1006 (z (/ (- year 2000) 1000.0)))
1007 (if (< year 1000) ; actually between -1000 and 1000
1008 (cond ((equal k 0) (+ 1721139.29189
1009 (* 365242.13740 y)
1010 (* 0.06134 y y)
1011 (* 0.00111 y y y)
1012 (* -0.00071 y y y y)))
1013 ((equal k 1) (+ 1721233.25401
1014 (* 365241.72562 y)
1015 (* -0.05323 y y)
1016 (* 0.00907 y y y)
1017 (* 0.00025 y y y y)))
1018 ((equal k 2) (+ 1721325.70455
1019 (* 365242.49558 y)
1020 (* -0.11677 y y)
1021 (* -0.00297 y y y)
1022 (* 0.00074 y y y y)))
1023 ((equal k 3) (+ 1721414.39987
1024 (* 365242.88257 y)
1025 (* -0.00769 y y)
1026 (* -0.00933 y y y)
1027 (* -0.00006 y y y y))))
1028 ; actually between 1000 and 3000
1029 (cond ((equal k 0) (+ 2451623.80984
1030 (* 365242.37404 z)
1031 (* 0.05169 z z)
1032 (* -0.00411 z z z)
1033 (* -0.00057 z z z z)))
1034 ((equal k 1) (+ 2451716.56767
1035 (* 365241.62603 z)
1036 (* 0.00325 z z)
1037 (* 0.00888 z z z)
1038 (* -0.00030 z z z z)))
1039 ((equal k 2) (+ 2451810.21715
1040 (* 365242.01767 z)
1041 (* -0.11575 z z)
1042 (* 0.00337 z z z)
1043 (* 0.00078 z z z z)))
1044 ((equal k 3) (+ 2451900.05952
1045 (* 365242.74049 z)
1046 (* -0.06223 z z)
1047 (* -0.00823 z z z)
1048 (* 0.00032 z z z z)))))))
1049
ce4d3fff 1050;;;###autoload
a92ade89 1051(defun solar-equinoxes-solstices ()
087c56fa 1052 "*local* date and time of equinoxes and solstices, if visible in the calendar window.
9f34a2a0 1053Requires floating point."
a92ade89
JB
1054 (let ((m displayed-month)
1055 (y displayed-year))
9f34a2a0
JB
1056 (increment-calendar-month m y (cond ((= 1 (% m 3)) -1)
1057 ((= 2 (% m 3)) 1)
1058 (t 0)))
1059 (let* ((calendar-standard-time-zone-name
a92ade89 1060 (if calendar-time-zone calendar-standard-time-zone-name "UTC"))
9f34a2a0
JB
1061 (calendar-daylight-savings-starts
1062 (if calendar-time-zone calendar-daylight-savings-starts))
1063 (calendar-daylight-savings-ends
1064 (if calendar-time-zone calendar-daylight-savings-ends))
1065 (calendar-time-zone (if calendar-time-zone calendar-time-zone 0))
1066 (k (1- (/ m 3)))
a1506d29 1067 (d0 (solar-equinoxes/solstices k y))
087c56fa
ER
1068 (d1 (list (car d0) (floor (car (cdr d0))) (car (cdr (cdr d0)))))
1069 (h0 (* 24 (- (car (cdr d0)) (floor (car (cdr d0))))))
1070 (adj (dst-adjust-time d1 h0))
9475e724
RS
1071 (d (list (car (car adj))
1072 (+ (car (cdr (car adj)) )
1073 (/ (car (cdr adj)) 24.0))
1074 (car (cdr (cdr (car adj))))))
087c56fa
ER
1075 ; The following is nearly as accurate, but not quite:
1076 ;(d0 (solar-date-next-longitude
1077 ; (calendar-astro-from-absolute
1078 ; (calendar-absolute-from-gregorian
1079 ; (list (+ 3 (* k 3)) 15 y)))
1080 ; 90))
1081 ;(abs-day (calendar-absolute-from-astro d)))
1082 (abs-day (calendar-absolute-from-gregorian d)))
75af4a4a
ER
1083 (list
1084 (list (calendar-gregorian-from-absolute (floor abs-day))
1085 (format "%s %s"
1086 (nth k (if (and calendar-latitude
1087 (< (calendar-latitude) 0))
1088 solar-s-hemi-seasons
1089 solar-n-hemi-seasons))
1090 (solar-time-string
1091 (* 24 (- abs-day (floor abs-day)))
1092 (if (dst-in-effect abs-day)
1093 calendar-daylight-time-zone-name
1094 calendar-standard-time-zone-name))))))))
1095
9f34a2a0
JB
1096
1097(provide 'solar)
1098
6b61353c 1099;;; arch-tag: bc0ff693-df58-4666-bde4-2a7837ccb8fe
9f34a2a0 1100;;; solar.el ends here