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