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